找了很多代码,测试断言效果。(会出现报错问题,非常难看哦????)
with self.client.post(url,headers=headers,json=query,catch_response=True) as response:
if json.loads(response.text)['code'] == '0':
response.success()
else:
response.failure('Failed!')
最后找到这个断言,非常好用,完美解决了问题
from json import JSONDecodeError
with self.client.post(url,headers=headers,json=query,catch_response=True) as response:
if response.status_code != 200:
response.failure("Did not get expected status code")
try:
if json.loads(response.text)['code'] != '0':
response.failure("Did not get expected value in status")
except JSONDecodeError:
response.failure("Response could not be decoded as JSON")
except KeyError:
response.failure("Response did not contain expected key 'status'")
最后
以上就是怡然朋友最近收集整理的关于Python—Locust 设置断言,巧遇请求都是200的情况,使用断言来操作。的全部内容,更多相关Python—Locust内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复