本系列汇总,请查看这里:https://www.cnblogs.com/uncleyong/p/18033074
实现目标
解析测试数据中要断言的字段,进行断言,判断用例是否成功。
添加工具模块
utils下添加assert_tool.py
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author: 韧 # @wx: ren168632201 # @Blog: https://www.cnblogs.com/uncleyong/ import jsonpath,json def assert_res(assertRes, res): res_status = 'pass' for i in assertRes.split(";"): i_ = i.strip() if i_: actual_expr = i_.split("=")[0].strip() actual = jsonpath.jsonpath(json.loads(res), actual_expr)[0] expect = i_.split("=")[1].strip() if str(actual) != expect: # print(type(actual),type(expect)) # print("expect: ",expect) # print("actual: ",actual) res_status = 'fail' return res_status return res_status if __name__ == '__main__': res = '{"data":{"id":3396,"username":"tester","password":"******","realName":"tester","sex":"1","birthday":"1990-06-16","phone":"13500000006","utype":"0","addtime":"2024-01-07 20:23:46.0","adduser":"tester"},"code":2001,"msg":"注册成功"}' assertRes = "$.msg=注册成功;" r = assert_res(assertRes,res) print(r)
修改测试类
添加:
assertFields = casedata["assertFields"] # logger.info(">>>>>assertFields:{}".format(assertFields)) if assertFields: res_status = assert_res(assertFields, res.text) logger.info("断言结果是:%s\n\n" % res_status) # gv.res.append([res.text, url, headers, cookies, params, body, res_status]) assert res_status == "pass"
运行结果
为了演示效果,最后一条用例断言失败
标签:11,status,actual,python,res,接口,assert,expect,assertFields From: https://www.cnblogs.com/uncleyong/p/18050230