- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要测试我的电报机器人。为此,我需要创建客户端用户来询问我的机器人。我找到了 telethon可以做到的图书馆。首先,我编写了一个代码示例,以确保授权和连接正常工作,并向自己发送测试消息(省略导入):
api_id = int(os.getenv("TELEGRAM_APP_ID"))
api_hash = os.getenv("TELEGRAM_APP_HASH")
session_str = os.getenv("TELETHON_SESSION")
async def main():
client = TelegramClient(
StringSession(session_str), api_id, api_hash,
sequential_updates=True
)
await client.connect()
async with client.conversation("@someuser") as conv:
await conv.send_message('Hey, what is your name?')
if __name__ == "__main__":
asyncio.run(main())
@someuser (me) 成功接收消息。好的,现在我根据上面的代码创建一个带有 fixture 的测试:
api_id = int(os.getenv("TELEGRAM_APP_ID"))
api_hash = os.getenv("TELEGRAM_APP_HASH")
session_str = os.getenv("TELETHON_SESSION")
@pytest.fixture(scope="session")
async def client():
client = TelegramClient(
StringSession(session_str), api_id, api_hash,
sequential_updates=True
)
await client.connect()
yield client
await client.disconnect()
@pytest.mark.asyncio
async def test_start(client: TelegramClient):
async with client.conversation("@someuser") as conv:
await conv.send_message("Hey, what is your name?")
运行pytest
后收到错误:
AttributeError: 'async_generator' 对象没有属性 'conversation'
似乎 client
对象从 client
fixture 返回“错误”条件。这是 print(dir(client))
:
['__aiter__', '__anext__', '__class__', '__class_getitem__', '__del__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'aclose', 'ag_await', 'ag_code', 'ag_frame', 'ag_running', 'asend', 'athrow']
我在哪里从 fixture 中的生成器中丢失了“正确的”客户端对象?
最佳答案
根据文档 https://pypi.org/project/pytest-asyncio/#async-fixtures 在异步 fixture 中使用 @pytest_asyncio.fixture
装饰器.
像这样:
import pytest_asyncio
@pytest_asyncio.fixture(scope="session")
async def client():
...
关于python - 在 fixture 中包含代码后,带有异步的 pytest 中的 AttributeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72996818/
我正面临 pytest 固定装置的一个小问题,感谢您的帮助。 我有一些功能装置,如下所述。为简单起见,我没有展示实现。 @pytest.fixture() def get_driver():
我有一个现有的 pytest使用一些预定义列表来测试它们的交叉产品的测试: A_ITEMS = [1, 2, 3] B_ITEMS = [4, 5, 6] C_ITEMS = [7, 8, 9] 我还
我要传一个pytest.fixture函数到另一个 fixture 函数的 param 参数。像这样的东西: @pytest.fixture() def foo(): return "foo"
我试图避免在我的测试中重复太多的样板文件,我想以更有条理的方式重写它们。假设我有两个不同的解析器,它们都可以将文本解析为 doc。然后该文档将用于其他测试。最终目标是公开一个可以在其他测试中使用的 d
我有很多通过 pytest 管理的参数化装置。 有时,我希望使用 fixture 的测试不必担心应用参数。 是否可以制作一个参数化另一个 fixture 的 fixture ? import pyte
我有一对 fixture 用于测试具有 x-y 输入的函数。一个生成 x 值,另一个生成 y 值。测试取决于两者。 y 值也取决于 x 值。以下是设置摘要: import pytest @pytest
例子: from pytest import fixture, skip @fixture(params=['a', 'b']) def f1(request): yield request.
我的 Karma 配置文件中的文件属性是 files: [ // Program files 'public/js/init.js',
Conftest.py @pytest.fixture(scope="module") def fixture2(request): do something @pytest.fixture(
我正在使用 unittest.mock 模拟一个 API。我的界面是一个在后台使用 requests 的类。所以我正在做这样的事情: @pytest.fixture def mocked_api_an
基于此堆栈溢出:pytest fixture of fixtures 我在同一个文件中有以下代码: @pytest.fixture def form_data(): return { ...
命令行 python3 -m pytest src/spec/ --app=android conftest.py import pytest def pytest_addoption(parser)
我正在对我的代码进行一些测试,我得到了我的第一个“停止”,因为我不知道如何继续前进。在我加载灯具的 setUp() 函数中看到: public function setUp() { stati
我在两种模式下运行测试:裸 pytest 和 pytest-xdist。我有一个用模块范围定义的重型 fixture 。在这个 fixture 中,我对使用 xdist 运行测试的情况进行了一些优化:
我想在相同参数化测试的不同实例化之间共享 fixture,其中 fixture 本身也被参数化: #!/usr/bin/py.test -sv import pytest numbers_for_fi
我有一个包含多个 JSON 文件的 AWS S3 目录,这些文件用作测试输入。 我创建了一个 PyTest 模块,它使用模块范围的 fixture 下载所有 JSON 文件一次,然后运行多个测试函数
我有一个固定装置,它返回该端点名称(传入)的端点 名称是测试中设置的字符串。我在测试中每次调用端点(参数化)都搞砸了,现在我不知道如何在不每次调用端点的情况下获得相同的功能。 基本上,我只需要调用一次
如果我有两个参数化的 fixture ,我如何创建一个测试函数,首先用一个 fixture 的实例调用,然后用另一个 fixture 的实例调用? 我想创建一个以某种方式连接两个现有装置的新装置是有意
我是python的新手。我已经声明了一个装置 configure_loggers()范围为“ session ”。现在,我想在另一个 fixture 之前使用这个 fixture setup_app(
我想在另一个固定文件中实例化的固定文件中使用一个对象。类似于以下内容(不起作用): monitor_france: objecttype_id: 2 name1: i-france-1 n
我是一名优秀的程序员,十分优秀!