Python asynctest mock patch decorator spilling into subsequent tests

610 views Asked by At

I am trying to test an async function. For this i am using pytest-asyncio and asynctest. I need to check how many times a function that is being used inside the function that i am testing is called. For this i am mocking the internal function using aynsctest.mock.patch(). My test file looks like the following:

@pytest.mark.asyncio
@asynctest.mock.patch('my_module.function_name')
async def test_function_called_twice(function_mock):
    await function_to_be_tested()
    assert function_mock.call_count == 2

@pytest.mark.asyncio
@asynctest.mock.patch('my_module.function_name')
async def test_function_called_once(function_mock):
    await function_to_be_tested()
    assert function_mock.call_count == 1

If i run the test cases separately, they pass. However, when I run the entire module, the second test case to run fails - regardless of the order they're run in. I suspect this is because the mock from the first test case still affects the second test case to run.

What am i doing incorrectly here?

0

There are 0 answers