Skip to main content

A twisted plugin for py.test.

Project description

PyPI version Supported Python versions Travis build status AppVeyor build status Black code style


Ralf Schmitt, Kyle Altendorf, Victor Titor







pytest-twisted is a plugin for pytest, which allows to test code, which uses the twisted framework. test functions can return Deferred objects and pytest will wait for their completion with this plugin.


Install the plugin with:

pip install pytest-twisted

Using the plugin

The plugin is available after installation and can be disabled using -p no:twisted.

By default twisted.internet.default is used to install the reactor. This creates the same reactor that import twisted.internet.reactor would. Alternative reactors can be specified using the --reactor option.

Presently only qt5reactor is supported for use with pyqt5 and pytest-qt. This guide describes how to add support for a new reactor.

The reactor is automatically created prior to the first test but can be explicitly installed earlier by calling pytest_twisted.init_default_reactor() or the corresponding function for the desired alternate reactor.

Beware that in situations such as a file that the name pytest_twisted may be undesirably detected by pytest as an unknown hook. One alternative is to import pytest_twisted as pt.


Using twisted.internet.defer.inlineCallbacks as a decorator for test functions, which take funcargs, does not work. Please use pytest_twisted.inlineCallbacks instead:

def test_some_stuff(tmpdir):
    res = yield threads.deferToThread(os.listdir, tmpdir.strpath)
    assert res == []


Using twisted.internet.defer.ensureDeferred as a decorator for test functions, which take funcargs, does not work. Please use pytest_twisted.ensureDeferred instead:

async def test_some_stuff(tmpdir):
    res = await threads.deferToThread(os.listdir, tmpdir.strpath)
    assert res == []

Waiting for deferreds in fixtures

pytest_twisted.blockon allows fixtures to wait for deferreds:

def val():
    d = defer.Deferred()
    reactor.callLater(1.0, d.callback, 10)
    return pytest_twisted.blockon(d)

The twisted greenlet

Some libraries (e.g. corotwine) need to know the greenlet, which is running the twisted reactor. It’s available from the twisted_greenlet funcarg. The following code can be used to make corotwine work with pytest-twisted:

@pytest.fixture(scope="session", autouse=True)
def set_MAIN(request, twisted_greenlet):
    from corotwine import protocol
    protocol.MAIN = twisted_greenlet

That’s (almost) all.




Use pytest_twisted.blockon


Use pytest_twisted.inlineCallbacks

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution (13.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page