简体   繁体   English

在Celery 3.1.11中使用Django进行单元测试?

[英]Unit Testing with Django in Celery 3.1.11?

I am using Celery 3.1.11 with Django 1.6. 我将Celery 3.1.11与Django 1.6结合使用。 I've been searching but I'm not able to find a lot of questions from the recent past about celery.(3.1) 我一直在搜索,但是最近找不到关于芹菜的很多问题。(3.1)

I am attempting to run unit tests through manage.py: 我正在尝试通过manage.py运行单元测试:

>> python manage.py test calculationApp

in my tests.py for calculationApp I create the task: 在我的tests.py for CalculationApp中,我创建了任务:

c = calculateCarbon.delay(project.id)
r = AsyncResult(c.id).ready()
print "c.backend: %s" % (c.backend)

print "AsyncResult(c.id).ready(): %s" % (r)
print "AsyncResult(c.id).state: %s" % (AsyncResult(c.id).state)
print "AsyncResult(c.id).result: %s" % (AsyncResult(c.id).result)   

while not r:
    r = AsyncResult(c.id).ready()

When I run the unit test, the test gets stuck in the test and is never ready (it never gets past the while loop), I get this as output: 当我运行单元测试时,测试陷入测试之中,并且从未准备就绪(它永远不会超过while循环),我将其作为输出:

/usr/lib/python2.7/dist-packages/numpy/core/_methods.py:96: RuntimeWarning: invalid value encountered in double_scalars
ret = ret / float(rcount)

c.backend: None
AsyncResult(c.id).ready(): False
AsyncResult(c.id).state: PENDING
AsyncResult(c.id).result: None

At this point I have to CTRL+C twice. 此时,我必须两次按CTRL + C。

I was reading Celery 3.0 Docs - Unit Testing , which told me to set. 我正在阅读Celery 3.0 Docs-单元测试 ,告诉我进行设置。

CELERY_ALWAYS_EAGER = True
TEST_RUNNER = 'djcelery.contrib.test_runner.CeleryTestSuiteRunner'

Is this still valid for celery 3.1.11? 这对于芹菜3.1.11仍然有效吗? I can't find any relevant documentation for Celery 3.1 about Django unit testing, and I'm not sure if these settings are helping or hurting as the backend for the task returns none when I have these set, but the calculations appear to actually execute. 我找不到关于Django单元测试的Celery 3.1的任何相关文档,并且我不确定这些设置是否对我们有帮助或不利,因为当我设置了这些设置时,任务的后端什么也没有返回,但是计算似乎可以实际执行。

When I remove these two lines from the settings file I get these results: 当我从设置文件中删除这两行时,会得到以下结果:

c.backend: <celery.backends.amqp.AMQPBackend object at 0x7a4de50>
AsyncResult(c.id).ready(): False
AsyncResult(c.id).state: PENDING
AsyncResult(c.id).result: None
AsyncResult(c.id).ready(): True
AsyncResult(c.id).state: FAILURE
AsyncResult(c.id).result: task args must be a list or tuple

======================================================================
FAIL: test_calculations (measuring.tests.TestCalculations)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/var/www/project/calculationApp/tests.py", line 70, in test_calculations
self.assertEqual(int(number.attribute), 2212)
AssertionError: 0 != 2212

----------------------------------------------------------------------
Ran 1 test in 2.765s

Are you trying to test the actual calculation logic or the fact that Celery works? 您是要测试实际的计算逻辑还是Celery可以工作的事实? That's two different things, and in my opinion you should be trying really hard to test your logic in isolation. 那是两件事,我认为您应该非常努力地孤立地测试逻辑。 It's faster, more future proof (what if you decide to replace Celery with something else) and simplifies things alot. 它更快,更可靠(如果您决定用其他东西代替Celery的话),并简化了很多事情。 If you insist on testing both together, just setting CELERY_ALWAYS_EAGER to True only in unit test context should solve your problem. 如果您坚持同时测试两者,只需在单元测试上下文中将CELERY_ALWAYS_EAGER设置为True即可解决您的问题。

You dont need to test Celery, it works and it has its own test suite to prove it. 您不需要测试Celery,它可以工作,并且它具有自己的测试套件来证明它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM