unittest-xml-reporting

repository·master·Indexed 43 days ago

https://github.com/xmlrunner/unittest-xml-reporting

A test runner for Python's unittest framework that generates test results in xUnit-compatible XML format for consumption by CI/CD tools like Jenkins and various IDEs. It provides the XMLTestRunner class for Python scripts and a command-line interface via the xmlrunner module, including specialized support for Django projects and doctest suites.

Tokens
1.5K
Snippets
7
Records
9
Agent score
5%

What's inside xmlrunner

  1. Compatibility with Jenkins plugins

    master

    The generated XML files are designed for consumption by Jenkins plugins:

    • Jenkins JUnit plugin: Parses the XML without XSD validation; should work without issues.
    • Jenkins xUnit plugin (version 1.100): Uses a lax XSD and performs validation; should work without issues.
    • Jenkins xUnit plugin (version 1.104+): Uses a stricter XSD. If you encounter validation issues, you may need to use the transform utility mentioned in the Transform XML reports section.
  2. Limitations of `unittest.TestCase.subTest` support

    master

    The unittest concept of subTest does not map directly to the standard xUnit schema. As a result:

    1. Loss of granularity: You will not find sub-test specific details in the generated XML reports.
    2. Inexact accounting: Since unittest does not report successful sub-tests, the total counts in the report may not be perfectly exact.
  3. Install and run unittest-xml-reporting via CLI

    master

    You can use unittest-xml-reporting (also known as xmlrunner) as a drop-in replacement for the standard unittest module to save test results to XML (JUnit) files. This allows CI/CD tools to consume your test results.

    Run tests using the following command patterns:

    • Run a specific module: python -m xmlrunner test_module
    • Run a specific test class: python -m xmlrunner module.TestClass
    • Run a specific test method: python -m xmlrunner module.Class.test_method
    • Discover and run all tests: python -m xmlrunner discover [options]
    python -m xmlrunner test_module
    python -m xmlrunner module.TestClass
    python -m xmlrunner module.Class.test_method
    python -m xmlrunner discover [options]
  4. Configure XMLTestRunner in a Python script

    master

    To integrate the XML reporter directly into your test scripts, use xmlrunner.XMLTestRunner as the testRunner argument in unittest.main().

    You can specify the output directory using the output parameter. To ensure that certain unittest options (like failfast, buffer, or catchbreak) do not appear in the help menu when they are not applicable to the XML runner, you should explicitly set them to False or their desired values.

    import unittest
    import xmlrunner
    
    if __name__ == '__main__':
        unittest.main(
            testRunner=xmlrunner.XMLTestRunner(output='test-reports'),
            failfast=False, 
            buffer=False, 
            catchbreak=False
        )
  5. Configure unittest-xml-reporting for Django

    master

    To integrate xmlrunner into a Django project, update your settings.py to use the specialized Django test runner:

    TEST_RUNNER = 'xmlrunner.extra.djangotestrunner.XMLTestRunner'

    Django Configuration Options

    Use the following settings in settings.py to fine-tune report generation:

    SettingDefaultValuesDescription
    TEST_OUTPUT_VERBOSE10, 1, 2Controls verbosity of information printed to sys.stderr.
    TEST_OUTPUT_DESCRIPTIONSFalseTrue, FalseIf True, displays test docstrings instead of method names (requires TEST_OUTPUT_VERBOSE = 2).
    TEST_OUTPUT_DIR.<str>Directory where XML reports are stored. Created if it doesn't exist.
    TEST_OUTPUT_FILE_NAMENone<str>If set, outputs a single XML report with this filename in TEST_OUTPUT_DIR. Note: This keeps results in memory longer.
  6. Transform XML reports for Jenkins xUnit plugin

    master

    If you are using the Jenkins xUnit plugin and need to remove extra attributes to ensure compatibility, use the transform function from xmlrunner.extra.xunit_plugin.

    from xmlrunner.extra.xunit_plugin import transform
    
    # Assuming 'out' is a BytesIO object containing the raw XML output
    with open('TEST-report.xml', 'wb') as report:
        report.write(transform(out.getvalue()))
    from xmlrunner.extra.xunit_plugin import transform
    
    with open('TEST-report.xml', 'wb') as report:
        report.write(transform(out.getvalue()))
  7. Use XMLTestRunner in Python scripts

    master

    To use XMLTestRunner in your own Python test scripts, pass an instance of xmlrunner.XMLTestRunner to the testRunner argument of unittest.main().

    Basic Usage (Output to directory)

    By default, providing a string to output creates a directory for the reports.

    import unittest
    import xmlrunner
    
    if __name__ == '__main__':
        unittest.main(
            testRunner=xmlrunner.XMLTestRunner(output='test-reports'),
            failfast=False, buffer=False, catchbreak=False)

    Reporting to a single file

    To consolidate results into one specific file, pass a file object to the output parameter.

    import unittest
    import xmlrunner
    
    if __name__ == '__main__':
        with open('/path/to/results.xml', 'wb') as output:
            unittest.main(
                testRunner=xmlrunner.XMLTestRunner(output=output),
                failfast=False, buffer=False, catchbreak=False)

    Running Doctests

    XMLTestRunner can also be used to report on doctest suites.

    import doctest
    import xmlrunner
    
    # ... (doctest definitions) ...
    
    if __name__ == '__main__':
        suite = doctest.DocTestSuite()
        xmlrunner.XMLTestRunner().run(suite)
  8. Use unittest-xml-reporting via Command Line

    master

    You can run tests and generate XML reports directly from the command line using the xmlrunner module.

    Use python -m xmlrunner to run tests in the current directory, or python -m xmlrunner discover to discover tests in a specific directory.

    Example: Run discovery on a specific test directory and save reports to a specific output folder:

    python -m xmlrunner discover -t ~/mycode/tests -o /tmp/build/junit-reports