Using coverage.py with pytest-cov
Both coverage.py and pytest-cov are third-party packages that need to be installed before use:
| $ pip install coverage |
| $ pip install pytest-cov |
To run tests with coverage.py, you need to add the --cov flag and provide either a path to the code you want to measure, or the installed package you are testing. In our case, the Cards project is an installed package, so we’ll test it using --cov=cards.
The normal pytest output is followed by the coverage report, as shown here:
| $ cd /path/to/code |
| $ pytest --cov=cards ch7 |
| ============================ test session starts ============================= |
| collected 27 items |
| |
| ch7/test_add.py ..... [ 18%] |
| ch7/test_config.py ... |
Get Python Testing with pytest now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.