[TIP] mock os.walk
Andrea Crotti
andrea.crotti.0 at gmail.com
Thu Mar 22 08:24:03 PDT 2012
On 03/22/2012 03:13 PM, Michael Foord wrote:
>
> Well, it may or may not be desired - but it is what you have told mock
> to do. :-)
>
> If you're using mock 0.8 you can do:
>
> @patch('os.walk', return_value=iter(test_dir))
> def test(mock_walk):
> ...
>
> This creates a new mock instead of a pre-created one. If you really
> *want* to use a single mock for all your tests then you can use a
> side_effect to create and return a fresh iterator every time:
>
> my_mock = Mock(side_effect=lambda *a, **kw: iter(test_dir))
>
> HTH,
>
> Michael
>
Yes sure it makes sense, but actually it's not a problem because passing
the list
my_mock = Mock(return_value=test_dir)
works perfectly, so I don't need to create an extra function..
The annoying thing was that I got this problem also using the mock once,
*but* patching
the TestCase class instead of the single modules, and in that case I
think it's counter-intuitive..
More information about the testing-in-python
mailing list