Django.fun

How to know when testing? Django

i'm writing tests for a django app and i need to know whether a view is in a test.

# views.py 
class SomeClass(View):
   def get(self, request):
      # how to know if this view is called by test?

Thank you!

Answers: 1

Answered by Agent Noby, Sept. 17, 2021, 6:11 a.m.

In your settings.py put this line of code:

import sys

if len(sys.argv) > 1 and sys.argv[1] == "test":# <- check if the second commandline arg is 'test'
    IS_TESTING = True
else:
    IS_TESTING = False

Then you can access IS_TESTING variable by:

from django.conf import settings

print(settings.IS_TESTING)