py.test的扭曲插件。

pytest-twisted的Python项目详细描述


PyPI versionSupported Python versionsTravis build statusAppVeyor build statusBlack code style

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-qtasyncio。这个guide描述了如何添加 支持一个新的反应堆。

反应堆在第一次测试前自动创建,但可以 通过调用 pytest_twisted.init_default_reactor()或相应的函数 用于所需的备用反应堆。

在诸如 一个名为pytest_twistedconftest.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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java中ArrayList的超简单问题   Java 8在一段时间后过期   java如何创建具有用户定义维度的矩阵,并使用从上到下、从左到右的递增值填充它?   java从JDBC重启mysql   带有sqlite的java LiveData未更新UI   带有JDialog的java小程序在Mac OSX中未正确隐藏   java ActionListener无法从公共类引用数组?   java Apache Digester:NoSuchMethodException:没有这样的可访问方法   安卓中数据库中的java数据没有以正确的格式检索   java快速排序实现:使用random pivot时几乎排序   安卓 Java:高效的ArrayList过滤?   java如何在单独的文件中制作GUI程序   jasper报告如何从JSP或Java代码在JasperReport中传递参数值?