Skip to Main Content
Python Testing with pytest
book

Python Testing with pytest

by Brian Okken
February 2022
Intermediate to advanced content levelIntermediate to advanced
274 pages
6h 28m
English
Pragmatic Bookshelf
Content preview from Python Testing with pytest

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python Testing with pytest

Python Testing with pytest

Brian Okken

Publisher Resources

ISBN: 9781680509427Errata Page