How To Use Coverage in Django

When you are developing a large project it is usually to forget some parts of the code you want to tests. is a python tool that helps you to know that information.


Use pip or easy_install, as you want

pip install coverage 

Running and erasing

For running on a django project, use this command

coverage run --source='.' test the-app-you-want-to-test

This command will fill a “.coverage”, located in COVERAGE_FILE and then you may see results or report. If you need to remove gathered data, execute:

coverage erase

For a single file

Maybe you only want to check a python code, then do

coverage run arg1 arg2 arg3

There are some additional options, take a look on

About templates, see the plugin section and install one of those eggs:

See results

If you want to show the results in the command line, run:

coverage report

For more readable reports:

coverage html

To know concretely what part of your code is covered by tests, use:

coverage annotate -d directory-where-to-put-annotated-files

It will generate same source code file with an additional syntax on it:

  • Line with “>” means it was executed

  • Line beginning with “!” means it was not executed

  • Line starting with “-” means the line was excluded in the coverage statistics

Good coverage level

A good coverage usually comes on 90%. However, if you see 100% it could be not so good signal because it could be someone dealing with coverage instead of quality of tests.

Some tips:

  • Be careful with the quality of your tests

  • Don’t slow down your dev. velocity of the sake of the coverage

  • Use coverage to find untested code and decide if it deserve to cover

Excluding code

So far so good, however sometimes we need to exclude some code that we don’t need to cover. There are several options:

  1. Specifying files to exclude and omit in .coveragerc

  2. Writing in the same line of a block the comment

 # pragma: no cover

For example, if you want to exclude generated code from coverage because it is suppossed to be maintained by the generation tool:

def generated_code():   # pragma: no cover

If you like this library, don’t miss more features and take a deep dive into documentation.

About the author

How To Use Django-SU

How To Use Django-SU

One of the many tedious processes that checking permissions has, is that of using the logout and login functionalities to ...

How To Create a Tab for Open edX

How To Create a Tab for Open edX

A tab in open edx is a section inside a course, accessible from any part of it. You can add new StaticTabs for your courses ...

How To Install Django Debug Toolbar

How To Install Django Debug Toolbar

The Django Debug Toolbar is a configurable set of panels that display various debug information about the current ...

How To Install Django-LocalFlavor

How To Install Django-LocalFlavor

Have you ever had to develop a web application with unique fields depending on the country to which the user is focused or ...

12 Tips and Tricks for Improved Django Security!

12 Tips and Tricks for Improved Django Security!

In this article we shall discuss a point to which we give little importance when we are beginning to programme our ...

Let’s have a coffee and talk about your project


Let’s have a coffee and talk about your project


We use cookies to ensure you get the best experience on our website. More info.