0
0
PyTesttesting~10 mins

Coverage report formats (terminal, HTML, XML) in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test runs pytest with coverage enabled and verifies that coverage reports are generated in terminal, HTML, and XML formats.

Test Code - unittest
PyTest
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()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsTest runner initialized, no tests running-PASS
2Runs subprocess to execute pytest with coverage options for terminal, HTML, and XML reportspytest runs tests and collects coverage dataCheck subprocess return code is 0 (success)PASS
3Reads subprocess stdout to verify terminal coverage report output contains 'coverage'Terminal output includes coverage summaryVerify 'coverage' keyword present in outputPASS
4Checks if 'htmlcov' directory exists for HTML coverage report'htmlcov' directory present with HTML filesVerify 'htmlcov' directory existsPASS
5Checks if 'coverage.xml' file exists for XML coverage report'coverage.xml' file present in current directoryVerify 'coverage.xml' file existsPASS
6Test ends successfullyAll coverage reports generated and verified-PASS
Failure Scenario
Failing Condition: Coverage reports are not generated or pytest run fails
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test check after running pytest with coverage options?
AThat only terminal coverage report is generated
BThat tests fail if coverage is below 100%
CThat coverage reports are generated in terminal, HTML, and XML formats
DThat coverage reports are saved only in XML format
Key Result
Always verify that coverage reports are generated in all requested formats by checking both the output and the presence of report files or directories.