|
2010-05-23 Replacing modules for testing (and fun)A while back I wrote about how I monkey-patch Python modules for testing, selectively replacing various functions they provide with test ones that return the results I like. But it's not the only way to do this. If monkey-patching system modules this way makes you nervous (perhaps due to its potential side effects), there's another way to do it: you can entirely replace the system module itself in the module you're testing. After all, (Note that neither sort of replacement deals very well with ' The replacement doesn't have to be a module, either; all you need is
something that has a namespace. The easiest thing to use is an instance
of a class, which also conveniently lets you put all of your fake module
functions and so on in one spot (and store state information). If you
want you can even give your class a (If you want to be sure that the module under test only calls things that you know about, you can restrict what gets forwarded.) There are advantages and drawbacks to each approach. Shimming module functions takes more work and has global effects, which may be good or bad depending on what you're testing. Replacing modules has strictly local effects and is probably simpler if you need to replace a bunch of things or keep state between your various replacement functions. Sidebar: an example fake
|
These are my WanderingThoughts GettingAround This is part of CSpace, and is written by ChrisSiebenmann. * * * Atom feeds are available; see the bottom of most pages. Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web |