单接口测试,python实现,实现测试数据和代码的分离。
#encoding=utf-8import requestsimport jsonimport osimport hashlibimport picklefrom conf import *static_data = {}def send_request(interface,value): r = requests.post(interface, data= value) return rdef get_response_info(response_obj): print (response_obj.status_code) print (response_obj.text) print (type(response_obj.json())) print (str(response_obj.json())) print (response_obj.url)def assert_response(response_obj,assert_word): assert assert_word in str(response_obj.json()) #需要默认文件里面写个初始化值1with open("E:\\workspace-python\\test\\test1.txt","r+") as fp: unique_number = fp.readline().strip() fp.seek(0,0) fp.write(str(int(unique_number)+10))with open("E:\\workspace-python\\test\\test_data.txt","r+") as fp: line=fp.readline() print("line*******",line) interface=eval(line.split("|")[0]) #等价于:interface=register.把字符串变成变量用eval print("line*******",interface) value=json.dumps(eval(line.split("|")[1])) #有'userdata'+unique_number, 要把它变成变量 assert_word=line.split("|")[2]#print(interface)#print(type(value))r=send_request(interface,value)get_response_info(r)try: assert_response(r,assert_word)except: print ("接口中断")static_data["username"]=eval(line.split("|")[1])["username"]print(static_data["username"])
执行结果:
E:\workspace-python\test>py -3 b.pyline******* register|{ 'username': 'userdata'+unique_number, 'password': 'zita12345', 'email': '109828@qq.com'}|{ 'code': '00', 'userid':line******* http://39.106.41.11:8080/register/200{ "code": "00", "userid": 55157}{ 'code': '00', 'userid': 55157}http://39.106.41.11:8080/register/userdata188
test1.txt文件里是初始值
test_data.txt
register|{ 'username': 'userdata'+unique_number, 'password': 'zita12345', 'email': '109828@qq.com'}|{ 'code': '00', 'userid':