0
0
PyTesttesting~8 mins

Coverage thresholds in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Coverage thresholds
Folder Structure
tests/
├── test_example.py
├── test_utils.py
conftest.py
pytest.ini
requirements.txt

# Coverage reports output folder
coverage_reports/
Test Framework Layers
  • Tests: Located in tests/ folder, contains test scripts like test_example.py.
  • Fixtures & Setup: conftest.py holds shared setup code and fixtures for tests.
  • Configuration: pytest.ini configures pytest and coverage plugin settings including coverage thresholds.
  • Utilities: Helper functions or modules can be added as needed, imported by tests.
  • Coverage Reports: Generated reports stored in coverage_reports/ folder for review.
Configuration Patterns

Use pytest.ini to configure coverage thresholds and reporting:

[pytest]
addopts = --cov=src --cov-report=term-missing --cov-fail-under=80

# Explanation:
# --cov=src : measure coverage for source code in src folder
# --cov-report=term-missing : show missing lines in terminal
# --cov-fail-under=80 : fail tests if coverage is below 80%

Adjust --cov-fail-under value to set your minimum coverage threshold.

For multiple environments or browsers, use environment variables or separate config files and load them in conftest.py.

Test Reporting and CI/CD Integration
  • Coverage reports are generated automatically during test runs.
  • Use --cov-report=xml option to create XML reports for CI tools.
  • Integrate with CI/CD pipelines (GitHub Actions, Jenkins, GitLab CI) to run tests and enforce coverage thresholds.
  • If coverage is below threshold, CI job fails, preventing merge/deployment.
  • Store coverage reports as artifacts or publish them to dashboards for team visibility.
Best Practices
  1. Set realistic coverage thresholds: Start with a reasonable percentage (e.g., 70-80%) and increase gradually.
  2. Fail fast on low coverage: Use --cov-fail-under to prevent low-quality code from merging.
  3. Use clear reports: Enable missing line reports to help developers find uncovered code easily.
  4. Separate config from code: Keep coverage settings in pytest.ini or environment variables for easy updates.
  5. Integrate with CI/CD: Automate coverage checks to maintain code quality continuously.
Self Check

Where in this folder structure would you add a new coverage threshold setting for a different environment?

Key Result
Use pytest.ini to set coverage thresholds that fail tests if coverage is below a set percentage, integrated with CI for quality enforcement.