celery | Matthew Daly's Bloghttps://matthewdaly.co.uk/blog/categories/celery/
celery | I'm a web developer in Norfolk. This is my blog...Sat, 10 Mar 2018 15:12:45 GMThttp://blogs.law.harvard.edu/tech/rssgrunt-blogbuilder https://github.com/matthewbdaly/grunt-blogbuilderMatthew Daly 2018https://matthewdaly.co.uk/blog/2015/06/17/getting-django-behave-and-celery-to-work-together/
https://matthewdaly.co.uk/blog/2015/06/17/getting-django-behave-and-celery-to-work-together/Wed, 17 Jun 2015 19:34:08 GMTI ran into a small issue today. I’m working on a Django app which uses Celery to handle certain tasks that don’t need to return a response within the context of the HTTP request. I also wanted to use django_behave for running BDD tests. The trouble is that both django_behave and Celery provide their own custom test runners that extend the default Django test runner, and so it looked like I might have to choose between the two.

However, it turned out that the Celery one was actually very simple, with only a handful of changes needing to be made to the default test runner to make it work with Celery. I was therefore able to create my own custom test runner that inherited from DjangoBehaveTestSuiteRunner and applied the changes necessary to get Celery working with it. Here is the test runner I wrote, which was saved as myproject/runner.py:

from django.conf import settings

from djcelery.contrib.test_runner import _set_eager

from django_behave.runner import DjangoBehaveTestSuiteRunner

classCeleryAndBehaveRunner(DjangoBehaveTestSuiteRunner):

defsetup_test_environment(self, **kwargs):

_set_eager()

settings.BROKER_BACKEND = 'memory'

super(CeleryAndBehaveRunner, self).setup_test_environment(**kwargs)

To use it, you need to set the test runner in settings.py

TEST_RUNNER = 'myproject.runner.CeleryAndBehaveRunner'

Once that was done, my tests worked flawlessly with Celery, and the Behave tests ran as expected.