pytest-cov helps you see how much of your code is tested. It shows which parts are checked and which are not.
0
0
pytest-cov for coverage
Introduction
You want to check if your tests cover all important parts of your program.
Before sharing your code, to make sure tests are thorough.
When adding new features, to confirm tests cover them.
To find untested code that might cause bugs.
To improve test quality by focusing on uncovered code.
Syntax
PyTest
pytest --cov=your_package tests/
Replace your_package with the folder or module you want coverage for.
tests/ is the folder where your test files are located.
Examples
Runs tests in
tests/ and measures coverage for the myapp package.PyTest
pytest --cov=myapp tests/
Shows coverage report in the terminal and highlights missing lines.
PyTest
pytest --cov=myapp --cov-report=term-missing tests/
Generates an HTML report you can open in a browser to see coverage details.
PyTest
pytest --cov=myapp --cov-report=html tests/
Sample Program
This example has a simple module with two functions: add and subtract. The test only checks add. Running pytest with coverage will show subtract is not tested.
PyTest
def add(a, b): return a + b def subtract(a, b): return a - b # test_sample.py import pytest from sample import add def test_add(): assert add(2, 3) == 5
OutputSuccess
Important Notes
Install pytest-cov with pip install pytest-cov before using it.
Use --cov-report=term-missing to see exactly which lines are not covered.
HTML reports help visualize coverage in a friendly way.
Summary
pytest-cov shows how much of your code is tested.
Run tests with pytest --cov=your_package to get coverage info.
Use reports to find untested code and improve your tests.