-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Q: overriding settings #13
Comments
Not built-in into behave-django's code base, as far as I know. But your code looks just like something that wants to be turned into a decorator. Would you mind preparing a pull request? (Don't shoot me for guessing!)Have you tried Django's EDIT: Oh, you said it didn't work. -- What was the problem then? |
I'm not sure how |
@farcepest are you still alive? |
Has there been any further guidance on that? Seems like it would be a handy thing to have, current solution is hacky at best! |
Reading @farcepest's #13 (comment) above I feel it should be easy to write a decorator that does exactly what the 3 lines of code above do. Kind of: from behave_django.decorators import override_settings
@override_settings(DEBUG=True)
def before_scenario(context, scenario):
pass That could be a handy addition. The implementation details of that decorator can then be improved incrementally as soon as someone has time to think of "a better solution". If the decorator worked both in the environment and the actual test implementations that would be cool (bonus points!). 🥇 Is anyone of you guys willing to contribute such a decorator in a PR? |
That would be fine for overriding settings for all scenarios, but I think the primary need is to override them per scenario. I've been struggling to find a way of writing a step that would achieve that -- this having to do with the state of the app, it would make sense to be able to use a 'Given' step. |
It's not easy. If you're using unittest, you can use |
Alright, so if that's the problem we have to reset the settings value in @override_settings(DEBUG=True)
def before_scenario(context, scenario):
pass
@override_settings(DEBUG=False)
def after_scenario(context, scenario):
pass If you don't like this, actually explicit, approach let's find a more elegant way to get this implemented. Am I missing something? And yes, the @farcepest I'm referring to your "It works" from above. You say, it works. So, why not implement this as a first attempt and then move on to improve the implementation incrementally. My point of view is, the code needs to be nice to read and self-explanatory. Because readability counts. A decorator is already better than the current situation, and it can hide the implementation details. Sorry if I'm being too pragmatic. Let's find a better way! |
It's a decorator... It temporarily changes the settings, runs the decorated function, then restores the settings. That just doesn't work on before_scenario, because your scenario still runs with the original settings. It is not at all obvious how to implement this with behave. "It works" applied to this:
and then you have to have similar code in after_scenario to undo what you did in before_scenario. It's not pretty, but it works. There doesn't seem to currently be a better solution for this currently. |
and I want to emphasize: |
No offence, please. Decorators are no magic, it's a wrapper function. If I do dirty stuff in its implementation the result will stay dirty, won't it? It may be meant to clean up after itself, but this is not automatic, right? I've not tried it (nor will I), so I'm really just trying to help us think: If overriding certain settings "the dirty way" won't have an effect, because the LiveServer instance is already running then we'll have to adapt the management command. In the worst case we may have to launch a separate LiveServer instance for each scenario (I hope that's not needed). Nothing is impossible, it may be hard though. Anyone willing to give it a shot? |
override_settings is specifically intended to temporarily change the settings for the duration of the wrapped class/method. Internally it's a context manager. https://docs.djangoproject.com/en/1.10/topics/testing/tools/#django.test.override_settings |
We should agree that we are talking about behave-django. I'm not referring to |
So right now, this is how I would do it. # some.feature
Feature: Change the settings for specific scenarios
@debug
Scenario: Change some settings here
Given I want to change DEBUG to True
Then DEBUG should be True # environment.py
from django.conf import settings
def before_tag(context, tag):
if tag == 'debug':
settings.DEBUG = True
def after_tag(context, tag):
if tag == 'debug':
settings.DEBUG = False This already isn't too bad, unless you need a bunch of different unique configurations. This is @bittner's proposal: @override_settings(DEBUG=True)
def before_scenario(context, scenario):
pass
@override_settings(DEBUG=False)
def after_scenario(context, scenario):
pass I don't think this would work though because you want to override the settings on a per Scenario basis. To make this better, maybe we'll go for: @override_settings('debug', DEBUG=True)
@override_settings('foo', FOO='some value')
def before_tag(context, tag):
pass
@restore_settings
def after_tag(context, tag):
pass First value to Though at this point, I'm liking the first approach more because it's less hacky, more explicit, and less magic, but that's just me. What do you guys think? |
First approach is the only viable one currently, IMHO; |
In the example I gave, |
Has behave-django seen changes in regards to this issue since? |
It makes sense to me for settings to work similar to how behave-django allows fixture loading, where behave-django would support both of the following options: Interface:Option 1: in
|
Is there a recommended way to override settings for the live server? Normally I'd use
@override_settings(DEBUG=True)
so I can see tracebacks while debugging, but had to settle for this:(and corresponding after_scenario())
It works, but is there a better way?
The text was updated successfully, but these errors were encountered: