rouan
rouan

Reputation: 7279

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.

Upvotes: 8

Views: 1049

Answers (2)

alexdotc
alexdotc

Reputation: 268

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.

Upvotes: 1

Filip Dupanović
Filip Dupanović

Reputation: 33670

I'd suggest that you take a look at LiveServerTestCase if your using an automated test client for running functional tests.

Then make sure you have a separate settings module your running your tests with that properly configures Celery to use your project's database for transport.

Upvotes: 0

Related Questions