使用Locust遇到的问题

一、locust版本0.13之后已经废除了min_wait和max_wait的使用

    min_wait = 3000
    max_wait = 7000

改为使用

wait_time = between(3,7)

官方说明:
在这里插入图片描述

二、request参数catch_response的使用

官方说明:
在这里插入图片描述
简单来讲,如果需要locust中的Fails正常记录到错误的信息,就需要使用catch_response来捕获请求,将其标记为成功或者失败
在这里插入图片描述
实现代码如下:

    def on_start(self):
        body = {"username":"scqy1","password":"123123"}
        response = self.client.post("http://10.46.59.15/login",json=body,catch_response=True)
        # 解析响应的json数据,后续就可以正常使用列表或者字典操作数据
        newText = json.loads(response.text)
        self.token = newText["result"]["token"]
        if newText["code"] == 0:
            response.success()
        else:
            response.failure("请求失败")

官方关于success与failure的解说:
在这里插入图片描述

三、post请求参数的说明

先说说遇到的问题,我使用了三种传参,得到的提示如图

self.client.post("http://10.46.59.156:8002/login",body)
self.client.post("http://10.46.59.156:8002/login",body=body)
response = self.client.post("http://10.46.59.156:8002/login",data=body)

在这里插入图片描述
最后我换成json成功了

response=self.client.post("http://10.46.59.156:8002/login",json=body)

官方说明:
在这里插入图片描述

四、执行顺序

官网说明:
在这里插入图片描述

五、执行程序

官网说明(谷歌翻译的结果):
在这里插入图片描述
实例:

class Run_test(HttpLocust):
    # 此处必须使用task_set,否则会提示No Locust class found!
    task_set = Test_yl
    #此处必须使用between的方法,min_wait和的用法max_wait在0.13版本之后就被弃用了
    wait_time = between(3, 7)
发布了44 篇原创文章 · 获赞 1 · 访问量 1481

猜你喜欢

转载自blog.csdn.net/cc_park/article/details/103605167
今日推荐