pytest-nunit
pytest-nunit is a pytest plugin for generating NUnit3 test result XML output. It integrates seamlessly with `pytest` to produce reports compatible with NUnit test runners. The project is actively maintained with frequent updates, including recent support for `pytest` 8 and ongoing compatibility improvements.
Warnings
- breaking Older versions of `pytest-nunit` (prior to 1.0.7) may not be fully compatible with `pytest` 8.0.0 and newer due to significant internal breaking changes introduced in `pytest`'s collection phase.
- deprecated The `datetime.utcnow()` function, used internally by `pytest-nunit`, was replaced in version 1.0.6 due to deprecation in Python. Running older `pytest-nunit` versions on newer Python environments might surface deprecation warnings related to `datetime` usage.
- gotcha The `xmlschema` package became a required dependency starting from `pytest-nunit` version 1.0.2. Environments that do not automatically resolve transitive dependencies might fail if `xmlschema` is not explicitly installed.
- gotcha The primary method to generate an NUnit XML report is by using the `--nunit-xml=<path>` command-line option. Forgetting this option will result in `pytest` running tests but not producing the desired XML output.
- deprecated The `nunit_suite_name` INI option is deprecated and its value is ignored by `pytest-nunit`.
- gotcha When using `pytest-xdist` with the `-f` (looponfail) option, it's recommended to add `looponfailroots = testdir` to your `pytest.ini` or `setup.cfg`. This prevents the generated XML report files from being continuously watched for changes, which can lead to unexpected behavior or performance issues.
Install
-
pip install pytest-nunit
Quickstart
import pytest
# Create a dummy test file, e.g., test_example.py
with open('test_example.py', 'w') as f:
f.write('def test_success():\n assert True\n\ndef test_failure():\n assert False\n')
# Run pytest with nunitxml output
# This will generate a 'test-results.xml' file in the current directory
pytest.main(['test_example.py', '--nunit-xml=test-results.xml'])
# Clean up (optional)
import os
os.remove('test_example.py')
os.remove('test-results.xml')