随笔 - 148  文章 - 3  评论 - 2  阅读 - 11万

(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()
复制代码

 

posted on   bruce_he  阅读(115)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示