0
0
PyTesttesting~8 mins

Excluding code from coverage in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Excluding code from coverage
Folder Structure
project-root/
├── src/
│   └── my_module.py
├── tests/
│   ├── test_feature.py
│   └── conftest.py
├── .coveragerc
├── pytest.ini
└── requirements.txt
Test Framework Layers
  • Source Code (src/): Contains the application code to be tested.
  • Tests (tests/): Contains pytest test files and fixtures.
  • Configuration Files: .coveragerc for coverage settings, pytest.ini for pytest options.
  • Utilities: Helpers or fixtures in conftest.py to support tests.
Configuration Patterns

To exclude code from coverage reports in pytest, use the .coveragerc file with coverage.py settings.

Example .coveragerc content:

[run]
omit =
    src/excluded_module.py
    src/*_deprecated.py

[report]
exclude_lines =
    # Exclude lines with this comment
    pragma: no cover
    if __name__ == '__main__':

In your source code, mark lines or blocks to exclude with # pragma: no cover comment.

Example in my_module.py:

def helper_function():
    # This function is only for debugging
    print("Debug info")  # pragma: no cover

Run tests with coverage using:

pytest --cov=src
Test Reporting and CI/CD Integration
  • Use pytest-cov plugin to generate coverage reports.
  • Configure coverage to exclude unwanted code to keep reports clean and meaningful.
  • Integrate coverage reports in CI pipelines (GitHub Actions, Jenkins, GitLab CI) to fail builds if coverage drops below thresholds.
  • Example GitHub Actions snippet to run tests and upload coverage report:
  • name: Python Tests
    
    on: [push, pull_request]
    
    jobs:
      test:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v3
          - name: Set up Python
            uses: actions/setup-python@v4
            with:
              python-version: '3.12'
          - name: Install dependencies
            run: |
              python -m pip install --upgrade pip
              pip install pytest pytest-cov
          - name: Run tests with coverage
            run: pytest --cov=src --cov-report=xml
          - name: Upload coverage to Codecov
            uses: codecov/codecov-action@v3
    
Best Practices
  1. Use # pragma: no cover comments to exclude specific lines or blocks that are not relevant for coverage (e.g., debug prints, platform-specific code).
  2. Configure .coveragerc to omit entire files or folders that should not be counted in coverage (e.g., legacy code, generated files).
  3. Keep coverage reports meaningful by excluding test helpers, mocks, or code that cannot be tested.
  4. Integrate coverage checks in CI to maintain code quality and prevent coverage regression.
  5. Document exclusions clearly so team members understand why some code is excluded.
Self Check

Where in this framework structure would you add a new file to exclude from coverage reporting?

Key Result
Use .coveragerc and pragma comments to exclude code from coverage in pytest frameworks.