(Python)使用 HTMLTestRunner 实现 unittest 报告生成
1. 下载 HTMLTestRunner 文件
地址: http://tungwaiyip.info/software/HTMLTestRunner.html
2. 将 .py 文件保存到 \Python37\Lib\site-packages
3. 将 HTMLTestRunner.py 由 python2 格式改为 python3 格式
94 行 | import io | import StringIO |
539 行 | self.outputBuffer = StringIO.StringIO() | self.outputBuffer = io.StringIO() |
631 行 | print >>sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime) | print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)) |
642 行 | if not rmap.has_key(cls): | if not cls in rmap: |
766 行 | uo = o.decode('latin-1') | uo = o |
772 行 | ue = e.decode('latin-1') | ue = e |
4. 调用生成
import unittest import time from Get_Path import GET_PATH from HTMLTestRunner import HTMLTestRunner class RunTestSuite(object): def __init__(self): self.suite = unittest.TestSuite() self.loader = unittest.TestLoader() def run_by_discover(self): testcases_dir = GET_PATH + "\Test_Case_Layer" self.suite.addTest(self.loader.discover(testcases_dir, pattern="*_Test.py")) def generate_report(self): get_time = time.strftime("%Y%m%d%H%M%S", time.localtime()) report_name = "CRM_Project" + get_time + ".html" report_path = GET_PATH + "\Main_Layer\Report\\" + report_name with open(report_path, 'wb') as fp: runner = HTMLTestRunner(stream=fp, verbosity=2, title="CRM Test Report", description="Detailed as below" ) runner.run(self.suite) fp.close() if __name__ == '__main__': run_testsuite = RunTestSuite() run_testsuite.run_by_discover() run_testsuite.generate_report()