Coverage in CI pipelines helps check how much of your code is tested automatically. It shows if important parts are missing tests.
Coverage in CI pipelines in PyTest
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
PyTest
pytest --cov=your_package tests/
This command runs tests and measures coverage for the specified package.
Use --cov-report=term to see coverage summary in the terminal.
Examples
tests/ folder and measures coverage for the myapp package.PyTest
pytest --cov=myapp tests/
PyTest
pytest --cov=myapp --cov-report=term-missing tests/
PyTest
pytest --cov=myapp --cov-report=xml tests/
Sample Program
This simple test checks only the add function. The subtract function is not tested, so coverage will show it as missing.
PyTest
def add(a, b): return a + b def subtract(a, b): return a - b def test_add(): assert add(2, 3) == 5 # Note: No test for subtract function # Run with: # pytest --cov=.
Important Notes
Always generate coverage reports in formats your CI tool supports, like XML or HTML.
Coverage shows what code runs during tests, but not if tests are correct.
High coverage is good, but focus also on meaningful tests.
Summary
Coverage in CI helps track how much code is tested automatically.
Use pytest --cov commands to measure and report coverage.
Integrate coverage reports in CI pipelines to catch missing tests early.
Practice
1. What is the main purpose of using coverage tools like
pytest --cov in CI pipelines?easy
Solution
Step 1: Understand coverage tools in testing
Coverage tools measure the percentage of code executed by tests.Step 2: Role in CI pipelines
In CI, coverage helps ensure tests cover enough code to catch bugs early.Final Answer:
To measure how much of the code is tested automatically -> Option DQuick 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
Solution
Step 1: Identify correct pytest coverage syntax
The correct syntax uses--covto specify coverage on the current directory.Step 2: Analyze options
pytest --cov=. correctly runs coverage on the current directory with default terminal reporting.Final Answer:
pytest --cov=. -> Option AQuick 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:
What will be the output shown in the CI logs?
pytest --cov=myapp --cov-report=term
What will be the output shown in the CI logs?
medium
Solution
Step 1: Understand --cov-report=term option
Thetermoption prints coverage summary in the terminal output.Step 2: Analyze expected CI log output
The CI logs will show coverage percentages summary, not an HTML file or error.Final Answer:
A summary of coverage percentages printed in the terminal -> Option AQuick 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:
But the coverage report is missing after the run. What is the most likely cause?
pytest --cov=myapp --cov-report=html
But the coverage report is missing after the run. What is the most likely cause?
medium
Solution
Step 1: Understand where coverage HTML reports are saved
By default, HTML reports are saved in a folder namedhtmlcovin the current directory.Step 2: Check CI pipeline file handling
If the CI pipeline does not collect or upload this folder, the report will appear missing.Final Answer:
The coverage report is saved in a different directory, not checked by CI -> Option BQuick 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
Solution
Step 1: Identify pytest coverage option for minimum coverage
The correct option is--cov-fail-underwhich sets a minimum coverage percentage.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.Final Answer:
--cov-fail-under=80 -> Option CQuick 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
