[Answer]-How to stop Tasks firing during Django Nose tests?

1๐Ÿ‘

โœ…

You got it half way there ๐Ÿ™‚ For tests, I usually just a global variable (settings parameter in Django case). or environment variable telling one should skip certain aspects of the application under the test. This only if there is no other way to community with the underlying code, like settings class variables or flags.

In settings โ€“ create a separate settings file for running the tests:

SKIP_TASKS = True

Then:

from django.conf import settings

def save(self, *args, **kwargs):

   if not settings.SKIP_TASKS:

       celery_app.send_task('test.apps.action')
   else:
       pass

Or from command line with environment variables:

SKIP_TASKS=true python manage.py test

import os

def save(self, *args, **kwargs):

   if not os.environ.get("SKIP_TASKS"):

       celery_app.send_task('test.apps.action')
   else:
       pass
๐Ÿ‘คMikko Ohtamaa

Leave a comment