django-override-settings
django-override-settings provides an easy way to override settings in Django tests.
The override_settings class can be used as either a class or method decorator or as a context manager to temporarily override the values of settings. After each test case has finished (when using it as a decorator) or after the context manager has exited, it resets the values in django.conf.settings to what they were before. This prevents side-effects from creeping in and lets each test case run in its own sandbox.
This package also provides two convenience functions (with_apps and without_apps) to modify just INSTALLED_APPS as well as a special object (SETTING_DELETED) to run tests without a given setting defined.
The functionality in this package will eventually be superseded when Django 1.4 is released as it will come with a built-in override_settings. But for those maintaining pre-1.4 codebases, hopefully this package comes in handy.
Installation
We’re on PyPI:
pip install django-override-settings
Usage
If you have a bunch of tests that require a given setting, you can decorate the class and each test case will use that value. For example:
from django.conf import settings
from django.test import TestCase
from override_settings import override_settings
@override_settings(FOO="abc")
class TestFoo(TestCase):
def test_foo(self):
self.assertEqual(settings.FOO, "abc")
Or you can decorate a single test case and have it only apply on that method:
@override_settings(BAR="123")
class TestBar(TestCase):
@override_settings(BAR="abc")
def test_bar(self):
self.assertEqual(settings.BAR, "abc")
def test_bar_no_decoration(self):
self.assertEqual(settings.BAR, "123")
You can also use it as a context manager:
class TestBar(TestCase):
@override_settings(BAR="123")
def test_bar(self):
self.assertEqual(settings.BAR, "123")
with override_settings(BAR="abc")
self.assertEqual(settings.BAR, "abc")
self.assertEqual(settings.BAR, "123")
To modify just INSTALLED_APPS, use with_apps or without_apps:
from override_settings import with_apps, without_apps
class TestAppModifiers(TestCase):
@with_apps('django.contrib.humanize')
def test_humanize(self):
# ...
@without_apps('django.contrib.sites')
def test_no_sites(self):
# ...
To run tests without a setting, use SETTING_DELETED:
from override_settings import override_settings, SETTING_DELETED
class TestMissingSetting(TestCase):
@override_settings(CUSTOM_OPTION=SETTING_DELETED)
def test_delete_custom_option(self):
"""
Useful to make sure a missing setting raises an Exception.
"""
self.assertRaises(AttributeError, getattr, settings, 'CUSTOM_OPTION')
Requirements
Django >= 1.2
Thanks
Jannis Leidel for both the original snippet and his work updating it to work when decorating TestCases as part of Django proper.
Joost Cassee for the idea of SETTING_DELETED as well as with_apps and without_apps as part of his django-analytical project.
Contact
If you notice any bugs, please open a ticket.
Metadata
Release files for django-override-settings 1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-override-settings-1.2.tar.gz | 4.5 kB | Without provenance | Details for django-override-settings-1.2.tar.gz |
Release files / django-override-settings-1.2.tar.gz
| Download URL | django-override-settings-1.2.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
375f0d86c199436054a22e7933fc0e9e59affb6dc8fea0a726de3850aa1d9de8
|
|
BLAKE2b-256 checksum How to use checksums |
e26f63f10bab3d1435c2848f1b3a3c91c9bfd40f627ad95bb3c1a81040478fc8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for django-override-settings 1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.