python调用HTMLTestRunner+unittest实现一次执行多个测试类,并生成与每个测试类对应的测试报告,具体看代码,附上整个project代码

时间:2023-03-09 06:39:35
python调用HTMLTestRunner+unittest实现一次执行多个测试类,并生成与每个测试类对应的测试报告,具体看代码,附上整个project代码
python自动化框架雏形,根据自己需要封装:ui自动化,接口自动化均可适用,python版本为python3.x,不要问我为什么不用python2.x,附上整个project代码:http://files.cnblogs.com/files/zw520ly/Python3.rar

测试类1:

# coding=UTF-8
import unittest
import os
import configparser
from Lib.ReaderFile import ReaderFile
from Lib.SendRepuest import SendRequest class RyTest(unittest.TestCase):
"""
在被测试的类中,这三个属性是固定的:
classNmae(一般与当前类名相同,用作测试报告的名称)
title(测试报告的主标题)
description(对整个被测试类测试流程的描述)
"""
className = 'RyTest';
title = '容易网大运营后台测试集';
description = '主要测试流程为金融事业部支付流程' def setUp(self):
#构造excel的文件读取对象
self.excelData = ReaderFile();
#构造发送http请求的client对象
self.ryLogin = SendRequest();
#当前工程的路径
self.parentPath = os.path.dirname(os.getcwd());
#构造配置文件的读取对象
self.config = configparser.ConfigParser()
#读取配置文件内容
self.config.read(self.parentPath + '/DataFile/config.conf');
print('数据初始化完成!!!') def tearDown(self):
print('测试完成!!!') """
大运营后台登录
""" def testLogin(self):
# 声明全局变量cookie,供后面的case调用
global cookie;
# 从excel获取参数
dataList = self.excelData.getExcelValue(self.parentPath + '/DataFile/Rytest.xlsx', 'ryLogin');
#配置文件中读取host及url信息
url = self.config.get('host', 'ry_host') + self.config.get('url', 'login_url');
for item in range(0, len(dataList)):
#获取excel参数
data = dataList[item]['paramer'];
headers = eval(dataList[item]['headers']);
cookieKey = dataList[item]['cookieKey'];
# 发送请求并获取cookie
cookie = self.ryLogin.GetCookie(url, data, headers, cookieKey)
print(cookie); """
大运营后台更新交易通道状态
""" def testUpdate(self):
# 从excel获取参数
dataList = self.excelData.getExcelValue(self.parentPath + '/DataFile/Rytest.xlsx', 'ryUpdate');
# 配置文件中读取host及url信息
url = self.config.get('host','ryfe_host')+self.config.get('url','updateChannelStatus_url');
for item in range(0, len(dataList)):
# 获取excel参数
data = dataList[item]['paramer'];
headers = eval(dataList[item]['headers']);
expectedResult = dataList[item]['expectedResult'];
# 发送请求并获取返回结果
response = self.ryLogin.doPost(url, data, headers, cookie);
print('请求参数:' + data)
print('期望结果:' + expectedResult)
print("实际返回结果:" + response)
print('对比结果:' + str(expectedResult == response)) 测试类2:
# coding=UTF-8
import unittest
import os from Lib.ReaderFile import ReaderFile class TestReaderFile(unittest.TestCase):
"""
在被测试的类中,这三个属性是固定的:
classNmae(一般与当前类名相同,用作测试报告的名称)
title(测试报告的主标题)
description(对整个被测试类测试流程的描述)
"""
className = 'TestReaderFile'
title = '文件读取测试流程'
description = '主要测试csv文件和excel文件的读取' # 初始化参数,从csv文件中读取数据,返回的是list[dict],testcase调用
def setUp(self):
# 构造文件读取器对象
self.reader = ReaderFile();
# 获取当前.py文件的上级目录
self.dataFile = os.path.dirname(os.getcwd())
print('测试数据初始化完成!!!') def tearDown(self):
print('测试结束!!!') def testReadCsvFile(self):
# 获取csv文件的数据,值为dict类型的list
csvList = self.reader.getCsvValue(self.dataFile + '/DataFile/csvData.csv');
# 遍历list中的所有dict,进行逻辑覆盖
for item in range(0, len(csvList)):
# 获取csv数据源中的某列
csvDict = csvList[item];
# 将csv参数初始化dict对象
prama = dict(name=csvDict['name'], age=csvDict['age'], sex=csvDict['sex']);
# 将csv文件中的期望结果中的";"替换成","
expectedStr = csvDict['expectedResult'].replace(';', ',');
# 将形如dict的字符串转换成dict
expectedDict = eval(expectedStr);
print('请求参数:' + str(prama));
print('期望结果:' + str(expectedStr))
# 对比期望结果与实际结果
print('对比结果:' + str(prama == expectedDict)); def testReadExcelFile(self):
# 获取excel文件sheet名称为'表格一'的全部内容,值为dict类型的list
excelList = self.reader.getExcelValue(self.dataFile + '/DataFile/excelFile.xlsx', '表格一');
# 遍历list中的所有dict,进行逻辑覆盖
for item in range(0, len(excelList)):
excelDict = excelList[item];
name = excelDict['name'];
age = excelDict['age'];
sex = excelDict['sex'];
expectedStr = eval(excelDict['expectedResult']);
prama = dict(name=name, age=age, sex=sex);
print('请求参数:' + str(prama))
print('期望结果:' + str(expectedStr))
# 对比期望结果与实际结果
print('对比结果:' + str(prama == expectedStr)); 测试入口封装类:
import HTMLTestRunner
import unittest
import os class HtmlReport:
"""
使用HTMLTestRunner+unittest去执行testcase并生成相应的HTML页面的测试报告
""" def reportTemple(self, classList):
# 遍历testCase(被测试类)的list集,一次执行多个testCase
for item in range(0, len(classList)):
#要执行的testCase
suite = unittest.makeSuite(classList[item]);
# 测试报告名称
filename = os.path.dirname(os.getcwd()) + '/Report/' + classList[item].className + '.html';
#向测试报告中写入测试结果
fp = open(filename, 'wb');
#运行testCase
runner = HTMLTestRunner.HTMLTestRunner(fp, title=classList[item].title,
description=classList[item].description);
runner.run(suite); 实际测试入口:
from Lib.HtmlReport import HtmlReport
from Service.RyTest import RyTest
from Service.TestReaderFile import TestReaderFile """
testCase的入口,调用htmlReport类下的reportTemple方法,传入被测试的test类的list即可,支持多个测试类测试,并生成与之对应的测试报告
"""
if __name__ == '__main__':
classList = [RyTest, TestReaderFile];
htmlReport = HtmlReport();
htmlReport.reportTemple(classList);