Code coverage demo
This project demonstrates how to setup a Python project using Poetry to get a report on code coverage.
Table of Contents
Install packages and start a poetry shell.
poetry add --dev pytest coverage
poetry shellThen generate and view the coverage report.
coverage run -m pytest && coverage report -mThis project was generated using the poetry new command and has this
directory structure.
├── .coverage
├── .gitignore
├── README.md
├── poetry.lock
├── pyproject.toml
├── python_coverage_demo
│ ├── __init__.py
│ └── demo.py
└── tests
├── __init__.py
└── test_python_coverage_demo.py
Here are more detailed descriptions of the relavant files.
| Filename | Contains |
|---|---|
.coverage |
generated results of the coverage run command |
.gitignore |
list of files and directories to exclude from source control |
pyproject.toml |
project dependencies and coverage settings |
python_coverage_demo/__init__.py |
version |
python_coverage_demo/demo.py |
demo python functions |
tests/test_python_coverage_demo.py |
tests for some, but not all, demo functions |
First you'll need to run your tests using the coverage program.
coverage run -m pytestAlternatively if you set up the configurations in your pyproject.py per the
instructions below you can simply type:
coverage runThis will run your tests via pytest as usual and will additionally generate
a .coverage file.
========================= test session starts ==========================
platform darwin -- Python 3.9.1, pytest-5.4.3, py-1.11.0, pluggy-0.13.1
rootdir: ~/demo/python_coverage_demo
plugins: pylama-8.4.1
collected 3 items
tests/test_python_coverage_demo.py ... [100%]
========================== 3 passed in 0.01s ===========================
To view the coverage report replace python_coverage_demo with the path(s) to
your code in the following command:
coverage report -m --include='python_coverage_demo/*.py'Alternatively if you set up the configurations in your pyproject.py per the
instructions below you can simply type:
coverage reportTa da!
Name Stmts Miss Cover Missing
----------------------------------------------------------------
python_coverage_demo/__init__.py 1 0 100%
python_coverage_demo/demo.py 6 2 67% 5, 8
----------------------------------------------------------------
TOTAL 7 2 71%
This project makes use of the following tools:
| Tool | Purpose |
|---|---|
| Poetry | dependency management |
| Pytest | test runner |
| Coverage.py | code coverage measurement |
Install Poetry according to its docs.
Use an existing Poetry project or create one using the poetry new command.
poetry new my-projectInstall the pytest and coverage packages:
poetry add --dev pytest coverageThen start a poetry shell.
poetry shellAdd the following to your pyprpoject.toml replacing python_coverage_demo with the
name of the directory containing your code.
pyproject.toml
[tool.coverage.run]
command_line = "-m pytest"
[tool.coverage.report]
include = ["python_coverage_demo/*.py"]
show_missing = trueAdd .coverage to your .gitignore file.
.gitignore
__pycache__
.pytest_cache
.coverage
The python-coverage-demo project is distributed under the MIT license.
