一、EXCEL文件接口保存方式,如圖。html
二、而後就是讀取EXCEL文件中的數據方法,以下:git
1 import xlrd 2 3 4 class readExcel(object): 5 def __init__(self, path): 6 self.path = path 7 8 @property 9 def getSheet(self): 10 # 獲取索引 11 xl = xlrd.open_workbook(self.path) 12 sheet = xl.sheet_by_index(0) 13 return sheet 14 15 @property 16 def getRows(self): 17 # 獲取行數 18 row = self.getSheet.nrows 19 return row 20 21 @property 22 def getCol(self): 23 # 獲取列數 24 col = self.getSheet.ncols 25 return col 26 27 # 如下是分別獲取每一列的數值 28 @property 29 def getName(self): 30 TestName = [] 31 for i in range(1, self.getRows): 32 TestName.append(self.getSheet.cell_value(i, 0)) 33 return TestName 34 35 @property 36 def getData(self): 37 TestData = [] 38 for i in range(1, self.getRows): 39 TestData.append(self.getSheet.cell_value(i, 1)) 40 return TestData 41 42 @property 43 def getUrl(self): 44 TestUrl = [] 45 for i in range(1, self.getRows): 46 TestUrl.append(self.getSheet.cell_value(i, 2)) 47 return TestUrl 48 49 @property 50 def getMethod(self): 51 TestMethod = [] 52 for i in range(1, self.getRows): 53 TestMethod.append(self.getSheet.cell_value(i, 3)) 54 return TestMethod 55 56 @property 57 def getUid(self): 58 TestUid = [] 59 for i in range(1, self.getRows): 60 TestUid.append(self.getSheet.cell_value(i, 4)) 61 return TestUid 62 63 @property 64 def getCode(self): 65 TestCode = [] 66 for i in range(1, self.getRows): 67 TestCode.append(self.getSheet.cell_value(i, 5)) 68 return TestCode
三、EXCEL中的數據讀取成功後,而後咱們須要對於讀出來的數據進行相應的處理。以下。固然示例中只是簡單列了一下關於POST,GET等二種方式,實際還有不少其它方式,如put,delete等,請求中也還會包括headers,這些均可以自已添加上去。json
1 import requests 2 import json 3 from xl.read_xl import readExcel 4 from pubulic_way.get_token import get_token 5 6 7 class testApi(object): 8 def __init__(self, method, url, data): 9 self.method = method 10 self.url = url 11 self.data = data 12 13 14 @property 15 def testApi(self): 16 # 根據不一樣的訪問方式來訪問接口 17 try: 18 if self.method == 'post': 19 r = requests.post(self.url, data=json.dumps(eval(self.data))) 20 elif self.method == 'get': 21 r = requests.get(self.url, params=eval(self.data)) 22 return r 23 except: 24 print('失敗') 25 26 def getCode(self): 27 # 獲取訪問接口的狀態碼 28 code = self.testApi.json()['error'] 29 return code 30 31 def getJson(self): 32 # 獲取返回信息的json數據 33 json_data = self.testApi.json() 34 return json_data
四、最後咱們就能夠調用以前準備好的方法進去測試了。api
1 from base.base_test import baseTest 2 from xl.read_xl import readExcel 3 from pubulic_way.test_api_way import testApi 4 import unittest 5 6 7 class testLoginApi(unittest.TestCase): 8 def testLoginApi(self): 9 '''測試發佈評倫接口。''' 10 excel = readExcel(r'F:\path\add_thread_data.xlsx') 11 name = excel.getName 12 data = excel.getData 13 url = excel.getUrl 14 method = excel.getMethod 15 uid = excel.getUid 16 code = excel.getCode 17 row = excel.getRows 18 for i in range(0, row - 1): 19 api = testApi(method[i], url[i], data[i]) 20 apicode = api.getCode() 21 apijson = api.getJson() 22 if apicode == code[i]: 23 print('{}、{}:測試成功。json數據爲:{}'.format(i + 1, name[i], apijson)) 24 else: 25 print('{}、{}:測試失敗'.format(i + 1, name[i])) 26 27 28 if __name__ == '__main__': 29 unittest.main(verbosity=2)
五、最後還須要把咱們的結果展現出來,這個就很簡單了,利用htmltestrunner來展現。展現一張報告的切圖。app
源碼地址:https://git.coding.net/gfihdx/demo.git 若是有須要的話能夠去clone一下。post