Unit testing using Django in celery 3.1.11?

I am using Celery 3.1.11 with Django 1.6. I searched, but I cannot find many questions from the recent past about celery. (3.1)

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

>> python manage.py test calculationApp 

in my tests.py for calculatingApp I create a 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 unit test, the test gets stuck in the test and is never ready (it never passes 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 should double CTRL + C.

I read Celery 3.0 Docs - Unit Testing , which I was asked to install.

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

Is it still relevant for celery 3.1.11? I cannot find the relevant documentation for Celery 3.1 on Django unit testing, and I'm not sure if these parameters help or hurt, since the backend for the task returns none when I have this set, but the calculations seem to be actually performed .

When I delete these two lines from the settings file, I get the following 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 
+6
source share
1 answer

Are you trying to check the actual logic of calculation or the fact that celery works? These are two different things, and in my opinion, you should try your best to test your logic in isolation. This is faster, more future proof (that if you decide to replace Celery with something else) and simplifies a lot. If you insist on testing together, just set CELERY_ALWAYS_EAGER to True only in the context of unit test to solve your problem.

You do not need to check celery, it works, and it has its own test suite to prove it.

+2
source

Source: https://habr.com/ru/post/970806/


All Articles