#对购物网站进行注册+登录+查询的业务场景进行性能测试
多个任务分配虚拟用户权重的方法有二:
(登录的账号,事先已注册好)
from locust import HttpLocust,task,TaskSet,between
import csv
class user_login_search(TaskSet):
@task(3)
def test_login(self):
for i in range(10, 16):
username = "xiaobing" + str(i)
login_data = {"login_info": username, "password": "123456"}
# 发送首页请求给服务器,登录有两个参数用post。
response=self.client.post("/index.php?controller=simple&action=login_act", data=login_data).text
loc = response.find(username)
if loc > 0:
print(username + "登录成功")
else:
print(username + "登录失败")
@task(2)
def test_search(self):
#以只读方式打开测试数据文件
file=open("searchdata.csv","r")
file2=open("searchresult.csv","w")
row=csv.reader(file)
for word in row:
#print(word)
response=self.client.get("/index.php?controller=site&action=search_list&word="+str(word)).text
#print(response)
loc=response.find(str(word))
if(loc>0):
print(str(word)+"搜索成功")
result=str(word)+"搜索成功"
file2.write(result+"\n")
else:
print(str(word) + "搜索失败")
result = str(word) + "搜索失败"
file2.write(result+"\n")
file2.close()
@task(1)
def test_reg(self):
for i in range(1, 6):
user = "baby" + str(i)
regdata = {"email": user + "@126.com",
"username": user,
"password": "123456",
"repassword": "123456",
"captcha": "11111",
"callback": ""}
response = self.client.post("/index.php?controller=simple&action=reg_act", data=regdata).text
loc = response.find("恭喜,操作成功!")
if loc > 0:
print(user + "注册成功")
else:
print(user + "注册失败")
#tasks = {test_login:1,test_search:3}
class WebSiteUser(HttpLocust):
host = "http://localhost/iwebshop"
task_set = user_login_search # 固定变量
wait_time = between(2, 5)