pythondjangodjango-nose

How to stop Tasks firing during Django Nose tests?


I have a testing which I use Django Nose. During a model creation I have a Celery task which is fired.

def save(self, *args, **kwargs):
   celery_app.send_task('test.apps.action',
                                 args=["BB",
                                       self.user.id,
                                       ], kwargs={})

However when I run my tests I don't want it to run the Celery task! How can I do something like....

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

   if running_not_nose_tests:

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

So that tests don't run the tasks?


Solution

  • 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