[TIP] fresh fixture on module functions
Michael Foord
fuzzyman at voidspace.org.uk
Mon Mar 5 10:06:30 PST 2007
Kumar McMillan wrote:
> On 3/4/07, Titus Brown <titus at caltech.edu> wrote:
>
>> import ObnoxiousModule
>> ObnoxiousModule = module_wrapper(ObnoxiousModule)
>>
>> # modify ObnoxiousModule all you want, and now the global
>> # ObnoxiousModule module won't be touched, e.g.
>>
>> ObnoxiousModule.A = mock_A
>>
>> # ObnoxiousModule.A is now module-local.
>>
>
> I think the problem with this is that the use case for accessing a
> module that you need to mock *only* in your test itself is limited.
> Most of the time the module attribute I need to mock has to be global
> for it to do what I want. Thus, enter the fairly lame idiom of:
>
> _saved_A = ObnoxiousModule.A
> ObnoxiousModule.A - mocked_A
> try:
> test_stuff()
> finally:
> ObnoxiousModule.A = _saved_A
>
>
This is a pattern we use quite often when patching at the module level.
(Although I assume that the '-' should be an '='.)
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
> I bet there are some mock libraries out there that simplify this
> approach but I use mocks so rarely that I haven't bothered to dig in.
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>
>
More information about the testing-in-python
mailing list