Bird
Raised Fist0
PyTesttesting~8 mins

pytest-cov for coverage - Framework Patterns

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using pytest-cov in your testing workflow?
easy
A. To generate test data automatically
B. To speed up test execution time
C. To automatically fix failing tests
D. To measure how much of your code is covered by tests

Solution

  1. Step 1: Understand pytest-cov functionality

    pytest-cov is a plugin that measures code coverage during test runs.
  2. Step 2: Identify the main goal of coverage tools

    Coverage tools show which parts of code are tested and which are not.
  3. Final Answer:

    To measure how much of your code is covered by tests -> Option D
  4. Quick Check:

    pytest-cov = code coverage measurement [OK]
Hint: Coverage means how much code tests run over [OK]
Common Mistakes:
  • Confusing coverage with test speed
  • Thinking pytest-cov fixes tests automatically
  • Assuming pytest-cov generates test data
2. Which command correctly runs pytest with coverage for a package named myapp?
easy
A. pytest -cov myapp
B. pytest --cov=myapp
C. pytest --coverage myapp
D. pytest --cov tests/

Solution

  1. Step 1: Recall correct pytest-cov syntax

    The correct option to enable coverage is --cov=package_name.
  2. Step 2: Compare options

    pytest --cov=myapp uses --cov=myapp, which is the correct syntax. pytest --cov tests/ would incorrectly target the tests directory instead of the myapp package.
  3. Final Answer:

    pytest --cov=myapp -> Option B
  4. Quick Check:

    Use --cov=package [OK]
Hint: Use equal sign with --cov like --cov=package [OK]
Common Mistakes:
  • Targeting tests directory instead of the package
  • Using -cov instead of --cov
  • Using --coverage instead of --cov
3. Given this command run:
pytest --cov=myapp tests/test_module.py
What output will you expect related to coverage?
medium
A. A report showing coverage percentage for myapp package
B. A list of all tests run without coverage info
C. An error saying coverage option is invalid
D. Coverage report for tests directory only

Solution

  1. Step 1: Understand coverage target

    The --cov=myapp option measures coverage for the myapp package, not tests.
  2. Step 2: Predict output

    Running tests in tests/test_module.py with coverage enabled will show coverage report for myapp code exercised by those tests.
  3. Final Answer:

    A report showing coverage percentage for myapp package -> Option A
  4. Quick Check:

    --cov=package shows coverage for that package [OK]
Hint: Coverage reports show tested code, not test files [OK]
Common Mistakes:
  • Expecting coverage for test files instead of source code
  • Thinking coverage option causes errors
  • Confusing coverage report with test list output
4. You ran pytest --cov=myapp but no coverage report appeared. What is the most likely cause?
medium
A. You forgot to install pytest-cov plugin
B. You ran pytest without any test files
C. Your code has no functions to cover
D. You used wrong package name in --cov option

Solution

  1. Step 1: Check plugin installation

    pytest-cov must be installed to enable coverage reporting.
  2. Step 2: Understand symptoms

    If no coverage report appears, the plugin is likely missing or not activated.
  3. Final Answer:

    You forgot to install pytest-cov plugin -> Option A
  4. Quick Check:

    pytest-cov missing = no coverage report [OK]
Hint: Install pytest-cov to get coverage reports [OK]
Common Mistakes:
  • Assuming coverage works without plugin installed
  • Thinking coverage depends on code content
  • Ignoring plugin installation errors
5. You want to generate an HTML coverage report for your package app using pytest-cov. Which command should you run?
hard
A. pytest --coverage=app --report=html
B. pytest --cov=app --html=coverage
C. pytest --cov=app --cov-report=html
D. pytest --cov app --html-report

Solution

  1. Step 1: Recall pytest-cov report syntax

    The option --cov-report=html generates an HTML coverage report.
  2. Step 2: Match correct command format

    pytest --cov=app --cov-report=html correctly uses --cov=app and --cov-report=html together.
  3. Final Answer:

    pytest --cov=app --cov-report=html -> Option C
  4. Quick Check:

    Use --cov-report=html for HTML coverage [OK]
Hint: Use --cov-report=html to get HTML coverage output [OK]
Common Mistakes:
  • Using --html instead of --cov-report=html
  • Omitting equal signs in options
  • Confusing coverage options with test report options
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard