Unit Testing/Pytest/View Test Coverage
< Unit Testing | Pytest
Install Coverage
editInstall Coverage using either:
pip install coverage
pip3 install coverage
Use Coverage to Run Pytest
editRun tests using Coverage to monitor test coverage:
coverage run -m pytest
Generate a Coverage Report
editGenerate a coverage report:
coverage report
Generate a coverage report with line numbers:
coverage report -m
Generate a Coverage HTML Report
editGenerate a coverage HTML report:
coverage html
Look for an htmlcov
folder and open index.html
in your browser. In the report, select multiply.py
to view test coverage of the multiply function.
Customize the Coverage HTML Report
editA .coveragec
file is used to customize the location and content of the Coverage HTML report. For example, the following would omit any Python library files in the /.local
folder, exclude pragma: no cover
lines, and rename the htmlcov
folder to coverage_report
. See Coverage documentation for more information.
[run] omit = */.local/* [report] exclude_lines = pragma: no cover [html] directory = coverage_report