1

I have the following class and I'm writing a test on method2.

y.py

class C:
    @classmethod
    async def method1(cls):  # to be mocked
        pass

    @classmethod
    async def method2(cls):  # to be tested
        await cls.method1()

I have the following

from asyncio import Future
from unittest.mock import Mock
import pytest
from y import C

@pytest.mark.asyncio
async def test_method2():
    C.method1 = Mock(return_value=Future())
    await C.method2()
    C.method1.assert_called_once()

However, it hangs when running python -m pytest test.py?

ca9163d9
  • 27,283
  • 64
  • 210
  • 413

1 Answers1

0

You are testing async function here. Therefore you should use AsyncMock. With async_mock you could than use awaited_onced instead of called once. You code would look like

from unittest.mock import AsyncMock
@pytest.mark.asyncio
async def test_method2():
    C.method1 = AsyncMock()
    await C.method2()
    C.method1.assert_awaited_once() 
Simon Hawe
  • 3,968
  • 6
  • 14