unittest-xml-reporting

4.0.0 · active · verified Thu Apr 09

unittest-xml-reporting is a unittest-based test runner that generates XML reports in the xUnit format. These reports are widely compatible with various tools, including build systems, IDEs, and continuous integration servers like Jenkins or GitLab. The library is actively maintained with regular releases; its current major version is 4.0.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a basic unittest suite and run it using `xmlrunner.XMLTestRunner` to generate xUnit-compatible XML reports in a specified output directory.

import unittest
import xmlrunner
import os

class MyTests(unittest.TestCase):
    def test_success(self):
        self.assertTrue(True)

    def test_failure(self):
        self.assertEqual(1, 2, "1 is not equal to 2")

    @unittest.skip("demonstrating skipping")
    def test_skipped(self):
        self.fail("shouldn't run this")

if __name__ == '__main__':
    output_dir = os.environ.get('TEST_OUTPUT_DIR', 'test-reports')
    os.makedirs(output_dir, exist_ok=True)
    
    # Run tests and generate XML report in the specified directory
    unittest.main(
        testRunner=xmlrunner.XMLTestRunner(output=output_dir),
        # Using exit=False allows the script to continue after tests,
        # which can be useful in some CI/CD setups or when integrating.
        exit=False
    )

    print(f"Test reports generated in: {os.path.abspath(output_dir)}")

view raw JSON →