Python/Testing/CodeCoverage

From Dev Wiki
Jump to navigation Jump to search

Python CodeCoverage is a tool to measure UnitTest coverage for Python code.

It can be very useful for making sure your tests cover all relevant cases.


Note: Coverage (alone) is not necessarily all-encompassing.

For example, it's possible to technically have "100% code coverage" (in that all lines are touched at least once), yet not actually be testing for important edge cases, etc.

Having said that, high code coverage is one indicator (of many) that you can use to gauge effectiveness of your UnitTests.


Installation

Using the Python environment of choice, run:

pip install coverage


Running Code Coverage

The base command is:

coverage run --source='<relative_path>' --omit='<relative_path>' -m <entity_to_run>

Where:

  • entity_to_run - Indicates the testing method to execute with.
    • Ex: manage.py test or pytest.
  • source - The path to the directory to recursively check coverage within.
    • Usually, this is the path to project root, such as --source='.'.
  • omit - The path to sub-directories of source to exclude.
    • Ex: There's no point in checkout our environment so we can add --omit='.venv/*'.

For example, a full example using Pytest (to check coverage at project root, and exclude only the venv folder):

coverage run --source='.' --omit='.venv/*' -m pytest


Viewing Coverage Results

Running Code Coverage will only generate results to files, not necessarily show them.

View basic results with:

coverage report

Optional useful args:

  • -m - Display line numbers of missing coverage for each file.
  • --skip-covered Skip output for any files at 100% coverage. Particularly useful for large projects with good coverage already.

Ex:

coverage report -m --skip-covered


Generate result as html with:

coverage html

Optional useful args:

  • -d - Allows specifying a directory to generate html files to.