Test coverage

Installation

You need to have the python-coverage paquet installed.

Commands

First, run the coverage on the tests:

python-coverage run --source <module_name> -m py.test

Then generate the HTML reports:

python-coverage html

The HTML result index is located here : htmlcov/index.html

Usage

Click on the test file you wanna have look at. Python coverage exposes the total statements, that is:

  • the number of the run tests: run
  • the code that miss test: missed
  • the code that has voluntary been excluded from the coverage: excluded

The important part is the code that has been missed of course. You can click on the missed button to see the missed code highlighted. Very practical.

The aim is to have 0 missed code, that is 100% of the code is tested.

If you don’t want some code line to be covered because this situation shall not just happen and corresponds to nothing, you can of course exclude this line of code from the coverage. Just use the pragma statement in the code:

else:  # pragma: no cover
    raise CreoleDictConsistencyError(_('cannot found a master {} '
                                       'nor a slave {}'.format(master_name,
                                                              slave_names)))

For further usage, have a look at the python coverage docs