"Never awaited" warning in Unit test

Viewed 558

I'm trying to write unit-test on async function which use to fetch data from API and process some values using those fetched results.

@pytest.mark.asyncio
async def test_get_score_calls(monkeypatch, set_env_var):

    m = mock.AsyncMock()
    m.side_effect = [
        await get_sugar_api_records_for_calls_main(url, access_token, payload), 
        await get_sugar_api_records_for_calls_sub(url, access_token)
    ]
    monkeypatch.setattr(calls, 'get_sugar_api_records', m)

    calls.get_sugar_api_records(url, access_token, payload)
    calls.get_sugar_api_records(url, access_token)
    assert m.call_args_list[0] == mock.call(url, access_token, payload)
    assert m.call_args_list[1] == mock.call(url, access_token)

    ##-# mocking the "scevents_time" function
    mock_scevents_time = mock.MagicMock(name = 'Mock Method: mock_scevents_time')
    mock_scevents_time.return_value = scevents_time_return_value_valid
    monkeypatch.setattr(calls, 'scevents_time', mock_scevents_time)

    ##-# mocking get_none_overlapping_duration funtion
    mock_get_none_overlapping_duration = mock.MagicMock(name = 'Mock Method: mock_get_none_overlapping_duration')
    mock_get_none_overlapping_duration.return_value = 15
    monkeypatch.setattr(calls, 'get_none_overlapping_duration', mock_get_none_overlapping_duration)

    assert await calls.get_calls('1', '2021-9-9', [[0, 0], [23, 59]], -7.0, 'sugar.domain.org', access_token) == expected_result_call_score

Line 163-168 I'm trying to mock 'get_sugar_api_records' function for two different time because it used in two places for different kind of API calls, and also get_sugar_api_records is a async function which use httpx library

Line 170 -173 I'm calling get_sugar_api_records to make sure all function arguments are using when I ruining the test file. Also this is the place that warning coming.

To avoid the waring which is: was never awaited I did some change on code like following on Line 170-171 by adding await in-front of the code.

await calls.get_sugar_api_records(url, access_token, payload)
await calls.get_sugar_api_records(url, access_token)

But it didn't work as i expected and return another error which is: "StopAsyncIteration"

Line 175 -183 In here I'm mocking another two functions(scevents_time, get_none_overlapping_duration) which uses on the main function

This is the warning message and the final result I'm receiving after I ran the Test


    ======================================================== warnings summary ========================================================
        test/test_base/test_services/calls_score_test.py::test_get_score_calls
          /home/anub/PROD-DEV/L2-SUGAR/l2-sugar/test/test_base/test_services/calls_score_test.py:170: RuntimeWarning: coroutine 'AsyncMockMixin._execute_mock_call' was never awaited
            calls.get_sugar_api_records(url, access_token, payload)
        
        test/test_base/test_services/calls_score_test.py::test_get_score_calls
          /home/anub/PROD-DEV/L2-SUGAR/l2-sugar/test/test_base/test_services/calls_score_test.py:171: RuntimeWarning: coroutine 'AsyncMockMixin._execute_mock_call' was never awaited
            calls.get_sugar_api_records(url, access_token)
        
        -- Docs: https://docs.pytest.org/en/stable/warnings.html

0 Answers
Related