1π
I was able to resolve this problem with the following steps:
- I created a new settings file which inherited from the base settings file. i.e
from settings import *
- Then I deleted the
DEFAULT_THROTTLE_RATES
key i.edel REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]
- Next thing I did was to point to the new settings file in
pytest.ini
i.eDJANGO_SETTINGS_MODULE="new_settings.py"
Now the tests will use the new settings file
1π
First you need to create a way to differentiate between test env and otherwise. Like we do for PROD and DEV using settings.DEBUG
config.
My recommendation is to create an env variable test=True
and then in your settings.py
write β
if os.environ.get("test", False):
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
],
'DEFAULT_THROTTLE_RATES': {
'anon': '100/day',
'user': '1000/day'
}
}
else it does nothing, and drf will not throttle.
- [Django]-Django 1.11: post form data to database
- [Django]-How to get rid of spacing in SimpleDocTemplate(). Python. ReportLab
- [Django]-Archiving Django models
- [Django]-Django REST Framework: Could not resolve URL for hyperlinked relationship using view name
1π
@ra123 has the right idea in general. As another approach, with all Django projects I add something like this to my settings/__init__.py
(or just settings.py
if you do a one file thing). It looks at argv
to see if its in test mode
IS_TESTING = bool(set(sys.argv[:2]) & {"pytest", "test", "jenkins"})
REST_FRAMEWORK = { "YOUR_CONFIG": "..." }
# at the very very end, AFTER your settings are loaded:
if IS_TESTING:
# override your rest framework settings in test mode
REST_FRAMEWORK["DEFAULT_THROTTLE_CLASSES"] = []
# some other handy things, for making tests faster/easier
PASSWORD_HASHERS = ("django.contrib.auth.hashers.MD5PasswordHasher",)
EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend"
DEFAULT_FILE_STORAGE = "inmemorystorage.InMemoryStorage"
I ended up with it this way so we donβt have to worry about it ever getting the wrong settings. It also helps keep things centralized, so (for example) you donβt call sentry.init
in testing mode, even if there is a sentry_url
in the environment.
- [Django]-Django test set created_at field
- [Django]-Django-CKeditor: How to show RichTextFields in templates
- [Django]-ModuleNotFoundError: No module named 'ebcli'
- [Django]-Spotify API authentication with Python
- [Django]-SIGWINCH ignored. Not daemonized