unittest 使用 HTMLTestRunner 生成测试报告
code
import unittest from HTMLTestRunner import HTMLTestRunner class Test3(unittest.TestCase): @classmethod def setUpClass(self): print("execute setUpClass1") @classmethod def tearDownClass(self): print("execute tearDownClass1") def setUp(self): print("execute setUp2") def tearDown(self): print("execute tearDown2") def test_one(self): print('execute test_one') self.assertTrue('FiOO'.isupper()) def test_two(self): print('execute test_two') if __name__ == '__main__': suite = unittest.TestSuite() # Test3是要测试的类名,test_one是要执行的测试方法 suite.addTest(Test3("test_one")) suite.addTest(Test3("test_two")) filename = 'result.html' fb = open(filename, 'wb+') runner = HTMLTestRunner(stream=fb, title="测试HTMLTestRunner", description="测试HTMLTestRunner") runner.run(suite) fb.close()
测试报告
code2
import unittest from HTMLTestRunner import HTMLTestRunner class TestDemo1(unittest.TestCase): """测试用例说明""" def test_success1(self): """执行成功""" self.assertEqual(2 + 3, 5) @unittest.skip("skip case") def test_skip2(self): pass def test_fail3(self): self.assertEqual(5, 6) def test_error4(self): self.assertEqual(a, 6) class TestDemo2(unittest.TestCase): def test_success5(self): self.assertEqual(2 + 2, 4) class TestDemo3(unittest.TestCase): def test_fail6(self): self.assertEqual(3, 4) if __name__ == '__main__': suit = unittest.TestSuite() suit.addTest(TestDemo1("test_success1")) suit.addTest(TestDemo1("test_skip2")) suit.addTest(TestDemo1("test_fail3")) suit.addTest(TestDemo1("test_error4")) suit.addTest(TestDemo2("test_success5")) suit.addTest(TestDemo3("test_fail6")) report = "result.html" with(open(report, 'wb+')) as fp: runner = HTMLTestRunner( stream=fp, title='Seldom自动化测试报告', description='单元测试' ) runner.run(suit)
测试报告