We use pytest-cov to check how much of our code is tested. It helps find parts that need more tests.
pytest-cov setup
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
pip install pytest-cov
pytest --cov=your_package_name
# Optional: generate HTML report
pytest --cov=your_package_name --cov-report=htmlReplace your_package_name with the folder or module you want to check coverage for.
The --cov-report=html option creates a nice report you can open in a browser.
Examples
PyTest
pip install pytest-cov
myapp folder in the terminal.PyTest
pytest --cov=myapp
htmlcov folder.PyTest
pytest --cov=myapp --cov-report=html
Sample Program
This simple test checks the add function. Running pytest with coverage shows which lines ran.
PyTest
# test_sample.py def add(a, b): return a + b def test_add(): assert add(2, 3) == 5 # Run in terminal: # pytest --cov=. --cov-report=term-missing
Important Notes
Always install pytest-cov in the same environment where you run pytest.
Use --cov-report=term-missing to see which lines are not covered in the terminal.
Coverage reports help you find untested code to improve your tests.
Summary
pytest-cov measures how much code your tests cover.
Install it with pip install pytest-cov and run pytest with --cov.
You can create reports in the terminal or as HTML files.
Practice
1. What is the main purpose of using
pytest-cov in testing?easy
Solution
Step 1: Recall pytest-cov's core function
pytest-cov is a plugin that tracks which parts of your code are executed during tests.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.Final Answer:
To measure how much of your code is tested by your tests -> Option BQuick 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
Solution
Step 1: Recall the exact package name
The official package name is 'pytest-cov' with a hyphen.Step 2: Compare options with correct spelling
Only pip install pytest-cov matches the correct package name and syntax.Final Answer:
pip install pytest-cov -> Option AQuick 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
Solution
Step 1: Analyze the pytest --cov command
The command runs tests and measures coverage for 'my_module'.Step 2: Determine output for 100% coverage
If all code is tested, coverage report shows 100% coverage.Final Answer:
A coverage report showing 100% coverage for my_module -> Option DQuick 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
Solution
Step 1: Check common reasons for missing HTML report
pytest-cov generates HTML reports in a folder named 'htmlcov' by default.Step 2: Understand report location
The report is generated but may be in a folder you did not check.Final Answer:
The HTML report is generated in a different folder -> Option CQuick 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
Solution
Step 1: Understand multiple module coverage syntax
pytest-cov accepts multiple modules separated by commas in a single --cov option.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.Final Answer:
pytest --cov=mod1,mod2 --cov-report=term --cov-report=html -> Option AQuick 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
