2

I have a function like this:

async def my_func:
    try:
        rv = await some_other_func()
        raise Exception()
     except Exception:
        rv.close()

test:

   @pytest.mark.asyncio
   async def test_close()
        with mock.patch("module.some_other_func") as f:
            await my_func()
            f.close.assert_called_once()

I would like to test that my awaitable is closed in case of an exception. Unfortunately, the coroutine never registers in the MagiCmock object. Any ideas how to test?

Christian Sauer
  • 10,351
  • 10
  • 53
  • 85
  • Small addendum: Replacing the return_value with asynctest.CoroutineMock does not work either – Christian Sauer Sep 01 '20 at 12:45
  • Gaah,n could the person closing this question as a duplicate at least try if the "duplicate" solves this problem? – Christian Sauer Sep 04 '20 at 19:32
  • 1
    Sorry, didn't see the comment. Yes, I actually did try it. I would put that into an answer, but well... – MrBean Bremen Sep 07 '20 at 18:41
  • 1
    I used the `CoroMock` from that answer, and the test looked like `with mock.patch("my_module.my_func.some_other_func", new_callable=CoroMock) as f: await my_func() f.coro.return_value.close.assert_called_once()` – MrBean Bremen Sep 07 '20 at 18:44

0 Answers0