[TIP] fresh fixture on module functions
titus at caltech.edu
Sun Mar 4 17:15:32 PST 2007
On Sun, Mar 04, 2007 at 03:47:01PM -0800, Grig Gheorghiu wrote:
-> --- 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
Check out Dependency Injection,
Python makes it easier than other languages, so you can do things like
assign/override new names in existing modules.
Hmm, here's a neat idea for testing functionality: a per-module module
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.
Probably people are doing this already...
More information about the testing-in-python