Adding coverage to you Django project

Now once you've installed and configured your Django project with py.test, let's add Coverage!

First install coverage like so:

$ pip install coverage==3.7.1

Then add a .coveragerc in the root of your project (next to

omit =

    # exclude tests

    # exclude factories

exclude_lines =
    pragma: no cover

    # Don't complain about missing debug-only code:
    def __repr__
    if settings\.DEBUG

    # Don't complain if tests don't hit defensive assertion code:
    raise AssertionError
    raise NotImplementedError
    except ImportError

    # Don't complain if non-runnable code isn't run:
    if 0:
    if __name__ == .__main__.:

directory = htmlcov
title = YourProject Coverage Report

Where yourproject should be replaced with the name of your project.

Now you can run the coverage like so:

$ coverage run --source . -m py.test && coverage html

The output will be generated in the directory you configured in .coveragerc (htmlcov by default).