unittest

unittest is a testing framework for Python.

Enabling XML Output

Though unittest is a part of the standard Python library, it does not support JUnit XML output. However pytest, another Python unit testing framework, supports running unittest tests out of the box.

pytest can produce output by running with the --junit-xml= option.

pytest --junit-xml=filepath.xml 

Test Suite Naming

pytest will automatically fill in values for the and name and classname attributes.

def test_95_percent():
    random_number = secrets.randbelow(100)
    assert random_number <= 95

would produce output that looks like this:

<testsuites>
    <testsuite name="pytest">
        <testcase classname="random_test" 
                  name="test_95_percent"
                  file="python/pytest/random_test.py"
                  />
     ...
</testsuites>

The suite name can be configured in the pytest.ini or similar config file.

[pytest]
junit_suite_name = my_suite

Configuring other XML output values is not currently supported, but experimental options are available. To include the test filenames in the XML output, use the -o junit_family=xunit1 option.

pytest --junitxml=output/path.xml -o junit_family=xunit1

By default, pytest will include the file attribute in the output XML.

Further Information

See an example of running pytest in a GitHub action here.

Last updated