I can only run my backend tests locally because all the instances of the mocked environment are created and into the actual db because of celery
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
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.