是有这么一个要求,需要用户请求一次接口,会推给他他们一次实验结果,次数太少无法看到自己设置的概率是否符合。内容十分简单,不复杂,但是当任务并行的时候,怎么才能保证质量又保证测试范围和效率,才是首要解决的问题,所以我们并不是需要多么高大上的测试代码,怎么简单怎么来。
代码很简单,我们在测试过程中首先要保证正确性,其次是提高效率,我在其他项目过程中执行了几次脚本,最后发现比率没问题,测试前还跟开发review了一下脚本和思路,也运行过1000 5000 10000次,比率都是符合要求的。后来,我在和开发沟通过程中还了解到了random的seed的作用,seed不能被单独调用,当seed后面数值一致的时候,才会获取两组相同的随机数。这样可以控制实验变量,这样更能设计实验更准确,更具有实验指导性。
#coding=utf-8
import requests
import json
import time
def testExpClassAB(ii):
# 定义登录获取cookie
# post请求方法
login_url = "https://loginXXXXXXXXXXXXXXXXXXXXX" #登录接口地址
api_url = "https://VVVVVVVVVVVVV" #被测接口连接
data_login = {"sign": "XXXXXXX", "pwd": "XXXXX", "vcode": 0}
login_re = requests.post(login_url, data=data_login)
login_cookie = login_re.cookies
data_expclass = {\
"step": 3, # 步骤
"gradeId": 8, # 年级
"subjectId": 2, # 学科
"provinceId": 1} # 省市
i = 1
flag_407 = 0.0
flag_408 = 0.0
flag_4 = 0.0
while i <= ii:
expclass = requests.post(api_url, data=data_expclass, cookies=login_cookie)
re_content = json.loads(expclass.content)
print("第"+str(i)+"条:"+str(re_content['data']))
for item in re_content['data']:
if str(item['timeSlot']).find('[407]') > 0:
flag_407 += 1
elif str(item['timeSlot']).find("[408]") > 0:
flag_408 += 1
elif str(item['timeSlot']).find("[4]") > 0:
flag_4 += 1
else:
pass
i += 1
time.sleep(0.5)
print("flag_4="+str(flag_4), "flag_407="+str(flag_407), "flag_408="+str(flag_408))
percent_4 = flag_4/(7*i)
percent_407 = flag_407/(7*i)
percent_408 = flag_408/(7*i)
print("percent_4="+str(percent_4), "percent_407="+str(percent_407), "percent_408="+str(percent_408))
percent_sum = percent_4+percent_407+percent_408
print("percent_sum : "+str(percent_sum))
if __name__ == "__main__":
Numbers = 10
try:
testExpClassAB(Numbers)
except Exception as e:
print("本次测试异常:"+e)
print("============测试结束!============")