0

I want to create tests but every time I run a test it triggers celery and celery creates instances into my local db. that means that if I run those tests in the prod or dev servers, then it will create rubish there. maybe that will trigger other stuff and create problems into the db. how can I avoid all of that? how can I mock celery so it doesn't create troubles in the dev server or prod server while running tests?

I tried some mocking throuth the @override_settings but it didn't work actually as I would like

1 Answer 1

0

When you run tests, Django automatically creates a temporary test database and deletes it when tests finish, so nothing goes into your real database. To make Celery safe during tests, you can run it in eager mode, which makes tasks run instantly in memory instead of going to a real worker. Just add this to your test settings (for example in settings_test.py):

CELERY_TASK_ALWAYS_EAGER = True
CELERY_TASK_EAGER_PROPAGATES = True
CELERY_BROKER_URL = "memory://"
CELERY_RESULT_BACKEND = None

Then run your tests like this:

DJANGO_SETTINGS_MODULE=project.settings_test python manage.py test

This way, Celery tasks run instantly and the database used is temporary, so your dev and prod data stay completely safe.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.