記錄python接口自動化測試--根據excel中的期望輸出是否存在請求返回的響應來判斷用例是否執行成功(第八目)


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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM