celery-mock allows you to mock celery task and then run them when you want
Sometimes a celery task retries itself to wait for some event or model to change. This is hard to test because celery tasks run (and retry) inline in tests. Now you can choose when to run your tasks.
- Python 3 support
- Celery 3.1.x and 4.0.x support
pip install celery-mock
from celery_mock import task_mock from django.test import TestCase, Client from myapp import dummyview class UsersTestCase(TestCase): def test_create_user(self): client = Client() client.post('/api/users/', data={'username': 'konrad') # runs tasks inline with task_mock(): client.post('/api/users/', data={'username': 'konrad') # no tasks started yet # all tasks ran here with task_mock('myapp.post_user_create_task'): client.post('/api/users/', data={'username': 'konrad') # all tasks started execept myapp.post_user_create_task # myapp.post_user_create_task started here # you can use task_mock manually: tmock = task_mock().start() client.post('/api/users/', data={'username': 'konrad') # no tasks started yet tomock.stop() # all tasks ran here