Skip to content

Commit a4f23dd

Browse files
committed
less hacky
1 parent 8ef4193 commit a4f23dd

File tree

4 files changed

+87
-17
lines changed

4 files changed

+87
-17
lines changed

pytest_twisted.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,19 +365,28 @@ def pytest_pyfunc_call(pyfuncitem):
365365
# TODO: only handle 'our' tests? what is the point of handling others?
366366
# well, because our interface allowed people to return deferreds
367367
# from arbitrary tests so we kinda have to keep this up for now
368-
f = pyfuncitem.obj.hypothesis.inner_test
369-
pyfuncitem.obj.hypothesis.inner_test = lambda **kwargs: _run_inline_callbacks(_async_pytest_pyfunc_call, pyfuncitem, f, kwargs)
370-
return None
368+
maybe_hypothesis = getattr(pyfuncitem.obj, "hypothesis", None)
369+
if maybe_hypothesis is None:
370+
_run_inline_callbacks(_async_pytest_pyfunc_call, pyfuncitem, pyfuncitem.obj, {})
371+
result = not None
372+
else:
373+
hypothesis = maybe_hypothesis
374+
f = hypothesis.inner_test
375+
pyfuncitem.obj.hypothesis.inner_test = lambda **kwargs: _run_inline_callbacks(_async_pytest_pyfunc_call, pyfuncitem, f, kwargs)
376+
result = None
377+
378+
return result
371379

372380

373381
@defer.inlineCallbacks
374382
def _async_pytest_pyfunc_call(pyfuncitem, f, kwargs):
375383
"""Run test function."""
376-
kwargs.update({
384+
fixture_kwargs = {
377385
name: value
378386
for name, value in pyfuncitem.funcargs.items()
379387
if name in pyfuncitem._fixtureinfo.argnames
380-
})
388+
}
389+
kwargs.update(fixture_kwargs)
381390

382391
maybe_mark = _get_mark(f)
383392
if maybe_mark == 'async_test':

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
python_requires='>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*',
1717
install_requires=["greenlet", "pytest>=2.3", "decorator"],
1818
extras_require={
19-
"dev": ["pre-commit", "black"],
19+
"dev": ["pre-commit", "black", "hypothesis"],
2020
"pyside2": [
2121
# >= 0.6.3 for PySide2 extra version constraints
2222
"qt5reactor[pyside2]>=0.6.3",

test.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

testing/test_basic.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,3 +1255,75 @@ def test_should_not_run():
12551255
rr.stdout.no_re_match_line(pat=pattern)
12561256
else:
12571257
assert re.match(pattern, rr.stdout.str()) is None
1258+
1259+
1260+
def test_hypothesis_async_passes(testdir, cmd_opts):
1261+
test_file = """
1262+
import hypothesis
1263+
import hypothesis.strategies
1264+
1265+
import pytest_twisted
1266+
1267+
@hypothesis.given(x=hypothesis.strategies.integers())
1268+
@pytest_twisted.ensureDeferred
1269+
async def test_async(x):
1270+
assert isinstance(x, int)
1271+
"""
1272+
testdir.makepyfile(test_file)
1273+
rr = testdir.run(*cmd_opts, timeout=timeout)
1274+
assert_outcomes(rr, {"passed": 1})
1275+
1276+
1277+
def test_hypothesis_inline_callbacks_passes(testdir, cmd_opts):
1278+
test_file = """
1279+
import hypothesis
1280+
import hypothesis.strategies
1281+
1282+
import pytest_twisted
1283+
1284+
@hypothesis.given(x=hypothesis.strategies.integers())
1285+
@pytest_twisted.inlineCallbacks
1286+
def test_inline_callbacks(x):
1287+
assert isinstance(x, int)
1288+
return
1289+
yield
1290+
"""
1291+
testdir.makepyfile(test_file)
1292+
rr = testdir.run(*cmd_opts, timeout=timeout)
1293+
assert_outcomes(rr, {"passed": 1})
1294+
1295+
1296+
def test_hypothesis_async_ils(testdir, cmd_opts):
1297+
test_file = """
1298+
import hypothesis
1299+
import hypothesis.strategies
1300+
1301+
import pytest_twisted
1302+
1303+
@hypothesis.given(x=hypothesis.strategies.integers())
1304+
@pytest_twisted.ensureDeferred
1305+
async def test_async(x):
1306+
assert isinstance(x, str)
1307+
"""
1308+
testdir.makepyfile(test_file)
1309+
rr = testdir.run(*cmd_opts, timeout=timeout)
1310+
assert_outcomes(rr, {"failed": 1})
1311+
1312+
1313+
def test_hypothesis_inline_callbacks_fails(testdir, cmd_opts):
1314+
test_file = """
1315+
import hypothesis
1316+
import hypothesis.strategies
1317+
1318+
import pytest_twisted
1319+
1320+
@hypothesis.given(x=hypothesis.strategies.integers())
1321+
@pytest_twisted.inlineCallbacks
1322+
def test_inline_callbacks(x):
1323+
assert isinstance(x, str)
1324+
return
1325+
yield
1326+
"""
1327+
testdir.makepyfile(test_file)
1328+
rr = testdir.run(*cmd_opts, timeout=timeout)
1329+
assert_outcomes(rr, {"failed": 1})

0 commit comments

Comments
 (0)