首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pytest调用asyncio协程的函数

Pytest调用asyncio协程的函数
EN

Stack Overflow用户
提问于 2021-09-28 07:09:57
回答 1查看 156关注 0票数 0

我正在尝试使用mock和pytest-asyncio编写单元测试用例。我有一个使用asyncio.run启动异步协程的普通函数。使用python3.7

代码语言:javascript
复制
import asyncio

async def sample_async(arg2):
     # do something
     proc = await asyncio.create_subprocess_shell(arg2)
     # some more asyncio calls
     rc = proc.returncode
     return rc

def launcher(arg1, arg2):
    if arg1 == "no_asyncio":
        # do something
        print("No asyncio")
        return 0
    else:
        return_code = asyncio.run(sample_async(arg2))
        # do something
        return return_code

我可以为异步函数sample_async编写单元测试,但不能为launcher编写单元测试。以下是我尝试过的方法:

代码语言:javascript
复制
class AsyncMock(MagicMock):
    async def __call__(self, *args, **kwargs):
        return super(AsyncMock, self).__call__(*args, **kwargs)


@patch("asyncio.create_subprocess_shell", new_callable=AsyncMock)
def test_launcher(async_shell):
    arg1 = "async"
    arg2 = "/bin/bash ls"
    sample.launcher(arg1, arg2)
    async_shell.assert_called_once()

当我尝试运行pytest时,我总是得到RuntimeError: There is no current event loop in thread 'MainThread'.,我不能使用@pytest.mark.asyncio进行这个测试,因为函数launcher不是异步协程。我在这里做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2021-09-29 08:59:37

有关显式创建事件循环的信息,请参阅此处:

RuntimeError: There is no current event loop in thread in async + apscheduler

请在此处特别参阅get_event_loop()的文档:

https://docs.python.org/3/library/asyncio-eventloop.html

如果当前OS线程中没有设置当前事件循环,则OS线程是main,并且尚未调用set_event_loop(),则asyncio将创建一个新的事件循环并将其设置为当前事件循环。

很可能,因为您的操作系统线程不是主线程,所以您需要对循环进行一些手动初始化。确保你对事件循环和这些函数在其生命周期中所做的事情有一个清晰的心理图景,把循环想象成一个对象,它只是一个带有方法和其他对象(任务)的对象。正如文档所说,主操作系统线程为你做了一些“魔术”,但并不是真正的“幕后魔术”。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69357231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档