Skip to content

Commit

Permalink
Add trio_run for configurable substitution for trio.run
Browse files Browse the repository at this point in the history
  • Loading branch information
altendky committed Aug 27, 2020
1 parent 80c1638 commit 4279e12
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
6 changes: 5 additions & 1 deletion pytest_trio/_tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ def enable_trio_mode_via_pytest_ini(testdir):
testdir.makefile(".ini", pytest="[pytest]\ntrio_mode = true\n")


def enable_qtrio_mode_via_pytest_ini(testdir):
testdir.makefile(".ini", pytest="[pytest]\ntrio_mode = true\ntrio_run = qtrio\n")


def enable_trio_mode_via_conftest_py(testdir):
testdir.makeconftest("from pytest_trio.enable_trio_mode import *")


enable_trio_mode = pytest.mark.parametrize(
"enable_trio_mode",
[enable_trio_mode_via_pytest_ini, enable_trio_mode_via_conftest_py]
[enable_trio_mode_via_pytest_ini, enable_qtrio_mode_via_pytest_ini, enable_trio_mode_via_conftest_py]
)
42 changes: 37 additions & 5 deletions pytest_trio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def pytest_addoption(parser):
type="bool",
default=False,
)
parser.addini(
"trio_run",
"what runner should pytest-trio use? [trio, qtrio]",
type="linelist",
default=["trio"],
)


def pytest_configure(config):
Expand Down Expand Up @@ -308,7 +314,23 @@ async def run(self, test_ctx, contextvars_ctx):


def _trio_test_runner_factory(item, testfunc=None):
testfunc = testfunc or item.obj
if testfunc:
run = trio.run
else:
testfunc = item.obj

runs = {marker.kwargs.get('run', trio.run) for marker in item.iter_markers("trio")}

if len(runs) == 0:
1/0
elif len(runs) == 1:
[run] = runs
else:
runs.discard(trio.run)
if len(runs) == 1:
[run] = runs
else:
1/0

if getattr(testfunc, '_trio_test_runner_wrapped', False):
# We have already wrapped this, perhaps because we combined Hypothesis
Expand All @@ -320,7 +342,7 @@ def _trio_test_runner_factory(item, testfunc=None):
'test function `%r` is marked trio but is not async' % item
)

@trio_test
@trio_test(run=run)
async def _bootstrap_fixtures_and_run_test(**kwargs):
__tracebackhide__ = True

Expand Down Expand Up @@ -438,19 +460,29 @@ def pytest_fixture_setup(fixturedef, request):
################################################################


def automark(items):
def automark(items, run=trio.run):
for item in items:
if hasattr(item.obj, "hypothesis"):
test_func = item.obj.hypothesis.inner_test
else:
test_func = item.obj
if iscoroutinefunction(test_func):
item.add_marker(pytest.mark.trio)
item.add_marker(pytest.mark.trio(run=run))


def pytest_collection_modifyitems(config, items):
if config.getini("trio_mode"):
automark(items)
[run_string] = config.getini("trio_run")

if run_string == "trio":
run = trio.run
elif run_string == "qtrio":
import qtrio
run = qtrio.run
else:
1/0

automark(items, run=run)


################################################################
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pytest >= 6.0.0 # https://github.com/python-trio/pytest-trio/pull/98#issuecomment-678699693
pytest-cov
hypothesis>=3.64
https://github.com/altendky/trio/archive/configurable_trio_run.zip

0 comments on commit 4279e12

Please sign in to comment.