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 at
> http://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 at
http://groups.google.com/group/django-developers?hl=en.