Skip to content

Commit c905f15

Browse files
authored
Merge pull request #49 from njsmith/trio-mode
Implement Trio mode
2 parents a972dc6 + 910d79c commit c905f15

4 files changed

Lines changed: 84 additions & 3 deletions

File tree

pytest_trio/_tests/test_hypothesis_interaction.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import pytest
22
from hypothesis import given, settings, strategies as st
33

4-
# To avoid unpredictable warnings/errors when CI happens to be slow
5-
# Example: https://travis-ci.org/python-trio/pytest-trio/jobs/406738296
6-
our_settings = settings(deadline=None)
4+
# deadline=None avoids unpredictable warnings/errors when CI happens to be
5+
# slow (example: https://travis-ci.org/python-trio/pytest-trio/jobs/406738296)
6+
# max_examples=5 speeds things up a bit
7+
our_settings = settings(deadline=None, max_examples=5)
78

89

910
@our_settings
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pytest
2+
3+
test_text = """
4+
import pytest
5+
import trio
6+
from hypothesis import given, settings, strategies
7+
8+
async def test_pass():
9+
await trio.sleep(0)
10+
11+
async def test_fail():
12+
await trio.sleep(0)
13+
assert False
14+
15+
@settings(deadline=None, max_examples=5)
16+
@given(strategies.binary())
17+
async def test_hypothesis_pass(b):
18+
await trio.sleep(0)
19+
assert isinstance(b, bytes)
20+
21+
@settings(deadline=None, max_examples=5)
22+
@given(strategies.binary())
23+
async def test_hypothesis_fail(b):
24+
await trio.sleep(0)
25+
assert isinstance(b, int)
26+
"""
27+
28+
29+
def test_trio_mode_pytest_ini(testdir):
30+
testdir.makepyfile(test_text)
31+
32+
testdir.makefile(".ini", pytest="[pytest]\ntrio_mode = true\n")
33+
34+
result = testdir.runpytest()
35+
result.assert_outcomes(passed=2, failed=2)
36+
37+
38+
def test_trio_mode_conftest(testdir):
39+
testdir.makepyfile(test_text)
40+
41+
testdir.makeconftest("from pytest_trio.enable_trio_mode import *")
42+
43+
result = testdir.runpytest()
44+
result.assert_outcomes(passed=2, failed=2)

pytest_trio/enable_trio_mode.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
__all__ = ["pytest_collection_modifyitems"]
2+
3+
from .plugin import automark
4+
5+
6+
def pytest_collection_modifyitems(items):
7+
automark(items)

pytest_trio/plugin.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
ORDERED_DICTS = False
2121

2222

23+
def pytest_addoption(parser):
24+
parser.addini(
25+
"trio_mode",
26+
"should pytest-trio handle all async functions?",
27+
type="bool",
28+
default=False,
29+
)
30+
31+
2332
def pytest_configure(config):
2433
# So that it shows up in 'pytest --markers' output:
2534
config.addinivalue_line(
@@ -264,6 +273,26 @@ def pytest_fixture_setup(fixturedef, request):
264273
return _install_async_fixture_if_needed(fixturedef, request)
265274

266275

276+
################################################################
277+
# Trio mode
278+
################################################################
279+
280+
281+
def automark(items):
282+
for item in items:
283+
if hasattr(item.obj, "hypothesis"):
284+
test_func = item.obj.hypothesis.inner_test
285+
else:
286+
test_func = item.obj
287+
if iscoroutinefunction(test_func):
288+
item.add_marker(pytest.mark.trio)
289+
290+
291+
def pytest_collection_modifyitems(config, items):
292+
if config.getini("trio_mode"):
293+
automark(items)
294+
295+
267296
################################################################
268297
# Built-in fixtures
269298
################################################################

0 commit comments

Comments
 (0)