How to generate python unittesting report in HTML format.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With