I am trying to write some unit tests for some celery tasks in my Django application. These tasks take the model identifier as an argument, do some things, and update the model. When you start devserver and the celery worker, everything works fine, but when I ran my tests it became clear that the celery task does not use the django test db, which is created and destroyed as part of the test run. The question is, how can I get celery to use the same temporary db as the rest of my tests?
As you can see, I use parameter overrides, which are offered in each answer for similar problems.
UPDATE: It has been found that instead of passing the identifier of the object to the task and retrieving the task from the database, if I just transfer the object to my task, the tests work correctly, without affecting the functioning of the task. So, at least for now, this will be my decision.
In my test:
class JobTest(TestCase): @override_settings(CELERY_ALWAYS_EAGER=True, CELERY_EAGER_PROPAGATES_EXCEPTIONS=True, BROKER_BACKEND='memory') def test_Job_Complete(self): job = models.Job() job.save() tasks.do_a_thing(job.id) self.assertTrue(job.complete)
In my task:
@celery.task def do_a_thing(job_id): job = models.Job.objects.get(pk=job_id) bunch_of_things(job) job.complete = True job.save()
source share