0
0
PyTesttesting~8 mins

pytest-cov for coverage - Framework Patterns

Choose your learning style9 modes available
Framework Mode - pytest-cov for coverage
Folder Structure
project-root/
├── src/
│   └── app_module.py
├── tests/
│   ├── test_app_module.py
│   └── __init__.py
├── .coveragerc
├── pytest.ini
├── requirements.txt
└── README.md
  
Test Framework Layers
  • Source Code Layer: src/ contains the application code to be tested.
  • Test Layer: tests/ holds test files using pytest functions and classes.
  • Configuration Layer: pytest.ini and .coveragerc configure pytest and coverage behavior.
  • Utilities Layer: Fixtures and helper functions can be added inside tests/ or a tests/utils/ folder.
Configuration Patterns

Use pytest.ini to set pytest options and enable coverage plugin:

[pytest]
addopts = --cov=src --cov-report=term-missing

# Optional: test paths
testpaths = tests
  

Use .coveragerc to customize coverage behavior:

[run]
branch = True
source = src

[report]
show_missing = True
skip_covered = True
  

Manage environments and credentials separately (e.g., environment variables or .env files) to keep tests clean and secure.

Test Reporting and CI/CD Integration
  • Run pytest with coverage to get a terminal report showing which lines are covered and which are missing.
  • Generate HTML coverage reports with --cov-report=html for detailed visual feedback.
  • Integrate coverage checks in CI pipelines (GitHub Actions, Jenkins, GitLab CI) to fail builds if coverage drops below thresholds.
  • Example GitHub Actions snippet:
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 -r requirements.txt
      - name: Run tests with coverage
        run: |
          pytest --cov=src --cov-report=html --cov-fail-under=80
      - name: Upload coverage report
        uses: actions/upload-artifact@v3
        with:
          name: coverage-report
          path: htmlcov/
  
Best Practices
  • Keep coverage focused on your source code folder (e.g., src/) to avoid noise from tests or external libraries.
  • Use --cov-fail-under to enforce minimum coverage thresholds and maintain quality.
  • Combine coverage with branch coverage (branch = True) to check all decision paths.
  • Generate multiple report formats (terminal, HTML, XML) for different audiences and tools.
  • Keep configuration files simple and version-controlled for consistency across environments.
Self Check

Where in this folder structure would you add a new fixture to provide a reusable database connection for tests?

Key Result
Use pytest with pytest-cov plugin to measure and enforce test coverage, configured via pytest.ini and .coveragerc, integrated into CI pipelines for quality control.