单元测试之路(七)
程序员文章站
2022-03-04 07:51:27
...
生成测试报告
1.导入测试报告包
import HTMLTestRunner
2.添加测试用例集
def createsuite():
suite = unittest.TestSuite()
# 测试用例目录
case_dirs = os.path.dirname(os.path.abspath('.')) + os.sep + "HappyRun"
discover = unittest.defaultTestLoader.discover(case_dirs, pattern="*.py", top_level_dir=None)
# 添加测试用例到suite中
for test_suite in discover:
for test_case in test_suite:
suite.addTests(test_case)
return suite
3.执行所有用例并生成报告
if __name__ == '__main__':
# 运行测试用例并生成报告
report_path = os.path.dirname(os.path.abspath('.')) + os.sep + "Report" + os.sep + "report.html"
stream = open(report_path, "wb")
runner = HTMLTestRunner.HTMLTestRunner(stream=stream, title='测试报告', description='测试报告详情')
runner.run(createsuite())