1.技术介绍
框架:unittest
请求处理:requests
excel数据处理:openpyxl
参数化:ddt
配置解析器:configparser
报告模板:HTMLTestRunnerNew.py(下载地址:https://pan.baidu.com/s/1w9AZU9AkIpxCYuzTto0EQA?pwd=1234)
testdemo.xlsx:测试数据,注意:implement=y为执行数据,否则不执行
2.代码结构
----->case.config 配置文件 ----->DoCase.py 测试用例类 ----->DoConfig 配置文件处理 ----->DoExcel.py Excel文件处理类,筛选有效数据返回 ----->DoHttp.py http请求处理类 ----->HttpRunner unittest加载用例执行生成报告 ----->report.html 执行后生成的报告 ----->HTMLTestRunnerNew.py 测试报告模板
3.目录介绍
4.excel数据
5.case.config
[CASE]
caseid=[1,3]
6.Docase.py
#测试用例类 import unittest from test02.DoHttp import Request_Http#引入请求类 from ddt import ddt,data,unpack#引入ddt做参数化 from test02.DoExcel import GetExcelData data_list = GetExcelData("C:\\Users\\Administrator\\Desktop\\testdemo.xlsx","s1").get_data() #用例类 @ddt#装饰器装饰类 class HttpCase(unittest.TestCase):#继承unittest测试用例类 @data(*data_list)#脱外套 def test_login_yes(self,item): res = Request_Http(item["method"], item["url"], item["data"]).request_http() try: self.assertEqual(item["expect"],res.json()["code"])#断言:预期与实际是否相等 except Exception as e: print("断言异常:{0}".format(e)) raise e#抛出异常
7.DoConfig.py
import configparser class GetConfig(): def get_config_data(self,file,section,option): cf = configparser.ConfigParser() cf.read(file, encoding="utf8") # 读取config,有中文注意编码 # 返回value return cf[section][option]
8.DoExcel.py
from openpyxl import load_workbook from test02.DoConfig import GetConfig class GetExcelData(): def __init__(self,file,sheet): self.file=file self.sheet=sheet def get_data(self): caseid = eval(GetConfig().get_config_data("case.config","CASE","caseid"))#处理配置文件获取要执行的caseid wb = load_workbook(self.file)#打开excel sheet = wb[self.sheet]#定位工作簿 data_list = [] for n in range(2,sheet.max_row+1): data_dic = {} for m in range(2,sheet.max_column+1): data_dic["case_id"] = sheet.cell(n, 1).value data_dic["module"] = sheet.cell(n, 2).value data_dic["title"] = sheet.cell(n, 3).value data_dic["method"]=sheet.cell(n,4).value data_dic["url"]=sheet.cell(n,5).value data_dic["data"]=eval(sheet.cell(n, 6).value)#还原数据类型 data_dic["expect"]=sheet.cell(n, 7).value if data_dic["case_id"] in caseid:#判断case_id是否包含在配置文件中 data_list.append(data_dic) return data_list
5.DoHttp.py
#http请求处理类 import requests class Request_Http(): def __init__(self,method,url,data,expected=None,headers=None,cookie=None): self.method=method.lower()#请求方式转小写 self.url=url self.data=data self.excepted=expected self.headers=headers self.cookie=cookie def request_http(self): if self.method=="get": try: return requests.get(self.url, params=self.data, headers=self.headers, cookies=self.cookie) except Exception as e: print("异常请求:{0}".format(e)) raise e # 抛出异常 else: try: return requests.post(self.url,params=self.data,headers=self.headers,cookies=self.cookie) except Exception as e: print("异常请求:{0}".format(e)) raise e#抛出异常
6.HttpRunner.py
#unittest加载用例执行生成报告 import unittest from test02 import DoCase import HTMLTestRunnerNew suite = unittest.TestSuite()#存放测试用例 loader = unittest.TestLoader()#加载器 suite.addTest(loader.loadTestsFromModule(DoCase))#加载测试模块 #上下文管理器 with open("report.html","wb") as file: runner = HTMLTestRunnerNew.HTMLTestRunner(stream=file, verbosity=2,title="标题",description="备注",tester="姓名") runner.run(suite)
7.report.html
8.报告模板下载
HTMLTestRunnerNew.py(下载地址:https://pan.baidu.com/s/1w9AZU9AkIpxCYuzTto0EQA?pwd=1234)