Skip to content

alissa-huskey/python-coverage-demo

Repository files navigation

Pytest Coverage Demo Project

Code coverage demo

This project demonstrates how to setup a Python project using Poetry to get a report on code coverage.

Table of Contents

✨ Quick start

Install packages and start a poetry shell.

poetry add --dev pytest coverage
poetry shell

Then generate and view the coverage report.

coverage run -m pytest && coverage report -m

Screenshot

(back to top)

📓 Manifest

This 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

(back to top)

🤖 Usage

1. Running tests

First you'll need to run your tests using the coverage program.

coverage run -m pytest

Alternatively if you set up the configurations in your pyproject.py per the instructions below you can simply type:

coverage run

This 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 ===========================

(back to top)

2. View coverage report

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 report

Ta 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%

(back to top)

🚀 Setup

Dependencies

This project makes use of the following tools:

Tool Purpose
Poetry dependency management
Pytest test runner
Coverage.py code coverage measurement

(back to top)

1. Install Poetry

Install Poetry according to its docs.

(back to top)

2. Create project

Use an existing Poetry project or create one using the poetry new command.

poetry new my-project

(back to top)

3. Install test packages

Install the pytest and coverage packages:

poetry add --dev pytest coverage

Then start a poetry shell.

poetry shell

(back to top)

4. Configure coverage

Add 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 = true

(back to top)

5. Update .gitignore

Add .coverage to your .gitignore file.

.gitignore

__pycache__
.pytest_cache
.coverage

(back to top)

💡 See also

(back to top)

📋 Meta

The python-coverage-demo project is distributed under the MIT license.

About

Demo of a Python Poetry project using the coverage tool.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages