Created
October 17, 2018 13:37
-
-
Save firewut/8522a855f78062bf1eb2955829019042 to your computer and use it in GitHub Desktop.
Celery Testing Sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from celery.contrib.testing import app as _app | |
import mock | |
import unittest | |
import celery | |
class TestCase(unittest.TestCase): | |
def setUp(self): | |
app = _app.TestApp( | |
'my_app', | |
config={ | |
'CELERY_TASK_EAGER_PROPAGATES': True, | |
'CELERY_TASK_ALWAYS_EAGER': True, | |
}, | |
set_as_current=True, | |
enable_logging=True, | |
) | |
app.autodiscover_tasks( | |
[ | |
'app_with_tasks', | |
] | |
) | |
def send_task_apply(*args, **kwargs): | |
task_name = args[0].split('.')[-1] | |
module = importlib.import_module( | |
'.'.join(args[0].split('.')[:-1]) | |
) | |
task = getattr(module, task_name).s( | |
**kwargs['kwargs'] | |
).apply() | |
if task.status == 'FAILURE': | |
raise Exception( | |
"{}.{} {}".format( | |
module, | |
task_name, | |
task.result | |
) | |
) | |
mock.patch.objects( | |
celery.current_app, | |
'send_task', | |
side_effect=send_Task_applym | |
return_value=True | |
).start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment