Skip to content

Commit 242139d

Browse files
committed
Support trio_run = trio_asyncio ini option.
`trio_asyncio` as another mode of the `trio_run` ini file option allows us to run tests with an `asyncio` loop implicitly available to all async tests. python-trio#71
1 parent f03160a commit 242139d

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

docs/source/reference.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ such as via :ref:`guest mode <trio:guest-mode>`, it can be used with
393393
pytest-trio as well. Setting ``trio_run`` in the pytest configuration
394394
makes your choice the global default for both tests explicitly marked
395395
with ``@pytest.mark.trio`` and those automatically marked by Trio mode.
396-
``trio_run`` presently supports ``trio`` and ``qtrio``.
396+
``trio_run`` presently supports ``trio``, ``qtrio``, and
397+
``trio_asyncio``.
397398

398399
.. code-block:: ini
399400

pytest_trio/_tests/test_trio_mode.py

+25
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def run(*args, **kwargs):
5555
"""
5656

5757

58+
# Fake trio_asyncio module.
59+
trio_asyncio_text = qtrio_text
60+
61+
5862
def test_trio_mode_and_qtrio_run_configuration(testdir):
5963
testdir.makefile(".ini", pytest="[pytest]\ntrio_mode = true\ntrio_run = qtrio\n")
6064

@@ -116,6 +120,27 @@ async def test_fake_qtrio_used():
116120
result.assert_outcomes(passed=1)
117121

118122

123+
def test_trio_asyncio_just_run_configuration(testdir):
124+
testdir.makefile(".ini", pytest="[pytest]\ntrio_run = trio_asyncio\n")
125+
126+
testdir.makepyfile(trio_asyncio=trio_asyncio_text)
127+
128+
test_text = """
129+
import pytest
130+
import trio_asyncio
131+
import trio
132+
133+
@pytest.mark.trio
134+
async def test_fake_trio_asyncio_used():
135+
await trio.sleep(0)
136+
assert trio_asyncio.fake_used
137+
"""
138+
testdir.makepyfile(test_text)
139+
140+
result = testdir.runpytest()
141+
result.assert_outcomes(passed=1)
142+
143+
119144
def test_invalid_trio_run_fails(testdir):
120145
run_name = "invalid_trio_run"
121146

pytest_trio/plugin.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def pytest_addoption(parser):
3030
)
3131
parser.addini(
3232
"trio_run",
33-
"what runner should pytest-trio use? [trio, qtrio]",
33+
"what runner should pytest-trio use? [trio, qtrio, trio_asyncio]",
3434
default="trio",
3535
)
3636

@@ -515,10 +515,14 @@ def choose_run(config):
515515
import qtrio
516516

517517
run = qtrio.run
518+
elif run_string == "trio_asyncio":
519+
import trio_asyncio
520+
521+
run = trio_asyncio.run
518522
else:
519523
raise ValueError(
520524
f"{run_string!r} not valid for 'trio_run' config."
521-
+ " Must be one of: trio, qtrio"
525+
+ " Must be one of: trio, qtrio, trio_asyncio"
522526
)
523527

524528
return run

0 commit comments

Comments
 (0)