[TIP] fresh fixture on module functions

Kumar McMillan kumar.mcmillan at gmail.com
Mon Mar 5 09:23:46 PST 2007


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

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.



More information about the testing-in-python mailing list