python使用unittest+HTMLTestRunner生成测试报告包含多个用例
程序员文章站
2024-03-21 22:26:52
...
之前自己在生成的时候只有一个测试套件,下图,能不能多个与TestLogin平级的呢?没想明白
今天看了博主文章,才明白过来自己有多笨
其实就是新增多个类,每个类继承[unittest.TestCase]
import unittest
from utils.HTMLTestRunner_PY3 import HTMLTestRunner
class TestHTMLTestRunnerPY3(unittest.TestCase):
def test_py3_success(self):
self.assertEqual(1+1, 2)
def test_py3_fail(self):
self.assertEqual(1+1, 3)
class TestHTML(unittest.TestCase):
def test_html_success(self):
self.assertEqual(1+2, 3)
class TestError(unittest.TestCase):
def test_error(self):
self.assertEqual(1/0, 1)
if __name__ == '__main__':
import os
report = os.path.join('E:/PY/P2C_test/report/report.html')
st = unittest.TestSuite()
st.addTests([TestHTMLTestRunnerPY3('test_py3_success'), TestHTMLTestRunnerPY3('test_py3_fail'),
TestHTML('test_html_success'), TestError('test_error')])
with open(report, 'wb') as f:
runner = HTMLTestRunner(f, verbosity=2, title='示例测试报告', description='执行人:灰蓝')
runner.run(st)
运行结果: