Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get django celery to write to the test database for my functional tests?

I am working on a Django application. We're using celery to queue writes to our Mongo database. I'm trying to write a functional test (using Selenium) for a function that queues something in celery.

The problem is that celery writes to the main Mongo database instead of the test database. How can I set up my functional tests to work with an instance of celery that writes to the test database?

We're using 'django_nose.NoseTestSuiteRunner' as our TEST_RUNNER.

UPDATE:

I haven't been able to figure out how to use another instance of celery for the tests, but I have found a way to bypass celery for the functional tests.

In my settings.py:

FUNC_TEST_COMMAND=['functional']
func_test_command = filter(lambda element: element in FUNC_TEST_COMMAND, sys.argv)
if len(func_test_command) > 0:
  CELERY_ALWAYS_EAGER = True

This mimics the behaviour of an AsyncResult without sending anything through a message queue when running the functional test suite. (See http://celery.readthedocs.org/en/2.4/configuration.html#celery-always-eager for more info.)

This solution is probably not ideal for functional tests, because it cuts out one of the application layers.

like image 737
rouan Avatar asked Oct 19 '12 06:10

rouan


People also ask

How do you write test cases for models in Django?

To write a test you derive from any of the Django (or unittest) test base classes (SimpleTestCase, TransactionTestCase, TestCase, LiveServerTestCase) and then write separate methods to check that specific functionality works as expected (tests use "assert" methods to test that expressions result in True or False values ...

How do you write unit test cases in Django?

Writing tests Django's unit tests use a Python standard library module: unittest . This module defines tests using a class-based approach. When you run your tests, the default behavior of the test utility is to find all the test cases (that is, subclasses of unittest.


1 Answers

Using CELERY_ALWAYS_EAGER = True does indeed bypass Celery's asynchornous processing. In order to write to the test database, you'll need to start your celeryd worker using the connection settings to the test database.

like image 106
alexdotc Avatar answered Nov 13 '22 22:11

alexdotc