Bird
Raised Fist0
PyTesttesting~20 mins

pytest-cov setup - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
pytest-cov Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Purpose of pytest-cov in testing

What is the main purpose of using pytest-cov in a Python testing environment?

ATo measure how much of the code is executed during tests
BTo automatically fix bugs found during tests
CTo speed up test execution by parallelizing tests
DTo generate test data for parameterized tests
Attempts:
2 left
💡 Hint

Think about what 'cov' in pytest-cov might stand for.

Predict Output
intermediate
1:30remaining
Output of pytest-cov command

What output will you see after running this command?

pytest --cov=my_module tests/

Assume my_module has 100 lines, and tests cover 80 lines.

AA list of all tests run without coverage information
BAn error saying <code>--cov</code> is an unknown option
CA coverage report showing 80% coverage of <code>my_module</code>
DA coverage report showing 100% coverage of <code>my_module</code>
Attempts:
2 left
💡 Hint

Think about what coverage means and what the command requests.

locator
advanced
2:00remaining
Correct pytest-cov configuration in pytest.ini

Which pytest.ini configuration correctly enables coverage reporting for the app package and generates an XML report?

A
[pytest]
addopts = --cov app --cov-report xml
B
[pytest]
addopts = --cov=app --cov-report=xml
C
[pytest]
addopts = --coverage=app --coverage-report=xml
D
[pytest]
coverage = app
coverage_report = xml
Attempts:
2 left
💡 Hint

Check the exact option names for pytest-cov.

assertion
advanced
1:30remaining
Valid assertion for coverage percentage in pytest-cov report

Which assertion correctly checks that the coverage percentage is at least 90% in a pytest-cov generated report stored as a variable coverage_percent?

Aassert coverage_percent < 90
Bassert coverage_percent > 90
Cassert coverage_percent == 90
Dassert coverage_percent >= 90
Attempts:
2 left
💡 Hint

Think about including 90% as acceptable coverage.

🔧 Debug
expert
2:00remaining
Identify the cause of missing coverage report

You ran pytest --cov=myapp tests/ but no coverage report is generated. Which is the most likely cause?

AThe <code>pytest-cov</code> plugin is not installed
BThe tests folder is empty
CThe <code>myapp</code> folder has no Python files
DThe <code>pytest.ini</code> file is missing
Attempts:
2 left
💡 Hint

Think about what enables coverage reporting in pytest.

Practice

(1/5)
1. What is the main purpose of using pytest-cov in testing?
easy
A. To speed up test execution time
B. To measure how much of your code is tested by your tests
C. To automatically fix failing tests
D. To generate test data automatically

Solution

  1. Step 1: Recall pytest-cov's core function

    pytest-cov is a plugin that tracks which parts of your code are executed during tests.
  2. Step 2: Eliminate incorrect options

    Only To measure how much of your code is tested by your tests correctly describes coverage measurement, others describe unrelated features.
  3. Final Answer:

    To measure how much of your code is tested by your tests -> Option B
  4. Quick Check:

    pytest-cov measures coverage = A [OK]
Hint: Remember: cov means coverage, it shows tested code parts [OK]
Common Mistakes:
  • Confusing coverage with test speed
  • Thinking pytest-cov fixes tests
  • Assuming it generates test data
2. Which command correctly installs the pytest-cov plugin?
easy
A. pip install pytest-cov
B. pip install pytest_cov
C. pip install pytestcov
D. pip install pytest coverage

Solution

  1. Step 1: Recall the exact package name

    The official package name is 'pytest-cov' with a hyphen.
  2. Step 2: Compare options with correct spelling

    Only pip install pytest-cov matches the correct package name and syntax.
  3. Final Answer:

    pip install pytest-cov -> Option A
  4. Quick Check:

    Correct package name = B [OK]
Hint: Use hyphen, not underscore or spaces in package name [OK]
Common Mistakes:
  • Using underscore instead of hyphen
  • Adding spaces in package name
  • Misspelling the package name
3. What will be the output when running pytest --cov=my_module if all code in my_module is covered by tests?
medium
A. Tests will fail automatically
B. An error saying coverage data not found
C. No coverage report is shown
D. A coverage report showing 100% coverage for my_module

Solution

  1. Step 1: Analyze the pytest --cov command

    The command runs tests and measures coverage for 'my_module'.
  2. Step 2: Determine output for 100% coverage

    If all code is tested, coverage report shows 100% coverage.
  3. Final Answer:

    A coverage report showing 100% coverage for my_module -> Option D
  4. Quick Check:

    Full coverage means 100% report = A [OK]
Hint: Full coverage means report shows 100% coverage [OK]
Common Mistakes:
  • Expecting errors when coverage is full
  • Thinking coverage report is hidden by default
  • Assuming tests fail if coverage is 100%
4. You run pytest --cov=my_module --cov-report=html but no HTML report is generated. What is the most likely cause?
medium
A. You forgot to install pytest-cov plugin
B. You did not specify the module name correctly
C. The HTML report is generated in a different folder
D. The tests did not run at all

Solution

  1. Step 1: Check common reasons for missing HTML report

    pytest-cov generates HTML reports in a folder named 'htmlcov' by default.
  2. Step 2: Understand report location

    The report is generated but may be in a folder you did not check.
  3. Final Answer:

    The HTML report is generated in a different folder -> Option C
  4. Quick Check:

    HTML report folder = C [OK]
Hint: Check 'htmlcov' folder for HTML report after running tests [OK]
Common Mistakes:
  • Assuming plugin is not installed without checking
  • Thinking report is shown in terminal only
  • Believing tests must fail to generate report
5. You want to measure coverage for multiple modules mod1 and mod2 and generate both terminal and HTML reports. Which command is correct?
hard
A. pytest --cov=mod1,mod2 --cov-report=term --cov-report=html
B. pytest --cov=mod1 --cov-report=term --cov=mod2 --cov-report=term
C. pytest --cov=mod1 mod2 --cov-report=term,html
D. pytest --cov=mod1 --cov=mod2 --cov-report=term --cov-report=term

Solution

  1. Step 1: Understand multiple module coverage syntax

    pytest-cov accepts multiple modules separated by commas in a single --cov option.
  2. Step 2: Understand multiple report formats syntax

    Multiple reports are specified by repeating the --cov-report flag, e.g., --cov-report=term --cov-report=html.
  3. Final Answer:

    pytest --cov=mod1,mod2 --cov-report=term --cov-report=html -> Option A
  4. Quick Check:

    Comma for modules, repeat --cov-report = D [OK]
Hint: Use commas to list modules in one --cov option [OK]
Common Mistakes:
  • Using multiple --cov options instead of comma separation
  • Combining modules without commas
  • Incorrectly combining report types in one option