py.test的扭曲插件。
pytest-twisted的Python项目详细描述
Authors: | Ralf Schmitt, Kyle Altendorf, Victor Titor |
---|---|
Version: | 1.11 |
Date: | 2019-08-20 |
Download: | https://pypi.python.org/pypi/pytest-twisted#downloads |
Code: | https://github.com/pytest-dev/pytest-twisted |
pytest twisted是pytest的一个插件,它允许测试代码, 它使用了扭曲的框架。测试函数可以返回延迟的 对象和pytest将使用此插件等待其完成。
安装
使用以下命令安装插件:
pip install pytest-twisted
使用插件
插件在安装后可用,可以使用 -p no:twisted。
默认情况下,twisted.internet.default用于安装反应器。 这将创建与^{tt3}相同的反应器$ 会的。可使用^{tt4}指定替代反应器$ 选项目前支持qt5reactor与^{tt6}一起使用$ 以及pytest-qt和asyncio。这个guide描述了如何添加 支持一个新的反应堆。
反应堆在第一次测试前自动创建,但可以 通过调用 pytest_twisted.init_default_reactor()或相应的函数 用于所需的备用反应堆。
在诸如 一个名为pytest_twisted的conftest.py文件 被pytest检测为未知钩子是不应该的。一种选择 是import pytest_twisted as pt。
内联回调
使用twisted.internet.defer.inlineCallbacks作为测试的装饰符 使用fixture的函数不起作用。请使用 pytest_twisted.inlineCallbacks取而代之:
@pytest_twisted.inlineCallbacks def test_some_stuff(tmpdir): res = yield threads.deferToThread(os.listdir, tmpdir.strpath) assert res == []
确保
使用twisted.internet.defer.ensureDeferred作为测试的装饰符 使用fixture的函数不起作用。请使用 pytest_twisted.ensureDeferred取而代之:
@pytest_twisted.ensureDeferred async def test_some_stuff(tmpdir): res = await threads.deferToThread(os.listdir, tmpdir.strpath) assert res == []
等待设备延迟
pytest_twisted.blockon允许装置等待延迟:
@pytest.fixture def val(): d = defer.Deferred() reactor.callLater(1.0, d.callback, 10) return pytest_twisted.blockon(d)
扭曲的绿色
有些图书馆(如corotwine)需要知道greenlet,即 运行扭曲的反应堆。它可以从 twisted_greenlet固定装置。以下代码可用于 Corotwine与Pytest Twisted一起工作:
@pytest.fixture(scope="session", autouse=True) def set_MAIN(request, twisted_greenlet): from corotwine import protocol protocol.MAIN = twisted_greenlet
差不多就这些了。
折旧
v1.9
- pytest.blockon
- 使用pytest_twisted.blockon
- pytest.inlineCallbacks
- 使用pytest_twisted.inlineCallbacks