skip to navigation
skip to content

Not Logged In

pytest-twisted 1.5

A twisted plugin for py.test.

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.

Installation

Install the plugin with:

pip install pytest-twisted

Using the plugin

The plugin must be enabled. This can be done in the following ways:

  1. Run py.test with the --twisted command line option

  2. Put the following into conftest.py:

    pytest_plugins = "pytest_twisted"
    
  3. Put the following into pytest.ini:

    [pytest]
    twisted = 1
    

inlineCallbacks

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

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

Waiting for deferreds in fixtures

pytest.blockon allows fixtures to wait for deferreds:

@pytest.fixture
def val():
    d = defer.Deferred()
    reactor.callLater(1.0, d.callback, 10)
    return pytest.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 all.

 
File Type Py Version Uploaded on Size
pytest-twisted-1.5.zip (md5) Source 2014-02-04 8KB
pytest_twisted-1.5-py27-none-any.whl (md5) Python Wheel 2.7 2014-02-04 5KB
  • Downloads (All Versions):
  • 64 downloads in the last day
  • 321 downloads in the last week
  • 1631 downloads in the last month