web自动化测试实战之生成测试报告
更新时间:2022-06-14 09:15:09 作者:多测师 浏览:112
01.第一种我们引用的 HTMLTestReportCN.py 如下代码:
# -*-coding=utf-8-*-
import unittest
import os, time
import HTMLTestReportCN
# 用例路径
case_path = 'F:\\python3\\python_code\\test_case_suite'
def AllTest():
'''获取所有的测试模块'''
suite = unittest.TestLoader().discover(
start_dir=os.path.dirname(case_path),
pattern='case*.py',
top_level_dir=None
)
return suite
def getNowTime():
'''获取当前的时间'''
return time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
def run():
fileName = os.path.join('F:\\python3\\report',
getNowTime() + 'report.html')
fp = open(fileName,'wb')
runner = HTMLTestReportCN.HTMLTestRunner(
stream=fp,
title=u'项目测试环境单元测试报告',
description=u'注:为减少时间人力成本,提高转测质量,特每次对测试环境待发布的代码会对基础功能模块进行单元测试,进一步的提高测试效率,如下为用例执行结果,请查阅!')
runner.run(AllTest())
if __name__ == '__main__':
run()
复制代码
注解:在以上完善后的AllTest.py文件中其中导入了os,timemo模块、HTMLTestReportCN 库。getNowTime 方法用来获取当前时间,每一次生成的测试报告如果文件名称一致,由于加上了最新时间信息,便可以根据文件名称确认哪个是最新的测试报告。
run 方法用来执行测试套件中的测试用例和生成测试报告。在report 文件夹下生成了最新的测试报告。
02.第二种我们引用的 HTMLTestRunner.py 如下代码:
# -*-coding=utf-8-*-
import unittest
import os, time
import HTMLTestRunner
# 用例路径
case_path = 'F:\\python3\\python_code\\test_case_suite'
def AllTest():
'''获取所有的测试模块'''
suite = unittest.TestLoader().discover(
start_dir=os.path.dirname(case_path),
pattern='case*.py',
top_level_dir=None
)
return suite
def getNowTime():
'''获取当前的时间'''
return time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
def run():
fileName = os.path.join('F:\\python3\\report',
getNowTime() + 'report.html')
fp = open(fileName,'wb')
runner = HTMLTestRunner.HTMLTestRunner(
stream=fp,
title=u'巡服带教测试环境单元测试报告',
description=u'注:为减少时间人力成本,提高转测质量,特每次对测试环境待发布的代码会对基础功能模块进行单元测试,进一步的提高测试效率,如下为用例执行结果,请查阅!')
runner.run(AllTest())
if __name__ == '__main__':
run()
以上内容为大家介绍了web自动化测试实战之生成测试报告,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/