0
0
PyTesttesting~5 mins

JUnit XML reporting for CI in PyTest

Choose your learning style9 modes available
Introduction

JUnit XML reporting helps share test results in a standard format. Continuous Integration (CI) tools use it to show test outcomes clearly.

You want your test results to be visible in your CI dashboard.
You need to share test results with your team automatically.
You want to track test failures and passes over time in CI.
You want to integrate pytest results with other tools that understand JUnit XML.
You want to generate reports that are easy to read and analyze.
Syntax
PyTest
pytest --junitxml=path/to/report.xml

This command runs pytest and saves results in JUnit XML format.

The file path can be any valid location where you want the report saved.

Examples
Saves the test results in a file named report.xml in the current folder.
PyTest
pytest --junitxml=report.xml
Runs tests inside the tests/ folder and saves the report in results/test-results.xml.
PyTest
pytest tests/ --junitxml=results/test-results.xml
Runs tests with verbose output and saves JUnit XML report.
PyTest
pytest -v --junitxml=report.xml
Sample Program

This simple test file has two tests. Running pytest with --junitxml=report.xml creates a JUnit XML report file named report.xml.

The CI system can read this file to show test results.

PyTest
def test_addition():
    assert 1 + 1 == 2

def test_subtraction():
    assert 5 - 3 == 2

# Run this in terminal:
# pytest --junitxml=report.xml
OutputSuccess
Important Notes

Make sure the folder for the report file exists before running pytest.

CI tools like Jenkins, GitLab, and GitHub Actions can automatically read JUnit XML reports.

You can combine --junitxml with other pytest options like -v for more details.

Summary

JUnit XML reporting creates a standard test result file for CI tools.

Use pytest --junitxml=filename.xml to generate the report.

This helps teams see test results clearly and track test health over time.