YASE - Yet Another Software Engineer
Measuring code coverage is a fundamental practice in software development to understand what percentage of your source code is actually executed by your test suite. It doesn’t guarantee that the code is correct, but it helps you identify untested parts.
The standard tool in Python for this is coverage.py
, which integrates perfectly with pytest
via the pytest-cov
plugin.
Here’s how to do it:
1. Basic Concept: What is Code Coverage?
Coverage measures how many executable “parts” of your code are activated during test execution. These “parts” can be:
if
/else
, try
/except
) been taken? (More comprehensive than line coverage).The goal is usually to aim for high coverage (often 80-90% or more), but 100% is not always necessary or practical and, I repeat, does not guarantee the absence of bugs. It’s more important to test critical logic well.
2. Installing the Tools
You’ll need pytest
and the pytest-cov
plugin. Install them via pip
in your virtual environment:
pip install pytest pytest-cov
pytest-cov
also takes care of installing coverage.py
as a dependency if it’s not already present.
The easiest way to run coverage tests is using pytest
. Just add the --cov
option to your normal pytest
command.
You can set the main settings for pytest coverage in the .coveragerc
file:
[run]
branch = true # Always enable branch coverage testing
source =
app # Specify here the modules to measure (alternative to --cov)
omit =
*/tests/* # Exclude files in the tests folder
*/__main__.py # Exclude any __main__ scripts
*/migrations/* # Exclude migrations (if using an ORM)
*/venv/* # Exclude the virtual environment
[report]
show_missing = true # Always show missing lines in the terminal report
fail_under = 40 # Fail the execution (exit code != 0) if total coverage is < 40%
exclude_lines =
pragma: no cover # Do not consider lines with this comment
def __repr__ # Exclude __repr__ definitions
if __name__ == .__main__.: # Exclude the standard main block
# Python 3.7+ type hint guard
if typing.TYPE_CHECKING:
# Abstract methods
@(abc\.)?abstractmethod
[html]
directory = coverage_html_report # Change the name of the HTML report folder
(Note: Added common exclude_lines
for typing.TYPE_CHECKING
and abstract methods to the .coveragerc
example)
Pytest-cov also allows saving in JSON format for future processing.