locust cookie、token处理

一、locust  实例中的self.client 指向了HTTPsession 类,只要服务端返回了cookie,默认就是可以自动处理cookie的

代码如下:

from locust import HttpUser, TaskSet, task, os


class MyTask(TaskSet):
def on_start(self):
self.data1 = {"userName": "admin", "password": "1234"}
response = self.client.post("/pinter/bank/api/login", data=self.data1, name="login")
print(response.text)

def on_stop(self):
print("用户结束")

@task
def query_account(self):
self.data2 = {"userName": "admin"}
response = self.client.get("/pinter/bank/api/query", params=self.data2, name="quer_account")
print(response.text)


class WebsiteUser(HttpUser):
tasks = [MyTask]
host = "http://lcocalhost:8080"
min_wait = 5000
max_wait = 9000



if __name__ == "__main__":
os.system("locust -f CookieTest.py --host=http://localhost:8088")


二、locust 处理token
代码如下:
from locust import HttpUser, TaskSet, task, os
import json

class MyTask(TaskSet):
def on_start(self):
self.data1 = {"userName": "admin", "password": "1234"}
response = self.client.post("/pinter/bank/api/login2", data=self.data1, name="login")
response1 = json.loads(response.text)
self.token = response1["data"]

def on_stop(self):
print("用户结束")

@task
def TokenTest(self):
self.headers = {
"testfan-token": self.token
}
self.data2 = {"userName": "admin"}
response = self.client.get("/pinter/bank/api/query2",headers = self.headers, params=self.data2, name="Token验证")
print(response.text)


class WebsiteUser(HttpUser):
tasks = [MyTask]
host = "http://lcocalhost:8080"
min_wait = 5000
max_wait = 9000


if __name__ == "__main__":
os.system("locust -f TokenTest.py --host=http://localhost:8088")



posted @ 2022-04-06 22:23  微风轻哨  阅读(649)  评论(0编辑  收藏  举报