Skip to content

Commit 4529f1b

Browse files
authored
Merge pull request #59 from altendky/58-altendky-deprecate_things_in_pytest_namespace
[58] Deprecate pytest.inlineCallbacks and pytest.blockon, use pytest_twisted.*
2 parents 681fa3d + 02cb2d8 commit 4529f1b

File tree

3 files changed

+112
-3
lines changed

3 files changed

+112
-3
lines changed

README.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,16 @@ corotwine work with pytest-twisted::
9898
protocol.MAIN = twisted_greenlet
9999

100100

101-
That's all.
101+
That's (almost) all.
102+
103+
============
104+
Deprecations
105+
============
106+
107+
``pytest.blockon``
108+
Use ``pytest_twisted.blockon``
109+
``pytest.inlineCallbacks``
110+
Use ``pytest_twisted.inlineCallbacks``
102111

103112

104113
.. |PyPI| image:: https://img.shields.io/pypi/v/pytest-twisted.svg

pytest_twisted.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import functools
12
import inspect
3+
import warnings
24

35
import decorator
46
import greenlet
@@ -22,6 +24,25 @@ class _instances:
2224
reactor = None
2325

2426

27+
def _deprecate(deprecated, recommended):
28+
def decorator(f):
29+
@functools.wraps(f)
30+
def wrapper(*args, **kwargs):
31+
warnings.warn(
32+
'{deprecated} has been deprecated, use {recommended}'.format(
33+
deprecated=deprecated,
34+
recommended=recommended,
35+
),
36+
DeprecationWarning,
37+
stacklevel=2,
38+
)
39+
return f(*args, **kwargs)
40+
41+
return wrapper
42+
43+
return decorator
44+
45+
2546
def blockon(d):
2647
if _config.external_reactor:
2748
return block_from_thread(d)
@@ -185,6 +206,13 @@ def pytest_addoption(parser):
185206

186207

187208
def pytest_configure(config):
188-
pytest.inlineCallbacks = inlineCallbacks
189-
pytest.blockon = blockon
209+
pytest.inlineCallbacks = _deprecate(
210+
deprecated='pytest.inlineCallbacks',
211+
recommended='pytest_twisted.inlineCallbacks',
212+
)(inlineCallbacks)
213+
pytest.blockon = _deprecate(
214+
deprecated='pytest.blockon',
215+
recommended='pytest_twisted.blockon',
216+
)(blockon)
217+
190218
reactor_installers[config.getoption("reactor")]()

testing/test_basic.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,82 @@ def test_inline_callbacks_in_pytest():
5757
assert hasattr(pytest, 'inlineCallbacks')
5858

5959

60+
@pytest.mark.parametrize(
61+
'decorator, should_warn',
62+
(
63+
('pytest.inlineCallbacks', True),
64+
('pytest_twisted.inlineCallbacks', False),
65+
),
66+
)
67+
def test_inline_callbacks_in_pytest_deprecation(
68+
testdir,
69+
cmd_opts,
70+
decorator,
71+
should_warn,
72+
):
73+
import_path, _, _ = decorator.rpartition('.')
74+
test_file = """
75+
import {import_path}
76+
77+
def test_deprecation():
78+
@{decorator}
79+
def f():
80+
yield 42
81+
""".format(import_path=import_path, decorator=decorator)
82+
testdir.makepyfile(test_file)
83+
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
84+
85+
expected_outcomes = {"passed": 1}
86+
if should_warn:
87+
expected_outcomes["warnings"] = 1
88+
89+
assert_outcomes(rr, expected_outcomes)
90+
91+
6092
def test_blockon_in_pytest():
6193
assert hasattr(pytest, 'blockon')
6294

6395

96+
@pytest.mark.parametrize(
97+
'function, should_warn',
98+
(
99+
('pytest.blockon', True),
100+
('pytest_twisted.blockon', False),
101+
),
102+
)
103+
def test_blockon_in_pytest_deprecation(
104+
testdir,
105+
cmd_opts,
106+
function,
107+
should_warn,
108+
):
109+
import_path, _, _ = function.rpartition('.')
110+
test_file = """
111+
import warnings
112+
113+
from twisted.internet import reactor, defer
114+
import pytest
115+
import {import_path}
116+
117+
@pytest.fixture
118+
def foo(request):
119+
d = defer.Deferred()
120+
d.callback(None)
121+
{function}(d)
122+
123+
def test_succeed(foo):
124+
pass
125+
""".format(import_path=import_path, function=function)
126+
testdir.makepyfile(test_file)
127+
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
128+
129+
expected_outcomes = {"passed": 1}
130+
if should_warn:
131+
expected_outcomes["warnings"] = 1
132+
133+
assert_outcomes(rr, expected_outcomes)
134+
135+
64136
def test_fail_later(testdir, cmd_opts):
65137
test_file = """
66138
from twisted.internet import reactor, defer

0 commit comments

Comments
 (0)