Flickr Badge

Sunday, April 22, 2007

Code coverage for your Django code

One of the things that I like about Django is that it allows you to run unit tests on the code with relative ease. So I have a bunch of unit tests now, and I was looking to expand it. For that I first needed to know which parts of the code had good coverage and which parts had no coverage. I needed to integrate code coverage with the unit testing framework. In the end, it involved modifying a few files. This is what I did:
  1. First, I downloaded Ned Batchelder's module.
  2. Next, get this script to colorize the coverage output. I saved it as
  3. Put both files somewhere on the python path. I put it at Python24\Lib\site-packages
  4. Now we need to modify the django unit test runner to include coverage. Head over to your django\test directory and edit the file
  5. At the top, add the following line:
    import os, coverage, coverage_color
  6. Scroll down to the run_tests function. You will see a line like this:
    destroy_test_db(old_name, verbosity)
  7. Modify it to read like this:
    if not os.path.exists(settings.COVERAGE_DIR):
    for module_string in settings.COVERAGE_MODULES:
    module = __import__(module_string, globals(), locals(), [""])
    f,s,m,mf = coverage.analysis(module)
    fp = file(os.path.join(settings.COVERAGE_DIR, module_string + ".html"), "wb")
    coverage_color.colorize_file(f, outstream=fp, not_covered=mf)
    destroy_test_db(old_name, verbosity)
  8. What that does is to record the coverage when running the tests. It then creates a directory for putting the HTML output, creates the colorized version of the source and dumps it into the output directory. At the end of everything, it cleans up the coverage data
  9. We now need to configure the COVERAGE_DIR and COVERAGE_MODULES settings. Open your Django file and add the following lines:
    COVERAGE_DIR = "scripts/build/coverage" # Where the HTML output should go
    COVERAGE_MODULES = ["apps.catalyst.views", "apps.catalyst.models"] # The modules that you want colorized
  10. Save and run your Django unit tests. After running, you will have a html file for each module in the specified directory with the colorized coverage output
Check out the output: The lines in red were not executed by any of the unit tests. Looks like there is a function that I'm not testing at all!


Olivier Grisel said...

Please also have a look at trace2html that achieves a similar task:

Sample report available online here:

Eddy Mulyono said...

Thanks for sharing.

Have you tried submitting this code upstream?

I'm pretty sure many people would find this code useful.

Anonymous said...

I tried this approach (and trace2html) but they don't seem to want to cover anything in models or views for some reason. Does anyone know why?

I tried running manually too and that didn't bring up any coverage information for code which clearly was being tested.

Siddhi said...

The trace2html output does look nice.

I thought about submitting it. I left it to clean up the code a bit, but then got too busy.

@Jason: Not sure what the problem is.

To test the views, you need to either call the view function explicitly or call it via the test client. Can you try something simple like shown here:

Eddy Mulyono said...

Hi Siddhi.

I expanded on your approach, and made doctests report coverage, too.

You can find out more in my post.

I wouldn't be able to do it if you didn't post this article, so, again: Thanx! said...

I have bookmarked your website because this site contains valuable information in it. I am really happy with articles quality and presentation about Django code. Thanks a lot for keeping great stuff. it services companies in dubai