0
0
PyTesttesting~8 mins

pytest-cov setup - Framework Patterns

Choose your learning style9 modes available
Framework Mode - pytest-cov setup
Folder Structure
project-root/
├── tests/
│   ├── test_example.py
│   └── __init__.py
├── src/
│   └── app_code.py
├── .coveragerc
├── pytest.ini
├── requirements.txt
└── README.md
  
Test Framework Layers
  • Test Cases: Located in tests/ folder, contains pytest test functions.
  • Application Code: In src/ folder, the code under test.
  • Configuration: pytest.ini and .coveragerc files configure pytest and coverage behavior.
  • Utilities: Helper functions or fixtures can be added inside tests/ or a separate tests/utils/ folder.
Configuration Patterns

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

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

Use .coveragerc to customize coverage settings, for example:

[run]
branch = True
source = src

[report]
exclude_lines =
    if __name__ == "__main__":
    pragma: no cover
  

Manage environment variables or credentials using pytest fixtures or external files, keeping secrets out of code.

Test Reporting and CI/CD Integration
  • Coverage reports show which code lines are tested and which are missed.
  • Use --cov-report=xml to generate XML reports for CI tools.
  • Integrate with CI/CD pipelines (GitHub Actions, Jenkins) to run tests and collect coverage on every commit.
  • Fail builds if coverage drops below a threshold using --cov-fail-under=80.
Best Practices
  1. Keep tests and source code separate for clarity and maintainability.
  2. Configure coverage to focus on your source code folder only, avoiding test files.
  3. Use coverage exclusion rules to ignore non-testable code like if __name__ == '__main__'.
  4. Run coverage in CI to ensure tests cover new code changes.
  5. Keep configuration files simple and version-controlled for team consistency.
Self Check

Where would you add a new fixture that sets up a test database connection for coverage-instrumented tests?

Key Result
Organize tests in a separate folder, configure pytest.ini and .coveragerc for coverage, and integrate coverage reports into CI pipelines.