Message395752
Dear maintainers,
I discovered an unexpected behavior when the `side_effect` of an `AsyncMock` includes an exception. The test case below fails but I expected it to pass:
```
import sys
import unittest
from unittest.mock import AsyncMock
class A:
async def foobar(self):
while True:
try:
return await self.mock()
except Exception:
continue
class TestA(unittest.IsolatedAsyncioTestCase):
async def test_refcount(self):
a = A()
a.mock = AsyncMock(side_effect=[Exception(), None])
refc = sys.getrefcount(a)
await a.foobar()
self.assertEqual(refc, sys.getrefcount(a))
if __name__ == "__main__":
unittest.main()
```
If `side_effect=[Exception(), None]` is changed to `side_effect=[None, None]` the test case pass.
I discovered this in a bigger codebase while debugging why a weakref.finalize did not trigger as expected. |
|
Date |
User |
Action |
Args |
2021-06-13 14:52:02 | penlect | set | recipients:
+ penlect, asvetlov, yselivanov |
2021-06-13 14:52:01 | penlect | set | messageid: <1623595921.99.0.250644393873.issue44410@roundup.psfhosted.org> |
2021-06-13 14:52:01 | penlect | link | issue44410 messages |
2021-06-13 14:52:01 | penlect | create | |
|