From 6f561aa017e6184f11e517fcfc1edb4940e282f6 Mon Sep 17 00:00:00 2001 From: NickGoog <66492516+NickGoog@users.noreply.github.com> Date: Mon, 27 Mar 2023 15:03:29 -0400 Subject: [PATCH] Add example of iterator in non-parameterized decorator More details in https://github.com/wolever/parameterized/issues/160 --- README.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.rst b/README.rst index 2afd207..8dc8689 100644 --- a/README.rst +++ b/README.rst @@ -334,6 +334,27 @@ into memory before the start of the test run (we do this explicitly to ensure that generators are exhausted exactly once in multi-process or multi-threaded testing environments). +This also applies to iterators or generators in other decorators. For example, +the following works for the first run but not the second: + +.. code:: python + + @parameterized.expand([('first run',), ('second run',)]) + @mock.patch.object(MyIterator, 'get_iterator', new=mock.Mock( + return_value=iter([1]))) + def test_foo(self, bar): + ... + +Workaround: + +.. code:: python + + @parameterized.expand([('first run',), ('second run',)]) + @mock.patch.object(MyIterator, 'get_iterator') + def test_foo(self, bar, mock_get_iterator): + mock_get_iterator.return_value = iter([1]) + ... + The ``@parameterized`` decorator can be used test class methods, and standalone functions: