Skip to content

Commit bac0f90

Browse files
authored
Merge branch 'master' into 35-altendky-tidy_async_await_fixtures
2 parents bdf459a + 4529f1b commit bac0f90

File tree

3 files changed

+111
-3
lines changed

3 files changed

+111
-3
lines changed

README.rst

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

116116

117-
That's all.
117+
That's (almost) all.
118+
119+
============
120+
Deprecations
121+
============
122+
123+
``pytest.blockon``
124+
Use ``pytest_twisted.blockon``
125+
``pytest.inlineCallbacks``
126+
Use ``pytest_twisted.inlineCallbacks``
118127

119128

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

pytest_twisted.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
import inspect
3+
import warnings
34

45
import decorator
56
import greenlet
@@ -47,6 +48,25 @@ class _instances:
4748
reactor = None
4849

4950

51+
def _deprecate(deprecated, recommended):
52+
def decorator(f):
53+
@functools.wraps(f)
54+
def wrapper(*args, **kwargs):
55+
warnings.warn(
56+
'{deprecated} has been deprecated, use {recommended}'.format(
57+
deprecated=deprecated,
58+
recommended=recommended,
59+
),
60+
DeprecationWarning,
61+
stacklevel=2,
62+
)
63+
return f(*args, **kwargs)
64+
65+
return wrapper
66+
67+
return decorator
68+
69+
5070
def blockon(d):
5171
if _config.external_reactor:
5272
return block_from_thread(d)
@@ -289,6 +309,13 @@ def pytest_addoption(parser):
289309

290310

291311
def pytest_configure(config):
292-
pytest.inlineCallbacks = inlineCallbacks
293-
pytest.blockon = blockon
312+
pytest.inlineCallbacks = _deprecate(
313+
deprecated='pytest.inlineCallbacks',
314+
recommended='pytest_twisted.inlineCallbacks',
315+
)(inlineCallbacks)
316+
pytest.blockon = _deprecate(
317+
deprecated='pytest.blockon',
318+
recommended='pytest_twisted.blockon',
319+
)(blockon)
320+
294321
reactor_installers[config.getoption("reactor")]()

testing/test_basic.py

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

7070

71+
@pytest.mark.parametrize(
72+
'decorator, should_warn',
73+
(
74+
('pytest.inlineCallbacks', True),
75+
('pytest_twisted.inlineCallbacks', False),
76+
),
77+
)
78+
def test_inline_callbacks_in_pytest_deprecation(
79+
testdir,
80+
cmd_opts,
81+
decorator,
82+
should_warn,
83+
):
84+
import_path, _, _ = decorator.rpartition('.')
85+
test_file = """
86+
import {import_path}
87+
88+
def test_deprecation():
89+
@{decorator}
90+
def f():
91+
yield 42
92+
""".format(import_path=import_path, decorator=decorator)
93+
testdir.makepyfile(test_file)
94+
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
95+
96+
expected_outcomes = {"passed": 1}
97+
if should_warn:
98+
expected_outcomes["warnings"] = 1
99+
100+
assert_outcomes(rr, expected_outcomes)
101+
102+
71103
def test_blockon_in_pytest():
72104
assert hasattr(pytest, 'blockon')
73105

74106

107+
@pytest.mark.parametrize(
108+
'function, should_warn',
109+
(
110+
('pytest.blockon', True),
111+
('pytest_twisted.blockon', False),
112+
),
113+
)
114+
def test_blockon_in_pytest_deprecation(
115+
testdir,
116+
cmd_opts,
117+
function,
118+
should_warn,
119+
):
120+
import_path, _, _ = function.rpartition('.')
121+
test_file = """
122+
import warnings
123+
124+
from twisted.internet import reactor, defer
125+
import pytest
126+
import {import_path}
127+
128+
@pytest.fixture
129+
def foo(request):
130+
d = defer.Deferred()
131+
d.callback(None)
132+
{function}(d)
133+
134+
def test_succeed(foo):
135+
pass
136+
""".format(import_path=import_path, function=function)
137+
testdir.makepyfile(test_file)
138+
rr = testdir.run(sys.executable, "-m", "pytest", "-v", *cmd_opts)
139+
140+
expected_outcomes = {"passed": 1}
141+
if should_warn:
142+
expected_outcomes["warnings"] = 1
143+
144+
assert_outcomes(rr, expected_outcomes)
145+
146+
75147
def test_fail_later(testdir, cmd_opts):
76148
test_file = """
77149
from twisted.internet import reactor, defer

0 commit comments

Comments
 (0)