I quite like the contextlib.contextmanager decorator as it makes it very easy to create a context manager to use with the 'with' statement. For some reason, I seem to mostly use that in tests when I want to replace parts of the system with test doubles. It looks something like:



@contextmanagerdef do_run_mocked(mock): orig_run = mod.do_run mod.do_run = mock yield mod.do_run = orig_run

And then I use it in a test like this:


read more



Läs mer...

Inlägget är automatiskt hämtat från www.ubuntu.com

2010-12-03 17:59