From 4279e12063bb4f2114ba25db4bdcb13c6022808d Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Wed, 26 Aug 2020 22:11:38 -0400 Subject: [PATCH] Add trio_run for configurable substitution for trio.run --- pytest_trio/_tests/helpers.py | 6 ++++- pytest_trio/plugin.py | 42 ++++++++++++++++++++++++++++++----- test-requirements.txt | 1 + 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/pytest_trio/_tests/helpers.py b/pytest_trio/_tests/helpers.py index 5ae5f9e..89ef16c 100644 --- a/pytest_trio/_tests/helpers.py +++ b/pytest_trio/_tests/helpers.py @@ -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] ) diff --git a/pytest_trio/plugin.py b/pytest_trio/plugin.py index 12e774b..4ec673f 100644 --- a/pytest_trio/plugin.py +++ b/pytest_trio/plugin.py @@ -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): @@ -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 @@ -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 @@ -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) ################################################################ diff --git a/test-requirements.txt b/test-requirements.txt index 5c53c25..9139fcf 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -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