February 2022
Intermediate to advanced
274 pages
6h 28m
English
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 ... |