Mock setting in Django

Write at 2019 Apr 03 in notes django mock

Sometimes I has to write certain thing that should never run in production. Such as some cleanup tasks for dev/qa.

I rely on certain APPENV to detect app environment. Imagine in setting file:

APPENV = os.environ.get("APPENV", "DEV")

from django.conf import settings
if settings.APPENV == "PROD":

To write test for these I need to change settings, and revert to its original value when finishing. Django has a great utility for that.

Enter override_settings. You use it like this:

from django.test.utils import override_settings
from django.conf import settings

def test_foo():
    # PROD

Small thing but very helpful

Want Email Forwarding For Your Domain? Check out Mailwip.