【Python+unittest】测试套件TestSuite执行完unittest测试用例,用HTMLTestRunner打印生成HTML格式的测试报告

根据之前的一段代码,生成运行后的测试报告。

https://blog.csdn.net/woshiyigerenlaide/article/details/104117384

文件名称:Calculator.py。一段用python编写的计算器Calculator代码

def add(x,y):
    return x + y
def sub(x,y):
    return x - y

文件名称:test36.py 。这是单元测试代码:测试用例代码

# _*_ coding: utf-8 _*_
from unittest import TestCase, main, skip
from test35 import add, sub

x, y = 5, 3
class MyTestCase(TestCase):
    def setUp(self):
        pass
    def test_01(self):
        result1 = add(x, y)
        self.assertEqual(result1, 8, "add方法,执行结果不正确")
    @skip("stop test_02")
    def test_02(self):
        print('test-02')

    # test_03的断言刻意写错了,让大家可以看看执行结果
    def test_03(self):
        result2 = sub(x, y)
        self.assertEqual(result2, 22, "sub方法,执行结果不正确")

    def tearDown(self):
        pass

if __name__ == '__main__':
    main()

这是测试套件的代码:(这段代码生成测试报告)

# _*_ coding: utf-8 _*_
from test36 import MyTestCase
from unittest import TestSuite, TextTestRunner
from HTMLTestRunner import HTMLTestRunner

suite = TestSuite()
suite.addTest(MyTestCase('test_01'))
suite.addTest(MyTestCase('test_02'))
suite.addTest(MyTestCase('test_03'))

if __name__ == '__main__':
    f = file('result.html', 'wb')
    runner = HTMLTestRunner(stream=f, title='My Test', description='Test Html by HTMLTestRunner.')
    runner.run(suite)

生成了result.html。

复制路径,到浏览器打开,查看报告

发布了94 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/woshiyigerenlaide/article/details/104189293