Django Testing Code Coverage

As part of the summer of code 2009, Django test coverage has been developed. I mentored Kevin Kusabik, who developed the code. It is hopefully going to be merged in 1.2, but there are still a few issues to be worked out in the implementation. That said, it currently works, and provides a nice introspective view of your code. This post will tell you how to run coverage on your code base.

It should be noted that having code coverage is a good way to look into your code, but doesn't guarantee that there are no bugs. Ned Batchelder's Pycon talk last year is a good introduction to coverage. We are using his Coverage.py module in this example to produce the coverage output in Django.

I have taken the commits from the Summer of Code and put them in a Branch on github. You will want to clone this and put it on your PYTHONPATH as your django module. If you are already using the github mirror, simply add me as a remote and pull down the coverage branch.

git remote add ericholscher git://github.com/ericholscher/django
git fetch ericholscher
git co -b coverage ericholscher/coverage

Once you have the code, you simple run your tests in the normal manner. However, now have the added options of --coverage and --report. If you run the test command with just --coverage, it will generate a text based coverage report. If you also specify the --report option, it will output a HTML report in the current directory. The HTML report is where most of the value of coverage comes from, allowing you to see what lines were covered and missed. Here is an example HTML report, showing the Django source code's coverage.

One of the major problems with coverage is that it slows down running tests by a non-trivial amount. For every instruction executed, there must be a record made. With coverage.py 3.0, this extension is written in C for speed, however it still noticeably slows down test speed.

I hope that you give it a try and enjoy the results. I'll be spending some time over the next week or 2 cleaning up the code and trying to get it into shape for inclusion in Django.




Comments

1 Martin De Wulf says...

Great ! I will try this tonight. Thanks so much.

Posted at 7:56 a.m. on November 15, 2009

2 Steve Call says...

Thanks a lot for your useful post. But I had difficulty navigating through your web site as I kept getting 502 bad gateway error. Just thought to let you know.

Posted at 7:06 p.m. on November 15, 2009

3 Eric Holscher says...

Odd. The site is working fine for me, but a couple people have e-mailed me about it. I'll try and look in my logs.

Posted at 9:57 p.m. on November 15, 2009

4 Compute Clusters says...

Good on ya!! and thanks!!

Posted at 6:32 a.m. on November 16, 2009

5 recurser says...

I was just trying to figure out how to generate coverage reports the day you posted this. Works like a dream, thank you very much!!

Posted at 11:10 p.m. on November 17, 2009

Comments support markdown

Comments are closed.

Comments have been close for this post.