Test Overview
This test runs pytest with coverage enabled and verifies that coverage reports are generated in terminal, HTML, and XML formats.
This test runs pytest with coverage enabled and verifies that coverage reports are generated in terminal, HTML, and XML formats.
import subprocess import os import unittest class TestCoverageReports(unittest.TestCase): def test_coverage_reports_generation(self): # Run pytest with coverage to generate terminal, HTML, and XML reports result = subprocess.run([ 'pytest', '--cov=.', '--cov-report=term', '--cov-report=html', '--cov-report=xml' ], capture_output=True, text=True) # Check pytest run was successful self.assertEqual(result.returncode, 0, f"Pytest failed:\n{result.stderr}") # Check terminal coverage report output contains coverage summary self.assertIn('coverage', result.stdout.lower()) # Check HTML coverage report directory exists self.assertTrue(os.path.isdir('htmlcov'), 'HTML coverage report directory not found') # Check XML coverage report file exists self.assertTrue(os.path.isfile('coverage.xml'), 'XML coverage report file not found') if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner initialized, no tests running | - | PASS |
| 2 | Runs subprocess to execute pytest with coverage options for terminal, HTML, and XML reports | pytest runs tests and collects coverage data | Check subprocess return code is 0 (success) | PASS |
| 3 | Reads subprocess stdout to verify terminal coverage report output contains 'coverage' | Terminal output includes coverage summary | Verify 'coverage' keyword present in output | PASS |
| 4 | Checks if 'htmlcov' directory exists for HTML coverage report | 'htmlcov' directory present with HTML files | Verify 'htmlcov' directory exists | PASS |
| 5 | Checks if 'coverage.xml' file exists for XML coverage report | 'coverage.xml' file present in current directory | Verify 'coverage.xml' file exists | PASS |
| 6 | Test ends successfully | All coverage reports generated and verified | - | PASS |