Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Django coupled with Celery

I'm having some trouble trying to figure how to test my app architecture. I've already 60% of my website complete, with full unit testing coverage (over all utility/lib functions, celery tasks as simple functions, and so on).

The problem arises when I try to test django views (plain functions) that executes celery tasks (delay method).

Example:

def myview(request):
  ...
  mytask.delay(myval)
  ... 

What should be the right way to test that scenes without really generating a new task execution?

The obvious way is to set up a condition before every task delay call, executing it only if it's not in test environment, but it seems really dirty.

Any tip?

like image 479
Isaac Avatar asked May 02 '26 02:05

Isaac


1 Answers

Use CELERY_ALWAYS_EAGER settings for test run.

It make the function to be called immediately instead of running it as a task.


Example django settings snippet:

if 'test' in sys.argv:
    CELERY_ALWAYS_EAGER = True
like image 57
falsetru Avatar answered May 03 '26 15:05

falsetru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!