0
0
PyTesttesting~5 mins

Test result publishing in PyTest

Choose your learning style9 modes available
Introduction

Test result publishing helps you share the outcome of your tests with others. It shows if your code works or has problems.

After running tests to see which tests passed or failed.
When you want to share test results with your team.
To keep a record of test outcomes for future reference.
When integrating tests into automated pipelines.
To quickly find errors in your code by reviewing test reports.
Syntax
PyTest
pytest --junitxml=results.xml

This command runs tests and saves results in an XML file.

The XML file can be used by other tools to show test reports.

Examples
Run all tests and save results in 'results.xml'.
PyTest
pytest --junitxml=results.xml
Run tests in a specific file and save results in 'output.xml'.
PyTest
pytest tests/test_example.py --junitxml=output.xml
Run tests with detailed output and save results.
PyTest
pytest --junitxml=results.xml -v
Sample Program

This script defines three tests for the add function. One test is designed to fail. Running the script will execute tests and save the results in 'test_results.xml'.

PyTest
import pytest

def add(a, b):
    return a + b

def test_add_positive():
    assert add(2, 3) == 5

def test_add_negative():
    assert add(-1, -1) == -2

def test_add_fail():
    assert add(2, 2) == 5  # This test will fail

if __name__ == '__main__':
    # Run pytest and save results to 'test_results.xml'
    pytest.main(['-v', '--junitxml=test_results.xml'])
OutputSuccess
Important Notes

Test result publishing helps track test outcomes outside the console.

JUnit XML format is widely supported by many CI/CD tools.

Make sure to run tests with the --junitxml option to generate reports.

Summary

Test result publishing shares test outcomes with others.

Use pytest --junitxml=filename.xml to save results.

Reports help find errors and improve code quality.