Test result publishing in PyTest - Build an Automation Script
Start learning this pattern below
Jump into concepts and practice - no test required
import os import xml.etree.ElementTree as ET import subprocess def test_run_and_publish_report(): report_file = 'test_results.xml' # Run pytest with JUnit XML report generation result = subprocess.run(['pytest', 'test_sample.py', f'--junitxml={report_file}'], capture_output=True, text=True) # Assert pytest ran successfully assert result.returncode == 0, f"Pytest failed: {result.stderr}" # Assert report file exists assert os.path.exists(report_file), f"Report file {report_file} not found" # Parse XML report and check for test case name and status tree = ET.parse(report_file) root = tree.getroot() testcases = root.findall('.//testcase') assert len(testcases) > 0, "No test cases found in report" # Check that at least one test case has a 'name' attribute assert any(tc.attrib.get('name') for tc in testcases), "Test case names missing in report" # Check that no test case has a failure element (means all passed) failures = root.findall('.//failure') assert len(failures) == 0, "Some tests failed according to report"
This test function runs pytest on a sample test file with the --junitxml option to generate a test results report in XML format.
It uses subprocess.run to execute pytest and captures the output and return code to verify pytest ran successfully.
Then it checks if the XML report file was created using os.path.exists.
Next, it parses the XML report using xml.etree.ElementTree to find test case elements and asserts that test cases exist and have names.
Finally, it asserts that there are no failure elements in the report, meaning all tests passed.
This approach ensures the test results are published correctly and can be verified programmatically.
Now add data-driven testing with 3 different test files, each generating its own report file
Practice
Solution
Step 1: Understand test result publishing
Publishing test results means sharing what happened during tests with others.Step 2: Identify the purpose in pytest context
In pytest, publishing results helps teams see which tests passed or failed.Final Answer:
To share test outcomes with team members or tools -> Option DQuick Check:
Test result publishing = sharing outcomes [OK]
- Thinking publishing speeds up tests
- Confusing publishing with writing tests
- Believing publishing changes test code
results.xml?Solution
Step 1: Recall pytest command for XML report
The correct option uses--junitxmlto save results in XML format.Step 2: Match the command with the filename
Usingpytest --junitxml=results.xmlsaves the test report asresults.xml.Final Answer:
pytest --junitxml=results.xml -> Option AQuick Check:
Use --junitxml to save XML report [OK]
- Using --savexml instead of --junitxml
- Using --output or --xmlfile which are invalid
- Forgetting the equals sign '='
pytest --junitxml=report.xml
What will happen after running tests?
Solution
Step 1: Understand the command option
The option--junitxml=report.xmltells pytest to save results in XML format to the filereport.xml.Step 2: Predict the outcome after running tests
Tests run normally and results are saved in the specified XML file.Final Answer:
Test results will be saved in a file named report.xml -> Option AQuick Check:
--junitxml saves results to XML file [OK]
- Thinking results only print on console
- Assuming tests won't run with this option
- Confusing file saving with syntax errors
pytest --junitxml report.xml but no report.xml file is created. What is the likely problem?Solution
Step 1: Check the command syntax
The correct syntax requires an equals sign:--junitxml=report.xml.Step 2: Understand effect of missing equals sign
Without '=', pytest treatsreport.xmlas a positional argument, so no file is created.Final Answer:
The command is missing an equals sign between option and filename -> Option BQuick Check:
Use '=' after --junitxml to save file [OK]
- Omitting the equals sign in command
- Thinking empty tests prevent report creation
- Believing pytest can't create XML reports
Solution
Step 1: Identify correct option for XML report
The option--junitxml=report.xmlcorrectly saves results in XML format.Step 2: Add option to show all test durations
The option--durations=0tells pytest to show durations for all tests.Step 3: Verify combined command correctness
Combining both options aspytest --junitxml=report.xml --durations=0is valid and achieves the goal.Final Answer:
pytest --junitxml=report.xml --durations=0 -> Option CQuick Check:
Use --junitxml and --durations=0 together [OK]
- Using wrong option names like --xmlreport
- Missing equals sign in options
- Using incorrect values like --duration=all
