- 一个完整的业务流程,通常都需要关联数据,最常见的session或者token
将用户登录成功后的token关联到每个任务中
from locust import HttpUser,TaskSet,between,task
import os,sys
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
PathProject = os.path.split(rootPath)[0]
sys.path.append(rootPath)
sys.path.append(PathProject)
n = 0
class UserBehavior(TaskSet):
def login(self):
global n
n += 1
return n
def on_start(self):
self.token = self.login()
@task(4)
def test1(self):
"""
用户浏览首页商品列表
:return:
"""
url = '/list?token=' + str(self.token)
param={}
with self.client.get(url,params=param,headers={},catch_response = True) as response:
print("用户浏览首页商品列表")
class WebsiteUser(HttpUser):
host = 'http://127.0.0.1'
tasks = [UserBehavior]
wait_time = between(1, 2)

每个虚拟用户的请求都关联上了token
网友评论