简体   繁体   中英

How to test code that creates Celery tasks?

I've read Testing with Celery but I'm still a bit confused. I want to test code that generates a Celery task by running the task manually and explicitly, something like:

def test_something(self):
    do_something_that_generates_a_celery_task()
    assert_state_before_task_runs()
    run_task()
    assert_state_after_task_runs()

I don't want to entirely mock up the creation of the task but at the same time I don't care about testing the task being picked up by a Celery worker. I'm assuming Celery works.

The actual context in which I'm trying to do this is a Django application where there's some code that takes too long to run in a request, so, it's delegated to background jobs.

In test mode use CELERY_TASK_ALWAYS_EAGER = True . You can set this setting in your settings.py in django if you have followed the default guide for django-celery configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM