1,先看一下接口測試用例json
2,文件1:寫get和post模板api
import requests
class PostGetModels:
def isMethod(self,url,data,method,headers,aeerts,**kw):
if method == 'GET':
self.send_get(url,headers,data)
else:
result = self.send_post(url,data,headers)
return result
def send_post(self,url,data,headers,**kw):
response = requests.post(url=url,data=data,headers=headers)
result = response.text
return result
def send_get(self,url,params,headers,**kw):
response = requests.get(url=url,params=params,headers=headers)
result = response.text
return result
3,文件2:經過傳參讀取進行接口調用
import requests
import json
import unittest
from api實現.PostGetModels import PostGetModels
class TestApi:
def test_http(self,url,data,method,headers,asserts):
pm = PostGetModels()
response = pm.isMethod(url,data,method,headers,asserts)
if json.loads(response)['success'] == asserts:
print('case執行結果---->pass')
else:
print('case執行結果---->failed')
4,文件3:讀取數據文件excel
'''參數化'''import xlrd,xlwt,jsonfrom api實現.讀取參數化接口說明 import TestApiclass ReadFileData: def __init__(self): self.tp = TestApi() def readFileData(self): filename='C:\\Users\\Administrator\\Desktop\\接口測試用例.xls' #讀取文件 workbook = xlrd.open_workbook(filename) #方法一;獲取文件中全部的表 # sheets = workbook.sheet_names() #['Sheet1', 'Sheet2', 'Sheet3'] # worksheet = workbook.sheet_names()[0] # print(worksheet) #獲得表名稱 #方法二:經過索引獲取 #sheets = workbook.sheet_by_index(0)#獲得一個內存地址 # 方法三:經過表名稱獲取 sheets = workbook.sheet_by_name('接口用例')#獲得一個內存地址 ''' sheets的名稱,行數,列數 sheets.name 表名稱 sheets.nrows 行數 sheets.ncols 列數 ''' nums = sheets.nrows #獲得行數 #獲取整行整列的值-->獲得的是個列表 #rows = sheets.row_values(0) #獲取第一行的標題 #cols = sheets.col_values(1) #獲取第二列的數據 # 獲取單元格內容 # print(sheets.cell(1, 0)) #1行0列 # print(sheets.cell_value(2, 0))#2行0列 # print(sheets.row(1)[0])#1行0列 # # 獲取單元格內容的數據類型 # print(sheets.cell(1, 0).ctype) for i in range(1,nums): rows = sheets.row_values(i) #調用接口函數 print('測試用例編號:',rows[0],'測試點:',rows[1]) url=rows[3]+rows[4] #print(url) method=rows[2] data=eval(rows[5]) #eval能夠將普通字符串轉爲字典 asserts=rows[-2] headers='' self.tp.test_http(url,data,method,headers,asserts)rf = ReadFileData()print(rf.readFileData())