码迷,mamicode.com
首页 > Web开发 > 详细

unittest生成测试报告(HTMLTestRunner)

时间:2018-02-24 17:58:45      阅读:712      评论:0      收藏:0      [点我收藏+]

标签:函数   tle   ==   utf-8   odi   cas   body   coding   htm   

# coding:utf-8
import unittest
import os
import HTMLTestRunner

# python2.7要是报编码问题,就加这三行,python3不用加

import sys
reload(sys)
sys.setdefaultencoding(‘utf8‘)

# 用例路径
case_path = os.path.join(os.getcwd(), "case")
# 报告存放路径
report_path = os.path.join(os.getcwd(), "report")
def all_case():
    discover = unittest.defaultTestLoader.discover(case_path,
                                                    pattern="test*.py",
                                                    top_level_dir=None)
    print(discover)
    return discover

if __name__ == "__main__":
    # runner = unittest.TextTestRunner()
    # runner.run(all_case())

    # html报告文件路径
    report_abspath = os.path.join(report_path, "result.html")
    fp = open(report_abspath, "wb")
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp,
                                           title=u‘自动化测试报告,测试结果如下:‘,
                                           description=u‘用例执行情况:‘)

    # 调用add_case函数返回值
    runner.run(all_case())
    fp.close()

unittest生成测试报告(HTMLTestRunner)

标签:函数   tle   ==   utf-8   odi   cas   body   coding   htm   

原文地址:https://www.cnblogs.com/chukun/p/8466887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!