This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author CendioOssman
Recipients CendioOssman
Date 2021-05-06.06:43:05
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1620283386.4.0.215210268869.issue44052@roundup.psfhosted.org>
In-reply-to
Content
Right now if you use unittest.mock.patch() as a decorator it may or may not pass the object as an argument to the test function. The behaviour is a side effect of the argument "new" rather than something the caller can explicitly control.

In many cases this gives the desired behaviour, but not in all. So this behaviour should be possible to explicitly controlled.

One common case is when using patch() as a class decorator. If you want to avoid getting extra arguments to every test function, then "new" has to be specified. But that means that it will be the same object that will be used for every test, not a fresh one.

E.g.

@patch('foo.bar.baz', [])
class TestCases(unittest.TestCase):
    def test_a(self):
        ...
    def test_b(self):
        ...
    def test_c(self):
        ...

The tests will now be running with the same list in "foo.bar.baz" rather than a fresh empty list for each run. Ideally we could instead specify:

@patch('foo.bar.baz', new_callable=list, as_arg=False)
class TestCases(unittest.TestCase):
    def test_a(self):
        ...
    def test_b(self):
        ...
    def test_c(self):
        ...

Or something along those lines.
History
Date User Action Args
2021-05-06 06:43:06CendioOssmansetrecipients: + CendioOssman
2021-05-06 06:43:06CendioOssmansetmessageid: <1620283386.4.0.215210268869.issue44052@roundup.psfhosted.org>
2021-05-06 06:43:05CendioOssmanlinkissue44052 messages
2021-05-06 06:43:05CendioOssmancreate