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 manage.py):

[run]
omit =
    yourproject/__init__.py
    yourproject/settings.py
    yourproject/settings_testing.py
    yourproject/wsgi.py
    manage.py
    */migrations/*

    # exclude tests
    conftest.py

    # exclude factories
    */factories.py


[report]
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__.:

[html]
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).