接口框架,基于文件做的
都分离成单独的模块
#主程序,获取测试用例,发送请求,并生成测试报告
mian.py
#encoding=utf-8 import requests import json import os import hashlib import pickle from conf import register from Util import send_request,get_response_info,assert_response import re from DataHandler import test_data_pre_handler global_values = {}
#处理测试数据格式,并获取测试数据后返回 def get_testcases(testcase_file_path): test_cases = [] with open(testcase_file_path,"r") as fp: for line in fp: print(line) interface_name=eval(line.split("||")[0]) test_value=json.dumps(eval(line.split("||")[1])) test_value=test_data_pre_handler(test_value) assert_word=line.split("||")[2].strip() test_cases.append((interface_name,test_value,assert_word)) return test_cases test_cases_num = 0 test_cases_success_num = 0 test_cases_failuer_num = 0
#取出测试数据遍历发送请求
for test_case in get_testcases("test_data.txt"): test_cases_num +=1 r=send_request(test_case[0],test_case[1]) get_response_info(r) try: assert_response(r,test_case[2]) result = "成功" test_cases_success_num+=1 global_values["userid"]=re.search(r'"userid": (d+)}',r.text).group(1) except AssertionError: result = "失败" test_cases_failuer_num +=1 except Exception as e: print(e) test_cases_failuer_num +=1 print("请求接口的url:%s" %r.url) print("测试接口的数据:%s" %test_case[1]) print("测试接口的返回结果:%s" %r.text) print("断言的值:%s" %test_case[2]) print("断言的结果:%s" %result) print("*"*50) print("一共运行了%s个测试用例" %test_cases_num) print("成功运行了%s个测试用例" %test_cases_success_num) print("失败运行了%s个测试用例" %test_cases_failuer_num) print(global_values["userid"])
#生成unique_number(注册用户名用)
UniqueNumber.py
#需要默认文件里面写个初始化值 def get_unique_number(data_file_path="data.txt"): with open(data_file_path,"r+") as fp: unique_number = fp.readline().strip() fp.seek(0,0) fp.write(str(int(unique_number)+1)) return unique_number if __name__ == "__main__": print(get_unique_number()) print(get_unique_number())
#发送接口请求
Util.py
import requests import json import os import hashlib import pickle 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()) #需要将分离出来的模块,单独做单元测试。 if __name__ =="__main__": #把数据转化成json串 data = json.dumps({"username":"wxh21ss","password":"12345678abc","email":"wxh@qq.com"}) r = send_request("http://39.106.41.11:8080/register/",data) get_response_info(r) assert_response(r,"00")
#测试数据和变量替换
DataHandler.py
import re import json from UniqueNumber import get_unique_number ''' 函数的功能: 1 请求的数据:wulaoshi${unique_num1}(自定义格式的变量) 2 写了一个函数,参数就是请求的变量,在函数里面把${unique_num1}替换成了get_unique_number()返回的唯一数字 3 函数返回替换后的结果 ''' #前置测试数据处理 def test_data_pre_handler(data): if re.search(r"${unique_w+}",data): print(re.search(r"${unique_w+}",data).group(0)) unique_num =get_unique_number() data = re.sub(r"${unique_w+}",unique_num,data) print(data) return data return data if __name__ =="__main__": data=json.dumps({'username': 'wulaoshi${unique_num1}', 'password': 'wulaoshi12345', 'email': 'wulaoshi@qq.com'}) test_data_pre_handler(data)
#配置数据,存放配置信息
conf.py
ip="39.106.41.11" port="8080" register="http://"+ip+":"+port+"/register/"
#测试数据
test_data.txt
register||{'username': 'wulaoshi${unique_num1}', 'password': 'wulaoshi12345', 'email': 'wulaoshi@qq.com'}||{'code': '00',
#unique_number要用的data数据
data.txt
1114
总结:
接口测试框架思想:
配置和程序分离
公共函数和主程序分离
测试数据和程序的分离
有测试报告
有日志