Btw, not all settings can be patched just by overriding values because
some of them are cached and the change of the value doesn't change
django behaviour (e.g. TEMPLATE_CONTEXT_PROCESSORS option behaves like
this).

On 5 ноя, 02:23, Łukasz Rekucki <[email protected]> wrote:
> Funny, I had exactly the same problem today at work while refactoring
> my application's tests suite :).
>
> Currently, I'm using a pair of save/restore functions: save() monkey
> patches the settings module and returns a dictionary of old values,
> restore() puts back the old values based on the dictionary. I usually
> put this in setUp/tearDown so I don't have to repeat in every test. I
> was about to propose that
> Django's TestCase should do something similar by default.
>
> Both the decorator and context processor are very useful, but having
> something to set values for the whole test case instead of a single
> test or a block of code would be great too. I was thinking about
> something in line of:
>
>     class EmailTestCase(TestCase):
>         settings = dict(DEFAULT_FROM_EMAIL="[email protected]")
>
> On 4 November 2010 21:11, David Cramer <[email protected]> wrote:
>
>
>
>
>
> > With a decorator approach here's what I whipped up:
>
> > (This is dry code)
>
> > def with_settings(**overrides):
> >    """Allows you to define settings that are required for this
> > function to work"""
> >    NotDefined = object()
> >    def wrapped(func):
> >       �...@wraps(func)
> >        def _with_settings(*args, **kwargs):
> >            _orig = {}
> >            for k, v in overrides.iteritems():
> >                _orig[k] = getattr(settings, k, NotDefined)
>
> >            try:
> >                func(*args, **kwargs)
> >            finally:
> >                for k, v in _orig.iteritems():
> >                    if v is NotDefined:
> >                        delattr(settings, k)
> >                    else:
> >                        setattr(settings, k, v)
> >        return _with_settings
> >    return wrapped
>
> > I'm not familiar with the context managers, but I imagine those would
> > solve things like adjusting CONTEXT_PROCESSORS.
>
> > On Thu, Nov 4, 2010 at 1:06 PM, Dan Fairs <[email protected]> wrote:
>
> >>> Let me start with an example test:
>
> >>> def test_with_awesome_setting(self):
> >>>    _orig = getattr(settings, 'AWESOME', None)
> >>>    settings.AWESOME = True
>
> >>>    # do my test
> >>>    ...
>
> >>>    settings.AWESOME = _orig
>
> >> Pedant: there's a small bug above which has bitten me before doing a 
> >> similar thing - settings.AWESOME  ends up set to None after the test has 
> >> run if it didn't exist before.
>
> >>> Anyways, I'd love to hear how others have dealt with this and any
> >>> other possible solutions.
>
> >> I've used Michael Foord's Mock library to patch a setting for the duration 
> >> of a test case. Chris Withers' testfixtures library also has some sugar to 
> >> provide a context manager approach, though I haven't used that in a little 
> >> while.
>
> >> Cheers,
> >> Dan
>
> >> --
> >> Dan Fairs | [email protected] |www.fezconsulting.com
>
> >> --
> >> You received this message because you are subscribed to the Google Groups 
> >> "Django developers" group.
> >> To post to this group, send email to [email protected].
> >> To unsubscribe from this group, send email to 
> >> [email protected].
> >> For more options, visit this group 
> >> athttp://groups.google.com/group/django-developers?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "Django developers" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to 
> > [email protected].
> > For more options, visit this group 
> > athttp://groups.google.com/group/django-developers?hl=en.
>
> --
> Łukasz Rekucki

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to