问题标签 [pytest-asyncio]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
0 回答
66 浏览

mongodb - “NoneType”对象没有属性“get_collection”,FastAPI + Mongodb

我正在使用 FastAPI 对应用程序中一个用户的注册进行测试。当应用程序正在运行时,我可以完美地注册用户但是当我尝试使用 pytest 对相同的方法进行测试时,我遇到了问题,它无法识别get_collection

这是测试的方法

这是错误

在此处输入图像描述

在此处输入图像描述

我的注册方法

create_user方法_

和mongodb配置

这两种方法已添加到 main.py

0 投票
1 回答
384 浏览

python - FastAPI、SQLAlchemy、pytest,无法获得 100% 的覆盖率,它没有正确收集

我正在尝试FastAPI使用测试构建完全覆盖的应用程序python 3.9 为此我选择了堆栈:FastAPI、uvicorn、SQLAlchemy、asyncpg、pytest(+ async、cov 插件)、覆盖率和 httpx AsyncClient

这是我的最低要求.txt

所有测试运行顺利,我得到了预期的结果。但是我遇到了这个问题,覆盖范围没有正确收集。await当协程将控制权返回给事件循环时,它在第一个关键字之后中断

这是关于如何重现此行为的最小设置(也可以在 GitHub 上找到)。

申请代码main.py

测试设置conftest.py如下所示:

测试很简单(只需检查状态码是 200)test_main.py

结果覆盖我得到:

在此处输入图像描述

  1. 上面的示例代码使用aiosqlite而不是,asyncpg但覆盖失败也会持续重现
  2. 我已经得出结论,这个问题与SQLAlchemy,因为这个例子asyncpg没有使用SQLAlchemy像魅力这样的作品
0 投票
0 回答
87 浏览

web-scraping - 如何在异步刮板中模拟多个请求?

所以这里我有异步刮板

https://markets.businessinsider.com/index/components/s&p_500这是我需要解析的网站

所以 get_page_data 函数解析每个页面,在这个函数里面 parse_details_page 解析这个页面上每个公司的数据

我至少有两个需要模拟的请求,但我无法将头绕在内部函数内部的模拟 url 上。

我知道应该用 requests-mock 和 pytest-asyncio 来完成,但不知道怎么做

如果您能给我一个提示,我将不胜感激!

0 投票
0 回答
73 浏览

python - 为什么运行第二个异步测试会返回一个 got Future连接到不同的循环

我正在尝试做一些集成测试,我直接调用本地数据库。我有 2 个测试一个接一个地执行,但是我收到以下错误。

E RuntimeError: Task <Task pending name='Task-3' coro=<TestDB.test_priority() running at /home/kay/checkpoint/entity-extractor/test/test_db.py:21> cb=[_run_until_complete_cb() at /usr/lib/python3.8/asyncio/base_events.py:184]> 将 Future 附加到另一个循环

/usr/lib/python3.8/asyncio/streams.py:517:运行时错误

==================================================== ============================================ 警告总结 ===== ==================================================== ======================================= test/test_db.py::TestDB::test_db
/ home/kay/.local/share/virtualenvs/entity-extractor-BuK_iA9e/lib/python3.8/site-packages/aiomysql/pool.py:46:DeprecationWarning:循环参数自 Python 3.8 起已弃用,并计划删除在 Python 3.10 中。self._cond = asyncio.Condition(loop=loop)

test/test_db.py::TestDB::test_db
/usr/lib/python3.8/asyncio/locks.py:335: DeprecationWarning: 自 Python 3.8 以来不推荐使用循环参数,并计划在 Python 3.10 中删除。锁=锁(循环=循环)

test/test_db.py::TestDB::test_db
/usr/lib/python3.8/asyncio/tasks.py:455: DeprecationWarning: 自 Python 3.8 以来不推荐使用循环参数,并计划在 Python 3.10 中删除。返回等待未来

-- 文档:https : //docs.pytest.org/en/stable/warnings.html ============================= ==================================================== ========== 简短的测试摘要信息 ===================================== ==================================================== ==== FAILED test/test_db.py::TestDB::test_priority - RuntimeError: Task <Task pending name='Task-3' coro=<TestDB.test_priority()

如果我单独运行测试,它们会起作用。我正在使用 aio-libs 中的一些工具来执行测试。pytest、pytest-asyncio 和 aiomysql。

test_db.py

0 投票
0 回答
109 浏览

python - 为什么 pytest 在测试简单的异步函数时挂起?

我有一个简单的回声服务器功能。如果我用一个测试启动 pytest,一切正常,但如果我用两个测试启动它,那么第二个会挂起等待服务器启动,我不明白为什么。这是一个完整的代码。

服务器:

测试设置:

测试:

Pytest输出:================================================ =================== 测试会话开始 ============================ ======================================== 平台 win32 -- Python 3.7.0, pytest- 6.2.5、py-1.10.0、pluggy-1.0.0 -- C:\Users\hurrd.virtualenvs\chatServer-iuEQ-ghN\Scripts\python.exe 缓存目录:.pytest_cache 根目录:C:\Users\hurrd\ PycharmProjects\chatServer 插件:asyncio-0.16.0 收集到 2 项

测试/单元/test_themes.py::test_something 已通过 [50%]

测试/单元/test_themes.py::test_another_thing

0 投票
1 回答
29 浏览

python - 与 pyppeteer 一起使用时,Pytest-asyncio 不会移动到下一条语句

我正在尝试使用 pytest 运行 Pyppeteer,但在启动 chromium 后它不会进入下一条语句。

运行此浏览器后启动,但未打开新页面。

0 投票
1 回答
37 浏览

pytest - 使用 asyncio 加速测试

这是我的代码

pytest 的时间显示需要 118 秒才能完成,这非常接近按顺序向 url 发送请求的测试。有什么改进可以加快这个测试吗?谢谢。

0 投票
0 回答
36 浏览

python - 如何使用异步方法模拟 Python 数据类?

我想测试一个调用底层异步方法的数据类。这是一个不起作用的小测试示例。我已经看到您还可以模拟数据类类,如此处所述:https : //stackoverflow.com/a/51641747/1998288 ,但这不能与 asyncio 结合使用。

下面是一个不起作用的最小示例。

0 投票
1 回答
108 浏览

python - 如何为 python-firestore AsyncClient 设置超时设置?

我正在将 google.cloud.firestore 与 Async Client 一起使用,并且我想添加超时设置以添加文档,但我无法以某种方式...

版本

  • 蟒蛇:3.9.7
  • google-cloud-firestore: ">=2.1.0"
  • API 框架:fastapi: "^0.70.0"
  • pytest:“^6.2.5”
  • pytest-asyncio:“^0.16.0”

问题

当我运行此代码时without setting firebase server turning on

这应该会导致错误only after when 10 secs已经过去,但实际上这会导致错误提示immidiately

我该如何解决db.collection('states').add(...)以适当地等待?

谢谢!

0 投票
0 回答
72 浏览

websocket - Websocket 错误:回调 _SelectorSocketTransport._read_ready() 中的异步异常

我收到以下错误:

ERROR asyncio:base_events.py:1608 Exception in callback _SelectorSocketTransport._read_ready()

它发生在代码的这一点上。基本上在超时期间无法完成任务:

最后,这会导致连接关闭:

因此,对我正在做的事情进行更多解释:我基本上尝试通过嵌入式系统上的 websocket 连接服务器。

我更改了超时值,但没有帮助。我对 websocket 如何工作的话题很陌生,所以经过一些研究,我真的不知道为什么会这样。

我很感激任何帮助:)