12

How to generate python unittesting report in HTML format.

passionTime
  • 989
  • 6
  • 14
  • 27

6 Answers6

9

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.

bstpierre
  • 30,042
  • 15
  • 70
  • 103
passionTime
  • 989
  • 6
  • 14
  • 27
  • 3
    In this code, your tests would be run **2 times**. One time they will be started by `unittest.TextTestRunner::run` and the second time by `HTMLTestRunner::run`. You should remove line `unittest.TextTestRunner(verbosity=2).run(suite)`. – Jury Aug 13 '15 at 07:01
  • for HTMLTestRunner is there a way to run the unit test and generate the file with python -m unittest? I have to run the script with python unit_test.py which I don't want to do as I am making the reusable CI/CD pipeline across all the repo we have – urvish patel Feb 22 '23 at 16:30
3

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.

melqkiades
  • 445
  • 6
  • 7
  • This doesn't really address the core question - it answers the question using an entirely different solution.. – rh0dium Jul 30 '14 at 00:33
  • The solution generates a python unittesting report in HTML format. I don't get how it is not relvevant to the question – melqkiades Jul 30 '14 at 09:05
  • @melqkiades Do we have any option to change the name of the report, which is generated. If I have multiple nosetests to run, then each time `result.html` will be created. So finally i will have only one `result.html`. But, I want same number of html files as of nosetests which I run.. How can I do that ? – Karthik Apr 26 '16 at 04:18
  • 1
    @Karthik you can use the `--html-out-file` option to specify the file name. For instance, try running `nosetests --with-html-out --html-out-file=my_results_file_name.html`. Hope this helps. – melqkiades Apr 27 '16 at 10:25
  • I will try it.. Meanwhile, I created a bash script to run all the files and create report for each.. `#!/bin/bash` `for f in *.py` `do ` `echo "$f"` `nosetests "$f" --with-html --html-file="$f".html ` `done` This works.. – Karthik Apr 27 '16 at 10:37
  • When I have failed tests the option --with-xunitdo not generate a report? do you know why? – rota90 Mar 30 '20 at 14:16
3

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

Martin Gergov
  • 1,556
  • 4
  • 20
  • 29
aknuds1
  • 65,625
  • 67
  • 195
  • 317
  • some level I got the solution python unit testing Nose Html report generation. Up to end of the day I think I will get the solution. thanks for All your support.... – passionTime Feb 28 '11 at 11:10
  • 2
    @prasanna.yelsangika: "thanks" [is best expressed as a green tick to the left of the answer that helped you solve your problem](http://stackoverflow.com/faq#howtoask). – johnsyweb Feb 28 '11 at 11:33
  • answer for my question is goto link http://testoob.sourceforge.net/features.html, In this link i generated python unit test report in XML / HTML format using testoob tool, but I generated XML format, but I am facing HTML once I got this answer I will post...... – passionTime Mar 03 '11 at 06:22
  • @prasanna.yelsangika: Then please post an answer to your own question here, and mark it as accepted. – aknuds1 Mar 03 '11 at 06:25
  • Python Unit testing Report can easily Generated in 2 ways 1. Testoob : In command prompt python Python_File_name --xml/html= file_Path 2.HTMLTestRunner : include HTMLTestRunner.py and add these lines code: outfile = open("C:\Report.html", "w") runner = HTMLTestRunner.HTMLTestRunner( stream=outfile, title='Test Report', description='This demonstrates the report output by HTMLTestRunner.') runner.run(testshuffle) – passionTime Mar 14 '11 at 07:38
3

I like to use pytest-html since pytest is compatible with 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
brada
  • 429
  • 5
  • 8
1

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
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • only pass, fail,skip and error report in HTML format, not code coverage report – passionTime Feb 28 '11 at 05:18
  • I want in this format, Please goto this link, http://tungwaiyip.info/software/sample_test_report.html – passionTime Feb 28 '11 at 05:25
  • 3
    Perhaps you want to look carefully first yourself and see that this report is obviously generated by http://tungwaiyip.info/software/HTMLTestRunner.html –  Feb 28 '11 at 05:31
  • ok fine, Is there any other way to generate the report in above HTML format. – passionTime Feb 28 '11 at 05:40
  • please, If you have any idea give me the links, I will go through it. – passionTime Feb 28 '11 at 05:51
  • 1
    @prasanna.yelsangika: I have an idea. If you want to produce a report exactly like the one produced by [HTMLTestRunner](http://tungwaiyip.info/software/HTMLTestRunner.html), try a product called [HTMLTestRunner](http://tungwaiyip.info/software/HTMLTestRunner.html) (as @pynator suggested a couple of comments back)! Hope this helps. Have a nice day. – johnsyweb Feb 28 '11 at 06:09
  • 1
    @prasanna.yelsangika: I gave you the link to the project page of HTMLTestRunner. Reading *appreciated* - please open your eyes! –  Feb 28 '11 at 06:11
1

Install 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()