override_settings not working #7806
Replies: 14 comments 1 reply
-
I'm running into this too, but for authentication. I'm guessing it's because of these lines: django-rest-framework/rest_framework/views.py Lines 96 to 104 in df77f7b Followup to #2466 ? |
Beta Was this translation helpful? Give feedback.
-
@crccheck that makes sense. If the class has already been defined, it has already accessed the relevant setting. Overriding the setting at a later date will have no effect on that view class. We should probably set those values to For now, a workaround would be to use a settings module specifically for the test environment. e.g., disable throttling, setting password hashers to only MD5, etc... |
Beta Was this translation helpful? Give feedback.
-
This introduces the possibility of running tests with settings that differ from the production environment (apart from the ones that we want to override) which is really undesirable in my opinion. |
Beta Was this translation helpful? Give feedback.
-
This gets into a larger discussion about how to manage your settings, but our typical pattern is:
With this pattern, our dev/test settings are usually ~5 lines of relatively inconsequential overrides. e.g, # application/settings.py
DEBUG = os.environ.get("DEBUG") == "true"
ALL_THE_SETTINGS = "..."
# tests/settings.py
from application.settings import *
EMAIL_BACKEND = "..." |
Beta Was this translation helpful? Give feedback.
-
In my case, I want some integration-style tests to run using the same DRF settings as production, but the rest to be more like unit-like tests where I'm trying to strip away boilerplate. I could do this with two test runs and different settings, but it'd be really nice if I could do this with It may be that adding special code to DRF just to support this case kills production performance. It'd make sense to not support it then. Maybe I can solve this with |
Beta Was this translation helpful? Give feedback.
-
@crccheck Have you gotten anywhere with this? |
Beta Was this translation helpful? Give feedback.
-
alas no, I already wrote the boilerplate so I haven't been motivated to re-visit this. Maybe my next DRF project. |
Beta Was this translation helpful? Give feedback.
-
Oof, I lost half a day to this - adding a simple throttle to my project for anonymous users caused half of my unit tests to fail with 429 errors. I followed @crccheck's hint and just manually adjusted the throttle rate by |
Beta Was this translation helpful? Give feedback.
-
Hi there; I'm running into this problem with
Although I expected this to override the settings to test my custom paginator; after looking into the code, I got to understand why this is not working. Is there any way to make it work? My second try was using a decorator in the test, so it would override the value before being called:
Again, looking into the code, this can't be expected. I made a few changes into
This works. As far as I can see, this is not the proper solution, as it only focuses on Thanks for your help. |
Beta Was this translation helpful? Give feedback.
-
Trying to implement a custom throttle class and running into this, just like OP. I think different settings values than production is necessary though, especially to test infrastructure additions like throttling or pagination on a full request. I don't want to rate-limit my unit tests :D I suppose, since these are declared in the class and not set at init time, they can't be replaced by a settings reload. For list
Hope this can get fixed at some time, since its making testing a lot of things nearly impossible. I've worked around this with a decorator, but this is only for throttling:
|
Beta Was this translation helpful? Give feedback.
-
I'm also facing this issue. I'm using Django-Q and need to set |
Beta Was this translation helpful? Give feedback.
-
@raywtham Use another setting file have |
Beta Was this translation helpful? Give feedback.
-
Just wanted to throw in my support for this. I, too, was having issues because if, for example, I can work my way around it, but it's not optimal. Also, using settings in this way is discouraged by Django, as per https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/coding-style/#use-of-django-conf-settings:
I'm circumventing this in my code by moving the import inside a function that's run much later on, instead of importing at top of my file. |
Beta Was this translation helpful? Give feedback.
-
Oh - perhaps to clarify - my problem wasn't really even with overriding settings - it's something that totally broke the python process if certain DRF files were loaded in a bit prematurely. So I'd def. see this as a larger problem than overriding settings during testing. Just to throw a bit more weight on the issue. Was wondering if I should file this as a separate issue, since it manifests very differently, but also wasn't sure if it would just be extra noise. |
Beta Was this translation helpful? Give feedback.
-
In my unit tests, I'm facing troubles with requests being denied because of throttle rates. By setting DEFAULT_THROTTLE_CLASSES to (), requests are no longer throttled. However, I only want to do this for unit tests, so I tried to use override_settings to override the DEFAULT_THROTTLE_CLASSES setting.
This does not work; I'm still getting 429's. How can I get this working?
Note: I'm using latest version of django-rest-framework.
Beta Was this translation helpful? Give feedback.
All reactions