我的代码中有一些异步调用。像这样:
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
谢谢,凯夫