Unit Testing/Pytest/View Test Coverage

From Wikiversity
Jump to navigation Jump to search

Install Coverage[edit | edit source]

Install Coverage using either:

  • pip install coverage
  • pip3 install coverage

Use Coverage to Run Pytest[edit | edit source]

Run tests using Coverage to monitor test coverage:

coverage run -m pytest

Generate a Coverage Report[edit | edit source]

Generate a coverage report:

coverage report

Generate a coverage report with line numbers:

coverage report -m

Generate a Coverage HTML Report[edit | edit source]

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[edit | edit source]

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[edit | edit source]