« The importance of striving for awesome. | You should stay for the sprints »
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.
Posted at 10:01 p.m. on November 13, 2009
Comments: 5
Tags: coverage , django , summerofcode , testing
Mocking an External Web Service in Python
Automating tests in Django
Announcing Django Crawler and django-test-utils
Welcome to the home of Eric Holscher on the web. I talk about software development, mostly in the realm of Django. I am interested in the real time web, testing, mobile apps, and other things.
Why Read the Docs matters
1 week, 5 days ago (Comments: 7)
Read the Docs Update
9 months, 4 weeks ago (Comments: 2)
Using Reviewboard with Git
1 year ago (Comments: 0)
Read the Docs Updates
1 year ago (Comments: 1)
Handling Django Settings Files
1 year ago (Comments: 12)
Required Reading
1 year, 2 months ago (Comments: 0)
Using Haystack to index non-database content
1 year, 2 months ago (Comments: 4)
Correct commands to check out and update VCS repos
1 year, 2 months ago (Comments: 0)
Site upgrades
1 year, 2 months ago (Comments: 0)
Building a Django App Server with Chef: Part 4
1 year, 2 months ago (Comments: 1)
Setting up Django and mod_wsgi
Building a Django App Server with Chef: Part 1
Screencast: Django Command Extensions
Big list of Django tips (and some python tips too)
Handling Django Settings Files
Lessons Learned From The Dash: Easy Django Deployment
Large Problems in Django, Mostly Solved: Delayed Execution
Building a Django App Server with Chef: Part 2


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