Locust学习笔记(2)

1.实现登录的基本功能,输出响应,证明脚本正确
2.多用户随机登录:构造随机数据,doLogin方法中用随机数据进行请求
3.添加初始化方法on_start:类似于构造方法,每个虚拟用户只运行一次。
4.添加检查点(断言)
    -  在请求方法中设置catch_response参数为True
    -  调用sucess或者failure方法标注成功或失败
from locust import HttpLocust, TaskSet, task, between
from random import randint

class TestLogin(TaskSet):
   # 每个虚拟用户执行一次
def on_start(self): self.login_data = [{"username": "user1", "password": "pwd1"}, {"username": "user2", "password": "pwd2"}, {"username": "user3", "password": "pwd3"}] self.ranIndex = randint(0, len(self.login_data) - 1) print("----------------------") @task def doLogin(self): print(self.login_data[self.ranIndex]) response = self.client.post("/admin/", data=self.login_data[self.ranIndex],catch_response=Ture) print(response.text) # 断言 if "login-pass" in response.text: response.success() else: response.failure("Can not login!") class WebSite(HttpLocust): task_set = TestLogin wait_time = between(3, 7)
原文地址:https://www.cnblogs.com/ronyjay/p/13745461.html