Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Unittest Reporting in HTML

How to generate python unittesting report in HTML format.

like image 833
passionTime Avatar asked Feb 28 '11 04:02

passionTime


2 Answers

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.

like image 66
passionTime Avatar answered Oct 05 '22 18:10

passionTime


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
like image 30
brada Avatar answered Oct 05 '22 19:10

brada