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.