0
0
PyTesttesting~10 mins

JUnit XML reporting for CI in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test runs a simple pytest test case and generates a JUnit XML report. It verifies that the test passes and the XML report file is created for CI integration.

Test Code - pytest
PyTest
import pytest
import os
import sys

def test_addition():
    assert 2 + 3 == 5

if __name__ == '__main__':
    # Run pytest with JUnit XML report output
    retcode = pytest.main(['-v', '--junitxml=report.xml'])
    # Check if report.xml file is created
    assert os.path.exists('report.xml')
    # Exit with pytest return code
    sys.exit(retcode)
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test starts by running pytest with the --junitxml option to generate XML reportTerminal shows pytest starting, test_addition discovered-PASS
2Pytest runs test_addition functionTest executes assertion 2 + 3 == 5Assert 2 + 3 == 5 is truePASS
3Pytest finishes test run and writes report.xml filereport.xml file is created in current directoryCheck that report.xml file existsPASS
4Test script asserts report.xml file presenceFile system contains report.xmlos.path.exists('report.xml') returns TruePASS
5Test script exits with pytest return code 0 indicating successProcess ends with exit code 0-PASS
Failure Scenario
Failing Condition: The test_addition assertion fails or report.xml file is not created
Execution Trace Quiz - 3 Questions
Test your understanding
What does the --junitxml option do in pytest?
AGenerates a JUnit XML report file
BRuns tests in parallel
CSkips tests marked as slow
DEnables verbose output
Key Result
Always verify that your CI test runs generate the expected JUnit XML report file to integrate test results with CI dashboards.