使用HTMLTestRunnerNew模块,生成单元测试的html报告,报告标题根据对应测试时间。
import unittest from datetime import datetime from Python_0717_unittest.HTMLTestRunnerNew import HTMLTestRunner one_suite = unittest.defaultTestLoader.discover(".") report_full_path = "./reports/"+"report_"+f"{datetime.now():%Y%m%d%H%M%S}"+".html" # 放入指定文件夹中 # with open(r"F:python_homeworkPython_0715_unittest_homework eport.html", mode="wb") as save_to_file: with open(report_full_path, mode="wb") as save_to_file: # 报告名称添加时间信息 curr_time = datetime.datetime.now() time_str = datetime.datetime.strftime(curr_time, '%Y-%m-%d %H:%M:%S') one_runner = HTMLTestRunner(stream=save_to_file, title=f"Python20期第一份测试报告 时间:{time_str}", verbosity=2, description="这是homework的内容啦", tester="zfy") one_runner.run(one_suite)