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.

classification
Title: patch object as argument should be explicit
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: CendioOssman, cjw296, lisroach, mariocj89, michael.foord, xtreak
Priority: normal Keywords:

Created on 2021-05-06 06:43 by CendioOssman, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg393062 - (view) Author: Pierre Ossman (CendioOssman) Date: 2021-05-06 06:43
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.
msg393072 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2021-05-06 08:10
In my test cases I have ended up just ignoring the mock object with a placeholder if it's not needed. For the given use case you can do this using patch objects at setUp and tearDown like in https://docs.python.org/dev/library/unittest.mock-examples.html#applying-the-same-patch-to-every-test-method . I feel introducing another argument increases the complexity on the existing patch api which already has a lot of options.

I have added others for their thoughts on this API change.


from unittest.mock import Mock, patch
from unittest import TestCase, main


class Foo:
    properties = []


class FooTest(TestCase):
    def setUp(self):
        patcher = patch(f"{__name__}.Foo.properties", new_callable=list)
        self.addCleanup(patcher.stop)
        patcher.start()

    def test_foo(self):
        Foo.properties.append(1)
        self.assertEqual(Foo.properties, [1])

    def test_bar(self):
        Foo.properties.append(2)
        self.assertEqual(Foo.properties, [2])


if __name__ == "__main__":
    main()
msg393084 - (view) Author: Pierre Ossman (CendioOssman) Date: 2021-05-06 11:46
I've always been cautious about running patch() manually since it was easy to miss the cleanup. But those fears might be irrelevant these days when we have addCleanup().

Still, decorators are a more robust in more complex setups since you don't have to worry about setUp() being properly called in every base class. So I still think this would be interesting.

A different function might be an option to avoid adding arguments. Just like there are a few special patch.*() already.
History
Date User Action Args
2022-04-11 14:59:45adminsetgithub: 88218
2021-05-06 11:46:48CendioOssmansetmessages: + msg393084
2021-05-06 08:10:14xtreaksetversions: + Python 3.11
nosy: + lisroach, cjw296, michael.foord, xtreak, mariocj89

messages: + msg393072

type: behavior
2021-05-06 06:43:06CendioOssmancreate