Handle Celery-dependent tests in Django and with django-jenkins

So in your life, one of these days, you're going to realize you need tests, and that “maybe” you also need to test components that depend on several Celery tasks.

Well to help you make this day more productive and less painful, here's a few tips.

First to make it work with Django-celery, a pretty good but small documentation is available here http://ask.github.com/django-celery/cookbook/unit-testing.html it may seems small and not enough, but it actually is enough. To sum up all you need to make it work with ./manage.py test is to change the test runner to :

[sourcecode language=”python”]
TEST_RUNNER = ‘djcelery.contrib.test_runner.CeleryTestSuiteRunner’
[/sourcecode]

But it's not enough, soon when you think everything's over, you'll want to deploy and make it go through Jenkins's testing processes. Well i don't know about you but to me the django-jenkins project is quite a good one and i use it everyday but it has one flaw, it already as its designated TEST_RUNNER so if you try to execute your tests through ./manage.py jenkins it won't work and you'll get at least a Connection Refused error.

Here's how to fix this, the documentation says if you want to replace the TEST_RUNNER you may do so, but the class you'll use needs to inherit from django_jenkins.runner.CITestSuiteRunner and if you want the Celery tasks to work you need to use the djcelery.contrib.test_runner.CeleryTestSuiteRunner.

Fair enough, here's what i did. I created a new class :

[sourcecode language=”python”]
from django_jenkins.runner import CITestSuiteRunner
from djcelery.contrib.test_runner import CeleryTestSuiteRunner

class MixedInTestRunner(CITestSuiteRunner, CeleryTestSuiteRunner):
pass
[/sourcecode]

Why not ? and used it as such defining the new TEST_RUNNER by changing the settings's variable JENKINS_TEST_RUNNER to the newly created class.

And voilà.