Bird
Raised Fist0
PyTesttesting~8 mins

Coverage in CI pipelines in PyTest - Framework Patterns

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Framework Mode - Coverage in CI pipelines
Folder Structure
project-root/
├── tests/
│   ├── test_example.py
│   └── __init__.py
├── src/
│   └── app_code.py
├── .github/
│   └── workflows/
│       └── ci.yml
├── pytest.ini
├── requirements.txt
└── README.md
  
Test Framework Layers
  • Test Layer: Located in tests/ folder, contains pytest test files like test_example.py.
  • Application Code Layer: Source code under src/ folder.
  • Configuration Layer: pytest.ini for pytest settings and coverage options.
  • CI Pipeline Layer: GitHub Actions workflow file .github/workflows/ci.yml to run tests and collect coverage.
  • Utilities: Optional helper scripts or fixtures inside tests/ or separate utils/ folder if needed.
Configuration Patterns
  • pytest.ini: Configure pytest and coverage plugin settings, e.g., [pytest] section with addopts = --cov=src --cov-report=xml to generate coverage XML report.
  • Environment Variables: Use environment variables in CI to control test behavior or credentials securely.
  • CI Workflow: In ci.yml, install dependencies, run pytest with coverage, and upload coverage reports.
  • Coverage Files: Store coverage data files like .coverage and reports in workspace for analysis.
Test Reporting and CI/CD Integration
  • Run tests with coverage collection in CI pipeline (e.g., GitHub Actions).
  • Generate coverage reports in XML or HTML format for easy viewing.
  • Upload coverage reports to coverage services like Coveralls or Codecov for visualization and tracking over time.
  • Fail CI build if coverage drops below a defined threshold using pytest-cov options.
  • Integrate test results and coverage badges in project README for transparency.
Best Practices
  1. Keep tests isolated: Each test should run independently to avoid false coverage results.
  2. Use coverage thresholds: Enforce minimum coverage in CI to maintain code quality.
  3. Generate multiple report formats: XML for CI tools, HTML for local inspection.
  4. Secure secrets: Use encrypted environment variables for credentials in CI.
  5. Automate coverage upload: Integrate with coverage services automatically after tests.
Self Check

Where in this folder structure would you add a new pytest fixture to share setup code across tests?

Key Result
Organize pytest tests and coverage config to run in CI pipelines with automated reporting and quality gates.

Practice

(1/5)
1. What is the main purpose of using coverage tools like pytest --cov in CI pipelines?
easy
A. To generate user documentation
B. To speed up the test execution time
C. To deploy the application automatically
D. To measure how much of the code is tested automatically

Solution

  1. Step 1: Understand coverage tools in testing

    Coverage tools measure the percentage of code executed by tests.
  2. Step 2: Role in CI pipelines

    In CI, coverage helps ensure tests cover enough code to catch bugs early.
  3. Final Answer:

    To measure how much of the code is tested automatically -> Option D
  4. Quick Check:

    Coverage measures tested code = D [OK]
Hint: Coverage shows tested code percentage in CI [OK]
Common Mistakes:
  • Confusing coverage with test speed
  • Thinking coverage deploys code
  • Assuming coverage creates docs
2. Which of the following is the correct command to run pytest with coverage reporting?
easy
A. pytest --cov=.
B. pytest --cov-report=html --cov
C. pytest -coverage
D. pytest --coverage=report

Solution

  1. Step 1: Identify correct pytest coverage syntax

    The correct syntax uses --cov to specify coverage on the current directory.
  2. Step 2: Analyze options

    pytest --cov=. correctly runs coverage on the current directory with default terminal reporting.
  3. Final Answer:

    pytest --cov=. -> Option A
  4. Quick Check:

    Correct pytest coverage command = B [OK]
Hint: Use --cov and --cov-report together for coverage output [OK]
Common Mistakes:
  • Using incorrect flags like --coverage=report
  • Missing --cov option
  • Wrong flag syntax like -coverage
3. Given this pytest command in a CI pipeline:
pytest --cov=myapp --cov-report=term

What will be the output shown in the CI logs?
medium
A. A summary of coverage percentages printed in the terminal
B. No coverage information will be shown
C. A detailed HTML coverage report saved to disk
D. An error because --cov-report=term is invalid

Solution

  1. Step 1: Understand --cov-report=term option

    The term option prints coverage summary in the terminal output.
  2. Step 2: Analyze expected CI log output

    The CI logs will show coverage percentages summary, not an HTML file or error.
  3. Final Answer:

    A summary of coverage percentages printed in the terminal -> Option A
  4. Quick Check:

    --cov-report=term shows summary in terminal = C [OK]
Hint: term report prints coverage summary in console [OK]
Common Mistakes:
  • Expecting HTML report in terminal
  • Assuming no coverage output
  • Thinking --cov-report=term is invalid
4. You added coverage to your CI pipeline with this command:
pytest --cov=myapp --cov-report=html

But the coverage report is missing after the run. What is the most likely cause?
medium
A. The --cov option is misspelled
B. The coverage report is saved in a different directory, not checked by CI
C. pytest does not support coverage reporting
D. The tests did not run because of a syntax error

Solution

  1. Step 1: Understand where coverage HTML reports are saved

    By default, HTML reports are saved in a folder named htmlcov in the current directory.
  2. Step 2: Check CI pipeline file handling

    If the CI pipeline does not collect or upload this folder, the report will appear missing.
  3. Final Answer:

    The coverage report is saved in a different directory, not checked by CI -> Option B
  4. Quick Check:

    HTML report saved in htmlcov folder = A [OK]
Hint: Check htmlcov folder location in CI artifacts [OK]
Common Mistakes:
  • Assuming --cov is misspelled without checking
  • Believing pytest lacks coverage support
  • Ignoring test run errors
5. In a CI pipeline, you want to fail the build if coverage falls below 80%. Which pytest coverage option helps enforce this?
hard
A. --fail-if-coverage-below=80
B. --coverage-threshold=80
C. --cov-fail-under=80
D. --cov-minimum=80

Solution

  1. Step 1: Identify pytest coverage option for minimum coverage

    The correct option is --cov-fail-under which sets a minimum coverage percentage.
  2. Step 2: Understand CI build failure behavior

    If coverage is below the set value, pytest returns a failure status causing CI to fail the build.
  3. Final Answer:

    --cov-fail-under=80 -> Option C
  4. Quick Check:

    Fail build if coverage under 80% = B [OK]
Hint: Use --cov-fail-under to enforce coverage minimum [OK]
Common Mistakes:
  • Using incorrect or non-existent flags
  • Confusing coverage reporting with build failure
  • Assuming coverage threshold is set elsewhere