我的代码中有一些异步调用。像这样:
send_message_to_install.delay(install, alert.message, badge, code,
campaign=alert.campaign, **kwargs)
我对获取任何结果不感兴趣,在运行测试时同步执行该函数非常重要 - 不会抛出错误。
测试时,我尝试将设置文件中的CELERY_ALWAYS_EAGER设置为True:
if "test" in sys.argv:
CELERY_ALWAYS_EAGER = True
我也尝试在测试中设置它:
from django.conf import settings
settings.CELERY_ALWAYS_EAGER = True
两次尝试都会让我的测试抛出错误(没有“.delay”它可以正常工作):
File "/opt/src/slicephone/cloud/users/api.py", line 520, in send_alert
campaign=alert.campaign, **kwargs)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/celery/app/task.py", line 357, in delay
return self.apply_async(args, kwargs)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/celery/app/task.py", line 463, in apply_async
options = router.route(options, self.name, args, kwargs)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/celery/app/routes.py", line 44, in route
route = self.lookup_route(task, args, kwargs)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/celery/app/routes.py", line 77, in lookup_route
return _first_route(self.routes, task, args, kwargs)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/celery/utils/functional.py", line 196, in _matcher
answer = getattr(maybe_promise(obj), method)(*args, **kwargs)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/kombu/utils/functional.py", line 50, in maybe_promise
return value.evaluate()
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/celery/utils/functional.py", line 162, in evaluate
self._value = super(mpromise, self).evaluate()
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/kombu/utils/functional.py", line 22, in evaluate
return self._fun(*self._args, **self._kwargs)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/celery/utils/imports.py", line 48, in instantiate
return symbol_by_name(name)(*args, **kwargs)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/kombu/utils/__init__.py", line 80, in symbol_by_name
module = imp(module_name, package=package, **kwargs)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named tasks
谢谢,Kev
答案 0 :(得分:0)
愚蠢的我。我的CELERY_ROUTES中有一个'messages.tasks'。但没有文件/messages/tasks.py。
答案 1 :(得分:0)
您可以使用CeleryTestRunner进行测试。 它将自动以ALWAYS_EAGER模式运行您的测试