1.首先在excel中的expectValue列填好預期結果值
這里判斷接口成功的依據是預期結果值是否存在於接口返回的數據中。
首先,要知道在之前封裝的get/post請求方法中返回的是‘str’,也就是json格式的字符串
而從excel文件解析出的expectValue也是‘str’,這樣就可以將兩者對比,來判斷expectValue是否存在於接口返回的數據中
使用if進行判斷,一個簡單的demo:
# coding:utf-8 import requests, json url = 'http://192.168.xxx.xxx:7001/xxx/api/xxx/xxx/query.v' data = {"controlSeq": "2018118579"} r = requests.post(url, data=data) re = json.dumps(r.json(),indent=2, sort_keys=False, ensure_ascii=False) print(re) print(type(r)) print(type(r.json())) print(type(re)) expect_value = '企業事項_hmk' if expect_value in re: """關鍵字in判斷前者是否存在於后者之中""" print('測試通過') else: print('測試失敗')
修改主函數代碼:
# coding:utf-8 from base.run_method import RunMain from util.handle_excel import * from util.common import CommonUtil import json class RunTestCase: def __init__(self): self.Runmain = RunMain() # 實例化調用get/post請求對象 self.data = HandleExcel() # 實例化操作excel文件對象 self.common = CommonUtil() # 實例化判斷實際結果是否與預期結果一致 def go_run(self): rows_count = self.data.get_rows() # 獲取excel行數 for i in range(1,rows_count): # 利用行數進行迭代處理每個接口 url = self.data.get_value(i, get_url()) # 循環獲取url的值 method = self.data.get_value(i, get_method()) # 循環獲取method的值 data = json.loads(self.data.get_value(i, get_params())) # 循環獲取請求參數,並將得到的數據反序列化 expect = self.data.get_value(i, get_expectvalue()) # 循環獲取期望輸出 is_run = self.data.get_value(i, get_priority()) # 獲取是否運行,即判斷excel中priority是不是"H" if is_run == 'H': res = self.Runmain.run_main(url, method, data) # 調用get/post主函數 if expect in res: print('測試通過') else: print('測試失敗') if __name__ == '__main__': run = RunTestCase() run.go_run()