Skip to content

Commit 7830268

Browse files
authored
Merge pull request #18 from altendky/deprecate_pytest_namespace
Update to not require use of the pytest_namespace() hook
2 parents 40bd20b + d3b8579 commit 7830268

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

README.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@ inlineCallbacks
3434
=================
3535
Using `twisted.internet.defer.inlineCallbacks` as a decorator for test
3636
functions, which take funcargs, does not work. Please use
37-
`pytest.inlineCallbacks` instead::
37+
`pytest_twisted.inlineCallbacks` instead::
3838

39-
@pytest.inlineCallbacks
39+
@pytest_twisted.inlineCallbacks
4040
def test_some_stuff(tmpdir):
4141
res = yield threads.deferToThread(os.listdir, tmpdir.strpath)
4242
assert res == []
4343

4444
Waiting for deferreds in fixtures
4545
=================================
46-
`pytest.blockon` allows fixtures to wait for deferreds::
46+
`pytest_twisted.blockon` allows fixtures to wait for deferreds::
4747

4848
@pytest.fixture
4949
def val():
5050
d = defer.Deferred()
5151
reactor.callLater(1.0, d.callback, 10)
52-
return pytest.blockon(d)
52+
return pytest_twisted.blockon(d)
5353

5454

5555
The twisted greenlet

pytest_twisted.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010

1111

1212
def blockon(d):
13+
if reactor.running:
14+
return block_from_thread(d)
15+
16+
return blockon_default(d)
17+
18+
19+
def blockon_default(d):
1320
current = greenlet.getcurrent()
1421
assert current is not gr_twisted, \
1522
"blockon cannot be called from the twisted greenlet"
@@ -42,7 +49,7 @@ def inlineCallbacks(fun, *args, **kw):
4249

4350
def pytest_namespace():
4451
return dict(inlineCallbacks=inlineCallbacks,
45-
blockon=block_from_thread if reactor.running else blockon)
52+
blockon=blockon)
4653

4754

4855
def stop_twisted_greenlet():
@@ -90,7 +97,7 @@ def in_reactor(d, f, *args):
9097

9198
d = defer.Deferred()
9299
reactor.callLater(0.0, in_reactor, d, _pytest_pyfunc_call, pyfuncitem)
93-
blockon(d)
100+
blockon_default(d)
94101
else:
95102
if not reactor.running:
96103
raise RuntimeError("twisted reactor is not running")

testing/test_basic.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,15 @@ def test_inlineCallbacks(testdir):
6363
testdir.makepyfile("""
6464
from twisted.internet import reactor, defer
6565
import pytest
66+
import pytest_twisted
6667
6768
@pytest.fixture(scope="module",
6869
params=["fs", "imap", "web"])
6970
def foo(request):
7071
return request.param
7172
7273
73-
@pytest.inlineCallbacks
74+
@pytest_twisted.inlineCallbacks
7475
def test_succeed(foo):
7576
yield defer.succeed(foo)
7677
if foo == "web":
@@ -107,13 +108,13 @@ def test_MAIN():
107108

108109
def test_blocon_in_hook(testdir):
109110
testdir.makeconftest("""
110-
import pytest
111+
import pytest_twisted as pt
111112
from twisted.internet import reactor, defer
112113
113114
def pytest_configure(config):
114115
d = defer.Deferred()
115116
reactor.callLater(0.01, d.callback, 1)
116-
pytest.blockon(d)
117+
pt.blockon(d)
117118
""")
118119
testdir.makepyfile("""
119120
from twisted.internet import reactor, defer
@@ -131,18 +132,19 @@ def test_succeed():
131132
def test_pytest_from_reactor_thread(testdir):
132133
testdir.makepyfile("""
133134
import pytest
135+
import pytest_twisted
134136
from twisted.internet import reactor, defer
135137
136138
@pytest.fixture
137139
def fix():
138140
d = defer.Deferred()
139141
reactor.callLater(0.01, d.callback, 42)
140-
return pytest.blockon(d)
142+
return pytest_twisted.blockon(d)
141143
142144
def test_simple(fix):
143145
assert fix == 42
144146
145-
@pytest.inlineCallbacks
147+
@pytest_twisted.inlineCallbacks
146148
def test_fail():
147149
d = defer.Deferred()
148150
reactor.callLater(0.01, d.callback, 1)

0 commit comments

Comments
 (0)