Python Unittest Reporting in HTML

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

How to generate python unittesting report in HTML format.

回答1:

Again Back with Answer...... Report can generate Using HTMLTestRunner like ex:

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.



回答2:

What kind of report? I assumed you meant coverage, since if you have more than one failing unit test You Are Doing It Wrong.

Look at Nose.

nosetests --with-coverage --cover-html 


回答3:

I don't know about HTML, but Nose can generate XUnit XML reports, through the --with-xunit option.



回答4:

I have used nose with the nose-html-output plugin and works like a charm.

To install nose just type pip install nose

Then install the nose-html plugin typing 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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!