[New-bugs-announce] [issue42159] AsyncMock restores stopped patch if same object stopped multiple times

Lisa Roach report at bugs.python.org
Mon Oct 26 17:05:24 EDT 2020


New submission from Lisa Roach <lisaroach14 at gmail.com>:

Hard to explain in the title, easier to see via a test case:

     async def async_func():
         raise Exception

    def test_simultaneous_mocks(self):
        class Test(IsolatedAsyncioTestCase):
            async def test_test(self):
                patcher1 = patch(f"{__name__}.async_func")
                patcher2 = patch(f"{__name__}.async_func")
                patcher1.start()
                await async_func()
                patcher2.start()
                await async_func()
                patcher1.stop()
                with self.assertRaises(Exception):
                    await async_func()
                patcher2.stop()
                with self.assertRaises(Exception): # Fails, mock is restored!
                    await async_func()

        test = Test("test_test")
        output = test.run()
        self.assertTrue(output.wasSuccessful()) # Fail


Calling stop() on the second patch actually restores the mock and causes the test to fail.

----------
assignee: lisroach
messages: 379687
nosy: lisroach
priority: normal
severity: normal
status: open
title: AsyncMock restores stopped patch if same object stopped multiple times
versions: Python 3.10, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42159>
_______________________________________


More information about the New-bugs-announce mailing list