47. Pytest code coverage | pytest-cov | coverage on tests| html report | installation | Rajiv

Опубликовано: 18 Апрель 2026
на канале: The Python Gurukul
1,102
19

coverage - tools that measure code coverage watch code while a test suite is being run and keep track of which lines are hit and which are coverage - tools that measure code coverage watch code while a test suite is being run and keep track of which lines are hit and which are not. That measurement—called line coverage - tools that measure code coverage watch code while a test suite is being run and keep track of which lines are hit and which are not. That measurement—called line coverage—is calculated by dividing the total number of lines run divided by the total lines of code. Code coverage tools can also tell if all paths are taken in control statements, a measurement called branch coverage.

Code coverage cannot tell if test suite is good; it can only tell how much of the application code is getting hit by test suite. But that in itself is useful information.


For running coverage with pytest-cov, use:
pytest --cov=cards test path to run with a simple report
pytest --cov=cards --cov-report=term-missing test path to show which lines weren’t run
pytest --cov=cards --cov-report=html test path to generate an HTML report

For running coverage by itself, use:

coverage run --source=cards -m pytest test path to run the test suite with coverage
coverage report to show a simple terminal report
coverage report --show-missing to show which lines weren’t run
coverage html to generate an HTML report
Even if you ran coverage from pytest --cov=..., you can run different reports or generate HTML using coverage report and coverage html.

The --cov and --source flags tell coverage what code to watch, and can either be the name of an installed package, or the path to the application code.

There’s a lot more to coverage.py and pytest-cov than we’ve covered here.coverage—is calculated by dividing the total number of lines run divided by the total lines of code. Code coverage tools can also tell if all paths are taken in control statements, a measurement called branch coverage.

Code coverage cannot tell if test suite is good; it can only tell how much of the application code is getting hit by test suite. But that in itself is useful information.


For running coverage with pytest-cov, use:
pytest --cov=cards test path to run with a simple report
pytest --cov=cards --cov-report=term-missing test path to show which lines weren’t run
pytest --cov=cards --cov-report=html test path to generate an HTML report

For running coverage by itself, use:

coverage run --source=cards -m pytest test path to run the test suite with coverage
coverage report to show a simple terminal report
coverage report --show-missing to show which lines weren’t run
coverage html to generate an HTML report
Even if you ran coverage from pytest --cov=..., you can run different reports or generate HTML using coverage report and coverage html.

The --cov and --source flags tell coverage what code to watch, and can either be the name of an installed package, or the path to the application code.

There’s a lot more to coverage.py and pytest-cov than we’ve covered here.ot. That measurement—called line coverage—is calculated by dividing the total number of lines run divided by the total lines of code. Code coverage tools can also tell if all paths are taken in control statements, a measurement called branch coverage.

Code coverage cannot tell if test suite is good; it can only tell how much of the application code is getting hit by test suite. But that in itself is useful information.


For running coverage with pytest-cov, use:
pytest --cov=cards test path to run with a simple report
pytest --cov=cards --cov-report=term-missing test path to show which lines weren’t run
pytest --cov=cards --cov-report=html test path to generate an HTML report

For running coverage by itself, use:

coverage run --source=cards -m pytest test path to run the test suite with coverage
coverage report to show a simple terminal report
coverage report --show-missing to show which lines weren’t run
coverage html to generate an HTML report
Even if you ran coverage from pytest --cov=..., you can run different reports or generate HTML using coverage report and coverage html.

The --cov and --source flags tell coverage what code to watch, and can either be the name of an installed package, or the path to the application code.

There’s a lot more to coverage.py and pytest-cov than we’ve covered here.