[英]Python Unittest Reporting in HTML
如何生成 HTML 格式的 Python 单元测试报告。
Again Back with Answer...... Report can generate Using HTMLTestRunner like ex:再次返回答案......报告可以使用 HTMLTestRunner 生成,例如:
import random
import unittest
import HTMLTestRunner
class TestSequenceFunctions(unittest.TestCase):
def setUp(self):
self.seq = range(10)
def test_shuffle(self):
# make sure the shuffled sequence does not lose any elements
random.shuffle(self.seq)
self.seq.sort()
self.assertEqual(self.seq, range(10))
# should raise an exception for an immutable sequence
self.assertRaises(TypeError, random.shuffle, (1,2,3))
@unittest.skip("Test Skipped1")
def test_choicep(self):
element = random.choice(self.seq)
self.assertTrue(element in self.seq)
@unittest.skip("Test Skipped2")
def test_samplep(self):
with self.assertRaises(ValueError):
random.sample(self.seq, 20)
for element in random.sample(self.seq, 5):
self.assertTrue(element in self.seq)
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
unittest.TextTestRunner(verbosity=2).run(suite)
outfile = open("C:\Report.html", "w")
runner = HTMLTestRunner.HTMLTestRunner(
stream=outfile,
title='Test Report',
description='This demonstrates the report output by Prasanna.Yelsangikar.'
)
runner.run(suite)
Get the result In HTML Format in C:\Report.html for skipping need to customize in HTMLTestRunner.py file.在 C:\Report.html 中获取 HTML 格式的结果,以便跳过需要在 HTMLTestRunner.py 文件中自定义。
I have used nose with the nose-html-output plugin and works like a charm.我已经将鼻子与鼻子-html-输出插件一起使用,并且像一个魅力一样工作。
To install nose just type pip install nose
要安装鼻子,只需键入
pip install nose
Then install the nose-html plugin typing python setup.py install
然后安装nose-html插件输入
python setup.py install
Finally run the unit tests by typing nosetests --with-html-out
, a report with the results of the unit tests will be stored in a file called results.html
.最后通过输入
nosetests --with-html-out
运行单元测试,包含单元测试结果的报告将存储在名为results.html
的文件中。
我不了解 HTML,但Nose可以通过--with-xunit
选项生成 XUnit XML 报告。
I like to use pytest-html
since pytest
is compatible with unittest
.我喜欢使用
pytest-html
因为pytest
与unittest
兼容。 If you already have a suite defined, you just run this:如果您已经定义了一个套件,您只需运行以下命令:
pip install pytest pytest-html
pytest -v suite.py --html=pytest_report.html --self-contained-html
Install HTMLTestRunner-rv安装 HTMLTestRunner-rv
pip install HTMLTestRunner-rv
import unittest
from test_print_all_details import TestCasePrintAllDetails
from test_by_id import TestCaseDetailsById
from test_details_by_name import TestCaseDetailsByNmae
from HTMLTestRunner import HTMLTestRunner
def test_suite():
test1 = unittest.TestLoader().loadTestsFromTestCase(TestCasePrintAllDetails)
test2 = unittest.TestLoader().loadTestsFromTestCase(TestCaseDetailsById)
test3 = unittest.TestLoader().loadTestsFromTestCase(TestCaseDetailsByNmae)
suite = unittest.TestSuite([test1,test2,test3])
runner = HTMLTestRunner(log=True, verbosity=2, output='report', title='Test report', report_name='report',
open_in_browser=True, description="HTMLTestReport")
runner.run(suite)
if __name__ == '__main__':
test_suite()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.