目录
前言
- 有时在测试中需要判断响应状态码等状态,这时候就需要断言,那么就一起来学习下locust设置断言吧。
代码示例
# -*- coding: utf-8 -*-
# @Time : 2021/4/10
# @Author : 大海
import os
from locust import HttpUser, task, TaskSet, between
class MyUser(TaskSet):
@task
def my_task(self):
# catch_response=True, 允许该请求被标记为失败
with self.client.get('/', catch_response=True) as response:
# 断言,如果响应状态码为200,通过;否则失败
if response.status_code == 200:
response.success()
else:
response.failure('Failed!')
class User(HttpUser):
tasks = [MyUser]
wait_time = between(3, 25)
host = "https://www.baidu.com"
if __name__ == '__main__':
file_path = os.path.abspath(__file__)
os.system(f'locust -f {file_path} --web-host=127.0.0.1')
说明:
- catch_response = True :布尔类型,如果设置为 True, 允许该请求被标记为失败。