测试TaskResult芹菜任务失败

时间:2017-12-11 16:29:23

标签: python django-celery

我正在尝试在我的项目中使用django_celery_results。我在add中创建了一个任务add_new(x, y)和一个函数tasks.py

from celery import shared_task
from django_celery_results.models import TaskResult

@shared_task(bind=True)
def add(self, x, y):
    return(x, y)

def add_new(x, y):
    task = add.delay(x, y)
    task_result = TaskResult(task_id=task.task_id)
    return task_result.as_dict()

我正在test_tasks.py测试我的功能,如下所示:

from tasks import add, add_new
from nose.tools import eq_
from django.test import override_settings


def test_add_task():
    result = add.apply(args=(4, 4,)).get()
    eq_(result, 8)


@override_settings(CELERY_TASK_ALWAYS_EAGER=True)
def test_add_new_task():
    result = add_new(4, 4)
    eq_(result['result'], 8)

第一次测试通过,但第二次测试失败:

AssertionError: None != 8

查看TaskResult个对象列表,不存在最新任务。重复测试,之前的测试中的task_id就在那里,但是最新的测试失败并且测试失败了。因此,即使任务成功完成,我仍然认为数据库未更新。尝试在shell中执行函数addadd_new,它完全正常。只用鼻子运行测试不起作用。我错过了什么?

0 个答案:

没有答案