Wednesday, 9 December 2020

Mock streaming API in python for unit test

I have an async function that calls a streaming api. What is the best way to write unit test for this function? The api response has to be mocked.

I tried with aiounittest and used mock from unittest. But this calls the actual api instead of getting the mocked response. Also tried with pytest.mark.asyncio annotation, but this kept giving me the error - coroutine was never awaited. I have verified that pytest-asyncio has been installed.

I am using VS Code and Python 3.6.6

Here is the relevant code snippet:

async def method1():
    response = requests.get(url=url, params=params, stream=True)
    for data in response.iter_lines():
        # processing logic here
        yield data


from Mock streaming API in python for unit test

No comments:

Post a Comment