Setting up a test runner

Ned Batchelder’s is an invaluable tool for any Python project. django_coverage makes run inside of Django, and this is my preferred way of using that tool.

Step 1 - environment prep

In your virtualenv install the necessary requirements:

$ pip install -r requirements.txt

Make a coverage directory in your project directory:

# This is done for you in django-party-pack
#   but you'll need to remember it for future projects
$ mkdir coverage

Step 2 - create

Create a file into your project root and paste in the following code:

# Make our own testrunner that by default only tests our own apps

from django.conf import settings
from django.test.simple import DjangoTestSuiteRunner
from django_coverage.coverage_runner import CoverageRunner

class OurTestRunner(DjangoTestSuiteRunner):
    def build_suite(self, test_labels, *args, **kwargs):
        return super(OurTestRunner, self).build_suite(test_labels or settings.PROJECT_APPS, *args, **kwargs)

class OurCoverageRunner(OurTestRunner, CoverageRunner):

Step 3 - settings customization

The first thing you’ll notice about dpp is that apps installment is broken up into three variables:

  • PREREQ_APPS - These are either built-in Django apps or third-party apps you don’t want to test.
  • PROJECT_APPS - These are the custom apps you’ve written for your project. You want to test these.
  • INSTALLED_APPS - This is what Django loads into it’s app cache. We generate this iterable by adding PREREQ_APPS to PROJECT_APPS.

Here is the sample code from dpp/pollaxe project file:


    'polls', # or whatever your custom project uses


Also in, underneath where you have defined the PREREQ_APPS setting, add the following:

TEST_RUNNER = 'testrunner.OurCoverageRunner'
    'tests$', 'settings$', 'urls$', 'locale$',
    'migrations', 'fixtures', 'admin$',

Step 4 - run it!

From the command-line:

$ python test

Open file:///path-to-your-project/coverage/index.html in a web browser and check out your coverage.