单接口测试框架
Posted huiziz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单接口测试框架相关的知识,希望对你有一定的参考价值。
实现测试数据和代码的分离。
#encoding=utf-8 import requests import json import os import hashlib import pickle from conf import * static_data = {} def send_request(interface,value): r = requests.post(interface, data= value) return r def 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()) #需要默认文件里面写个初始化值1 with 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 est>py -3 b.py line******* register|{‘username‘: ‘userdata‘+unique_number, ‘password‘: ‘zita123 45‘, ‘email‘: ‘[email protected]‘}|{‘code‘: ‘00‘, ‘userid‘: line******* http://39.106.41.11:8080/register/ 200 {"code": "00", "userid": 55157} <class ‘dict‘> {‘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‘: ‘[email protected]‘}|{‘code‘: ‘00‘, ‘userid‘:
以上是关于单接口测试框架的主要内容,如果未能解决你的问题,请参考以下文章
接口自动化框架(java)--5.通过testng.xml生成extentreport测试报告