目前有多个库可以将测试结果保存为html格式。通过PyPi查询HTMLTestRunner,查询到6个项目,这里取其中2个进行对比:HTMLTestRunner
html-testRunner
1 HTMLTestRunner
当前版本0.8.0,只支持Python 2与英文,大部分教程介绍的都是这个工具。由于年代久远,目前有基于这个版本进行修改的库,见Python3,支持中文版本。
以下介绍基于修改后的版本。
1) 在以上链接中下载HTMLTestRunner_PY3.py文件并放到对应‘...\Lib’文件夹。
2) 编写代码如下:
import unittest
from HTMLTestRunner_PY3 import HTMLTestRunner
class TestStringMethods(unittest.TestCase):
def test_upper(self):
""" This test should be passed. """
self.assertEqual('foo'.upper(), 'FOO')
def test_error(self):
""" This test should be marked as error one. """
raise ValueError
def test_fail(self):
""" This test should fail. """
self.assertEqual(1, 2)
@unittest.skip("This is a skipped test.")
def test_skip(self):
""" This test should be skipped. """
pass
if __name__ == '__main__':
# 报告名称
report_title = 'Example用例执行报告'
# 报告描述
desc = '用于展示修改样式后的HTMLTestRunner'
# 报告地址与文件名
report_file = r'E:\Python_study\ExampleReport.html'
testsuite = unittest.TestSuite()
testsuite.addTest(TestStringMethods('test_upper'))
testsuite.addTest(TestStringMethods('test_error'))
testsuite.addTest(TestStringMethods('test_fail'))
testsuite.addTest(TestStringMethods('test_skip'))
with open(report_file, 'wb') as report:
runner = HTMLTestRunner(stream=report, title=report_title, description=desc)
runner.run(testsuite)
注意,以上代码需要采用Python运行,不能采用unittest运行。
以上库有以下需要改进:只支持中文;
不支持skip统计。
2 html-testRunner
安装:
pip install html-testRunner
当前版本1.1.2,支持Python3,但是只有英文。格式与上面差异不大,代码如下:
import unittest
import HtmlTestRunner
class TestStringMethods(unittest.TestCase):
def test_upper(self):
""" This test should be passed. """
self.assertEqual('foo'.upper(), 'FOO')
def test_error(self):
""" This test should be marked as error one. """
raise ValueError
def test_fail(self):
""" This test should fail. """
self.assertEqual(1, 2)
@unittest.skip("This is a skipped test.")
def test_skip(self):
""" This test should be skipped. """
pass
if __name__ == '__main__':
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output=r'E:\Python_study'))
注意,以上代码需要采用Python运行,不能采用unittest运行。
以上库有以下需要改进:不支持中文;
不指定地址和文件名(没找到方法?)。
错误结果有记录但是不能显示。
参考: