March 15, 2012

Code coverage in django with ‘coverage’ and ‘django-jenkins’

Posted in Software at 18:59 by graham

Getting unit test code coverage in python, and in django, is easy thanks to coverage:

pip install coverage
coverage run test app1 app2 app3
coverage html --include="my_project/*"
www-browser htmlcov/index.html

A disadvantage of Django’s test runner is that by default it runs tests for all the packages in INSTALLED_APPS. Usually you only want to run tests for your apps, not for third-party libraries. Hence I prefer the test runner in django-jenkins:

pip install django-jenkins

or directly from git (at time of writing the –coverage-html is only in git):

pip install git+git://

Edit to add these lines, changing “my_project” in the last line to your project’s top level package name:

INSTALLED_APPS += ('django_jenkins',)
JENKINS_TASKS = ('django_jenkins.tasks.django_tests',)
PROJECT_APPS = [appname for appname in INSTALLED_APPS if appname.startswith('my_project')]

Now run jtest with coverage, and it will always include all your apps, and only your apps: jtest --coverage-html-report=htmlcov

Older versions of django_jenkins don’t have --coverage-html, so use this:

coverage run jtest
coverage html --include="my_project/*"
www-browser htmlcov/index.html

As the name implies, django-jenkins is also a great choice if you’re using continuous integration server Jenkins.

Updated to include –coverage-html. Thanks kmmbvnr!

Note: I’ve had trouble running debugger pdb / pudb in a unit test started from jtest. Using just ‘test’ works fine.

March 13, 2012

In-memory key-value store in C, Go and Python

Posted in Software at 06:00 by graham

On paternity leave for my second child, I found myself writing an in-memory hashmap (a poor-man’s memcached), in Go, Python and C. I was wondering how hard it would be to replace memcached if we wanted to do something unusual with our key-value store. I also wanted to compare my knowledge of the languages, and, well, I get bored easily!

The code is on github as Key-Value-Polyglot.

Each version implements enough of the get and set commands from the memcached protocol that we can test them with a memcached client.

The wonderful Internet

Thanks to a great discussion on Hacker News, in the comments here, and on reddit, I have heavily updated this post. You’re reading the new improved version.

Having the Internet review your code is humbling. It is also an amazing way to learn. The code, and my knowledge, have improved tremendously thanks to the feedback.

Read the rest of this entry »