python unittest--HTMLTestRunner生成报告

同一个目录下

test1.py

import unittest

class test1(unittest.TestCase):
    u"类1的测试用例"
    def test1(self):
        u"测试用例1"
        print "test001lalala"

    def test2(self):
        u"测试用例2"
        print "test002hahaha"

test2.py

import unittest

class test2(unittest.TestCase):
    u"类2的测试用例"
    def test3(self):
        u"测试用例3"
        print "test001lalala"

    def test4(self):
        u"测试用例4"
        print "test002hahaha"

run.py

import unittest
from HTMLTestRunner import *
import sys

reload(sys)
sys.setdefaultencoding("utf-8")

def main():
    discover=unittest.TestLoader().discover(".", "test*.py")

    with open('test_result.html', 'wb') as file:
        runner = HTMLTestRunner(stream=file,
                                                   title='测试报告',
                                                  description='xxx测试报告',
                                                  verbosity=2)
        runner.run(discover)


if __name__=="__main__":
main()

原文地址:https://www.cnblogs.com/dmtz/p/10999618.html