Ant*_*nko 9 python pytest python-3.x python-asyncio aiohttp
我使用 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)
我在使用 asyncio 测试 FastAPI 代码时遇到了同样的问题。.coveragerc修复方法是在项目的根目录下创建或编辑包含以下内容的文件:
[run]\nconcurrency = gevent\nRun Code Online (Sandbox Code Playgroud)\n如果您使用 a,pyproject.toml您也可以将此部分包含在该文件中:
[tool.coverage.run]\nconcurrency = ["gevent"]\nRun Code Online (Sandbox Code Playgroud)\n此外,你还必须pip install gevent. 如果使用 Poetry,请运行poetry add --group dev gevent.
如果上面的\xe2\x80\x99不起作用,请尝试使用concurrency = thread,gevent(请参阅此评论。注意它说使用--concurrency,但当您使用时此选项不可用pytest-cov;您必须使用.coveragerc)。
| 归档时间: |
|
| 查看次数: |
2221 次 |
| 最近记录: |