Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing with Django in Celery 3.1.11?

I am using Celery 3.1.11 with 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)

I am attempting to run unit tests through manage.py:

>> python manage.py test calculationApp

in my tests.py for calculationApp I create the task:

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:

/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.

I was reading Celery 3.0 Docs - Unit Testing, which told me to set.

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

Is this still valid for celery 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.

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
like image 439
skullkid Avatar asked Jun 13 '14 16:06

skullkid


1 Answers

Are you trying to test the actual calculation logic or the fact that Celery works? 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. If you insist on testing both together, just setting CELERY_ALWAYS_EAGER to True only in unit test context should solve your problem.

You dont need to test Celery, it works and it has its own test suite to prove it.

like image 174
Sorin Neacsu Avatar answered Sep 23 '22 02:09

Sorin Neacsu