Skip to main content

Pytest

In the steps below, we'll start with a Python project that has an existing pytest test suite, and we'll add JUnit XML as an additional output format for the test suite. In each step, we'll show the Git diff for the change that we're making.

1. Configure pytest to use the xunit2 format.

Create a pytest.ini file (or update the existing file if your project already has one) and set the junit_family as shown below.

@@ -0,0 +1,2 @@
+[pytest]
+junit_family=xunit2

If you're using pytest 6.1 or later, xunit2 is already the default. As long as you're not currently overriding the default, you can skip this step for pytest 6.1 or later.

2. Update your CI workflow to output a JUnit XML file describing the test results.

Below we add the --junitxml option when running pytest, and we tell it to write the report to a file named junit.xml at the root of the project.

@@ -23,4 +23,4 @@ jobs:

- name: Test with pytest
run: |
- pipenv run pytest
+ pipenv run pytest --junitxml=junit.xml

This example project uses Github Actions for CI, so we're updating our CI script in the .github/workflows/ directory. If you're using a different CI service, apply this change wherever your CI script is defined (e.g., .circleci/config.yml for CircleCI, etc.).

3. Commit these changes to your repository.

git commit -am "Update CI to generate JUnit XML for test results"

The final result of these changes should resemble commit 830a749 in the buildpulse-example-pytest repository.