0
0
PyTesttesting~15 mins

Excluding code from coverage in PyTest - Build an Automation Script

Choose your learning style9 modes available
Verify code exclusion from coverage report using pytest
Preconditions (2)
Step 1: Run pytest with coverage enabled on the module
Step 2: Check the coverage report generated
Step 3: Verify that the lines marked for exclusion are not counted in coverage
✅ Expected Result: Coverage report shows 100% coverage excluding the marked lines
Automation Requirements - pytest with coverage.py
Assertions Needed:
Coverage report excludes lines marked with # pragma: no cover
Coverage percentage matches expected value excluding excluded code
Best Practices:
Use # pragma: no cover comment to exclude lines
Run coverage with pytest-cov plugin
Generate coverage report in XML or terminal format for verification
Automated Solution
PyTest
import subprocess
import xml.etree.ElementTree as ET

def test_coverage_exclusion():
    # Run pytest with coverage and generate XML report
    result = subprocess.run([
        'pytest', '--cov=sample_module', '--cov-report=xml', '--maxfail=1', '--disable-warnings'
    ], capture_output=True, text=True)
    assert result.returncode == 0, f"Pytest failed:\n{result.stdout}\n{result.stderr}"

    # Parse coverage XML report
    tree = ET.parse('coverage.xml')
    root = tree.getroot()

    # Find coverage line-rate attribute
    line_rate = float(root.attrib.get('line-rate', '0'))

    # Assert coverage is 1.0 (100%) excluding excluded lines
    assert line_rate == 1.0, f"Expected 100% coverage excluding excluded lines, got {line_rate*100:.2f}%"

This test runs pytest with coverage enabled on a sample module that has some lines marked with # pragma: no cover to exclude them from coverage.

It generates an XML coverage report, then parses it to check the overall line coverage rate.

The assertion verifies that the coverage is 100%, meaning the excluded lines are not counted against coverage.

Using subprocess.run allows running pytest as a separate process and capturing output and exit code.

This approach ensures the coverage exclusion comment works as expected.

Common Mistakes - 3 Pitfalls
{'mistake': "Not using the '# pragma: no cover' comment to exclude code", 'why_bad': "Coverage tools won't know which lines to exclude, so coverage will be lower than expected.", 'correct_approach': "Add '# pragma: no cover' comment on lines or blocks to exclude them from coverage."}
{'mistake': 'Running pytest without coverage plugin or options', 'why_bad': 'No coverage data will be collected, so exclusion cannot be verified.', 'correct_approach': "Run pytest with '--cov' and '--cov-report' options to collect and generate coverage reports."}
Parsing coverage report incorrectly or not checking coverage percentage
Bonus Challenge

Now add a test that verifies exclusion of multiple code blocks marked with '# pragma: no cover' in different files

Show Hint