[TIP] mock os.walk

Andrea Crotti andrea.crotti.0 at gmail.com
Thu Mar 22 10:33:56 PDT 2012

On 03/22/2012 03:30 PM, Michael Foord wrote:
> If you apply patch to a class it is the same as applying it to every 
> test method in the class. So although it may look like you were only 
> using it once you were actually calling it several times.
> How would you expect Mock to be able to deal with this - you have 
> given it an iterator object as the return value and it has no way of 
> knowing that you actually want a *new* iterator every time it is called.
> I think it's really just a question of understanding the tools and 
> what they're doing. Mock is flexible enough to allow you to return a 
> different object every time it is called (using side_effect), or you 
> can have patch create a new mock for every test. They will only do 
> what you tell them though.
> In your place I would prefer to have patch create the mock for you, 
> there's less danger of problems caused by re-using mocks.
> Michael

Sure I don't expect mock to understand my desires, today was my first 
day that I actually started to use it so
I am still in "banging my head against the wall"-mode, but hopefully it 
will be over soon ;)

Creating a new mock every time seems like a good idea yes, thanks

More information about the testing-in-python mailing list