testbook icon indicating copy to clipboard operation
testbook copied to clipboard

support `async` function calls

Open BugDiver opened this issue 3 years ago โ€ข 0 comments

Currently I'm having issues testing my async methods in notebook. Exmaple: Notebook:

import httpx


async make_api_call(client, url, headers, body)
    res = await client.post(url, headers=headers, json=body)
    if res.status_code == 200:
        return res.json()
    else:
        print(res)


if __name__ == '__main__'
    with httpx.AsyncClient() as client:
        await make_api_call(cleint, url,... ..)

TestCase:

import unittest
from unittest.mock import MagicMock

class TestAPI(unittest.IsolatedAsyncioTestCase):
    async def test_api_call(self):
        with testbook(notebook_path, execute=True) as nb:
            make_api_call = nb.ref('make_api_call')
            cleint = MagicMock()
            await make_api_call(client,url, ...)
            client.post.assert_called()

Is there any way to test async function calls?

BugDiver avatar Mar 22 '22 09:03 BugDiver