Testing

Testing how your app behaves with different config values is achieved with the override_config class. This intentionally mirrors the use of Django’s override_setting.

class override_config(**kwargs)

Replaces key-value pairs in the config. Use as decorator or context manager.

Usage

It can be used as a decorator at the TestCase level, the method level and also as a context manager.

from constance import config
from constance.test import override_config

from django.test import TestCase


@override_config(YOUR_NAME="Arthur of Camelot")
class ExampleTestCase(TestCase):

    def test_what_is_your_name(self):
        self.assertEqual(config.YOUR_NAME, "Arthur of Camelot")

    @override_config(YOUR_QUEST="To find the Holy Grail")
    def test_what_is_your_quest(self):
        self.assertEqual(config.YOUR_QUEST, "To find the Holy Grail")

    def test_what_is_your_favourite_color(self):
        with override_config(YOUR_FAVOURITE_COLOR="Blue?"):
            self.assertEqual(config.YOUR_FAVOURITE_COLOR, "Blue?")

Pytest usage

Django-constance provides pytest plugin that adds marker @pytest.mark.override_config(). It handles config override for module/class/function, and automatically revert any changes made to the constance config values when test is completed.

pytest.mark.override_config(**kwargs)

Specify different config values for the marked tests in kwargs.

Module scope override

pytestmark = pytest.mark.override_config(API_URL="/awesome/url/")

def test_api_url_is_awesome():
    ...

Class/function scope

from constance import config

@pytest.mark.override_config(API_URL="/awesome/url/")
class SomeClassTest:
    def test_is_awesome_url(self):
        assert config.API_URL == "/awesome/url/"

    @pytest.mark.override_config(API_URL="/another/awesome/url/")
    def test_another_awesome_url(self):
        assert config.API_URL == "/another/awesome/url/"

If you want to use override as a context manager or decorator, consider using

from constance.test.pytest import override_config

def test_override_context_manager():
    with override_config(BOOL_VALUE=False):
        ...
# or
@override_config(BOOL_VALUE=False)
def test_override_context_manager():
    ...

Pytest fixture as function or method parameter ( NOTE: no import needed as fixture is available globally)

def test_api_url_is_awesome(override_config):
    with override_config(API_URL="/awesome/url/"):
        ...

Any scope, auto-used fixture alternative can also be implemented like this

@pytest.fixture(scope='module', autouse=True)  # e.g. module scope
def api_url(override_config):
    with override_config(API_URL="/awesome/url/"):
        yield

Memory backend

If you don’t want to rely on any external services such as Redis or database when running your unittests you can select MemoryBackend for a test Django settings file

CONSTANCE_BACKEND = 'constance.backends.memory.MemoryBackend'

It will provide simple thread-safe backend which will reset to default values after each test run.