Bird
Raised Fist0
PyTesttesting~10 mins

Coverage in CI pipelines in PyTest - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to run pytest with coverage reporting.

PyTest
pytest --cov=[1] tests/
Drag options to blanks, or click blank then click option'
Amy_module
Btests
Csetup.py
Ddocs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'tests' as the coverage target instead of the main module.
2fill in blank
medium

Complete the pytest command to generate a coverage report in XML format for CI tools.

PyTest
pytest --cov=my_module --cov-report=[1] tests/
Drag options to blanks, or click blank then click option'
Aterm
Bhtml
Cxml
Dannotate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'html' instead of 'xml' for CI coverage reports.
3fill in blank
hard

Fix the error in the pytest coverage command to include branch coverage.

PyTest
pytest --cov=my_module --cov-report=xml --cov-branch=[1] tests/
Drag options to blanks, or click blank then click option'
Atrue
Byes
C1
Don
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'yes' or 'on' which are not recognized by pytest-cov.
4fill in blank
hard

Fill both blanks to configure pytest coverage to fail if coverage is below 80% and to show the report in the terminal.

PyTest
pytest --cov=my_module --cov-fail-under=[1] --cov-report=[2] tests/
Drag options to blanks, or click blank then click option'
A80
Bterm
Chtml
D70
Attempts:
3 left
💡 Hint
Common Mistakes
Setting fail-under too low or using 'html' instead of 'term' for terminal output.
5fill in blank
hard

Fill all four blanks to create a pytest coverage command that measures coverage, generates XML and HTML reports, and fails if coverage is below 90%.

PyTest
pytest --cov=[1] --cov-report=[2] --cov-report=[3] --cov-fail-under=[4] tests/
Drag options to blanks, or click blank then click option'
Amy_module
Bxml
Chtml
D90
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to specify the module or mixing up report formats.

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