小编Ant*_*nko的帖子

异步方法的 Python 覆盖范围

我使用 aiohttp、pytest 和 pytest-cov 来覆盖我的代码。我想要获得更好的覆盖率<但现在我有点卡住了,因为事件简单代码没有显示 100% cov。比如这段代码:

@session_decorator()
async def healthcheck(session, request):
    await session.scalar("select pg_is_in_recovery();")
    return web.json_response({"status": "ok"})
Run Code Online (Sandbox Code Playgroud)

在覆盖率报告中,它显示返回的行未被覆盖。

我已阅读此链接异步方法的代码覆盖率。但这是针对 C# 的,我无法理解:

如果您正在等待的操作在等待之前已完成,则最常发生这种情况。

我的运行测试的代码:

python3.9 -m pytest -vv --cov --cov-report xml --cov-report term-missing
Run Code Online (Sandbox Code Playgroud)

我对上面代码的测试

async def test_healthz_page(test_client):
    response = await test_client.get("/healthz")
    assert response.status == HTTPStatus.OK
Run Code Online (Sandbox Code Playgroud)

python pytest python-3.x python-asyncio aiohttp

9
推荐指数
1
解决办法
2221
查看次数

标签 统计

aiohttp ×1

pytest ×1

python ×1

python-3.x ×1

python-asyncio ×1