user3595632
user3595632

Reputation: 5730

Django: check whether run as development or production when testing?

I'm using Django 1.9 and django.test for my unit test.

This is my code for testing signals:

@receiver(post_save, sender=EmailNotification, dispatch_uid="spacegraphy")
def post_save_notification(sender, instance, created, **kwargs):
    if created:
        if settings.DEBUG:
            print("post_save: created!!!")
        else:
            instance.send_notification()

When I run this application in local, it run as development mode, which shows print(settings.DEBUG) as True. (I checked it in shell_plus of django-extension

However, when I test my unit tests, print(settings.DEBUG) show False.

I have no idea why it happened. Any idea, please?

Upvotes: 1

Views: 1426

Answers (1)

ChidG
ChidG

Reputation: 3223

From https://docs.djangoproject.com/en/1.10/topics/testing/overview/#other-test-conditions:

Regardless of the value of the DEBUG setting in your configuration file, all Django tests run with DEBUG=False. This is to ensure that the observed output of your code matches what will be seen in a production setting.

I would suggest you write your test differently. The purpose is to test the functionality of the code as it would run in production, so there should be no need to check the value of DEBUG.

Upvotes: 2

Related Questions