Skip to content

Update to not require use of the pytest_namespace() hook #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ inlineCallbacks
=================
Using `twisted.internet.defer.inlineCallbacks` as a decorator for test
functions, which take funcargs, does not work. Please use
`pytest.inlineCallbacks` instead::
`pytest_twisted.inlineCallbacks` instead::

@pytest.inlineCallbacks
@pytest_twisted.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_twisted.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)
return pytest_twisted.blockon(d)


The twisted greenlet
Expand Down
11 changes: 9 additions & 2 deletions pytest_twisted.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@


def blockon(d):
if reactor.running:
return block_from_thread(d)

return blockon_default(d)


def blockon_default(d):
current = greenlet.getcurrent()
assert current is not gr_twisted, \
"blockon cannot be called from the twisted greenlet"
Expand Down Expand Up @@ -42,7 +49,7 @@ def inlineCallbacks(fun, *args, **kw):

def pytest_namespace():
return dict(inlineCallbacks=inlineCallbacks,
blockon=block_from_thread if reactor.running else blockon)
blockon=blockon)


def stop_twisted_greenlet():
Expand Down Expand Up @@ -90,7 +97,7 @@ def in_reactor(d, f, *args):

d = defer.Deferred()
reactor.callLater(0.0, in_reactor, d, _pytest_pyfunc_call, pyfuncitem)
blockon(d)
blockon_default(d)
else:
if not reactor.running:
raise RuntimeError("twisted reactor is not running")
Expand Down
12 changes: 7 additions & 5 deletions testing/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ def test_inlineCallbacks(testdir):
testdir.makepyfile("""
from twisted.internet import reactor, defer
import pytest
import pytest_twisted

@pytest.fixture(scope="module",
params=["fs", "imap", "web"])
def foo(request):
return request.param


@pytest.inlineCallbacks
@pytest_twisted.inlineCallbacks
def test_succeed(foo):
yield defer.succeed(foo)
if foo == "web":
Expand Down Expand Up @@ -107,13 +108,13 @@ def test_MAIN():

def test_blocon_in_hook(testdir):
testdir.makeconftest("""
import pytest
import pytest_twisted as pt
from twisted.internet import reactor, defer

def pytest_configure(config):
d = defer.Deferred()
reactor.callLater(0.01, d.callback, 1)
pytest.blockon(d)
pt.blockon(d)
""")
testdir.makepyfile("""
from twisted.internet import reactor, defer
Expand All @@ -131,18 +132,19 @@ def test_succeed():
def test_pytest_from_reactor_thread(testdir):
testdir.makepyfile("""
import pytest
import pytest_twisted
from twisted.internet import reactor, defer

@pytest.fixture
def fix():
d = defer.Deferred()
reactor.callLater(0.01, d.callback, 42)
return pytest.blockon(d)
return pytest_twisted.blockon(d)

def test_simple(fix):
assert fix == 42

@pytest.inlineCallbacks
@pytest_twisted.inlineCallbacks
def test_fail():
d = defer.Deferred()
reactor.callLater(0.01, d.callback, 1)
Expand Down