< Unit Testing < Pytest

Install Coverage

Install Coverage using either:

  • pip install coverage
  • pip3 install coverage

Use Coverage to Run Pytest

Run tests using Coverage to monitor test coverage:

coverage run -m pytest

Generate a Coverage Report

Generate a coverage report:

coverage report

Generate a coverage report with line numbers:

coverage report -m

Generate a Coverage HTML Report

Generate 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

A .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

See Also

This article is issued from Wikiversity. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.