[TIP] fresh fixture on module functions

Grig Gheorghiu grig at gheorghiu.net
Sun Mar 4 15:47:01 PST 2007

--- Nate Lowrie <solodex2151 at gmail.com> wrote:

> I am currently writing unit tests for the dabo framework.  I have run
> into a test smell that I don't quite know how to approach.  I have
> several functions defined in a file called dColors. So, it would be:
> -----------------------
> def a():
>   do something...
> def b():
>   do something else...
> etc.
> -------------------------
> When I am testing I do import dColors at the beginning.  All of the
> functions are calculation functions so they could be easily tested.
> However, I noticed that there was a Fragile Test smell due to a
> persistent fixture.  I wanted to stub some of the module functions
> out
> so that I can do behavior verification on one function and avoid
> repeated test code.  

How about using the technique of overriding the namespace? Let's say
you have another module called mocked_dcolors.py which contains
stubs/mocks for each of the functions in dColors: mocked_a, mocked_b

Then in test_dcolors you would do:

import dColors

def test_a():
    from mocked_dcolors import mocked_b
    dColors.b = mocked_b

    # test a's behavior while using a mock/stub for b

See also how Michal is doing this in Cheesecake here:


(look at the mocked_urlretrieve section)


More information about the testing-in-python mailing list