search_per_test.py
#针对查询功能进行性能测试标签:search,word,--,查询,功能测试,result,str,print,csv From: https://www.cnblogs.com/zhangyideyl/p/17146540.html
from locust import HttpLocust,task,TaskSet
import csv
import datetime
import os
class UserBehavior_search(TaskSet):
@task
def test_search(self):
#以只读方式打开测试数据文件
file1=open("searchdata.csv","r") #可写测试数据
###########################################
#若测试报告文件存在,先进行删除
# result = os.path.exists("searchresult.csv")
# #print(result)
# if result:
# os.remove("searchresult.csv")
# ###########################################
file2=open("searchresult.csv","a") #可写测试报告,a是追加写,w是覆盖写
rows=csv.reader(file1)
for word in rows:
#print(word)
response=self.client.get("/index.php?controller=site&action=search_list&word="+str(word)).text
#print(response)
loc = response.find(str(word)) #在结果中response中找关键字"word"
te=datetime.datetime.now()
if loc >= 0:
#print( str(word)+"测试成功")
result=str(te)+str(word)+"测试成功"
file2.write(result+"\n") # "\n"回车
else:
#print(str(word)+"测试失败")
result = str(te)+str(word) + "测试失败"
file2.write(result + "\n")
file2.close() #以写的方式打开的文件需要关闭,以读的方式打开的不用关闭
class webSiteUser(HttpLocust):
host="http://localhost/iwebshop"
task_set = UserBehavior_search
min_wait = 2000
max_wait=5000