Skip to content

Commit

Permalink
Support trio_run = trio_asyncio ini option.
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
jmehnle committed Aug 12, 2024
1 parent f03160a commit 242139d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/source/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,8 @@ such as via :ref:`guest mode <trio:guest-mode>`, it can be used with
pytest-trio as well. Setting ``trio_run`` in the pytest configuration
makes your choice the global default for both tests explicitly marked
with ``@pytest.mark.trio`` and those automatically marked by Trio mode.
``trio_run`` presently supports ``trio`` and ``qtrio``.
``trio_run`` presently supports ``trio``, ``qtrio``, and
``trio_asyncio``.

.. code-block:: ini
Expand Down
25 changes: 25 additions & 0 deletions pytest_trio/_tests/test_trio_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def run(*args, **kwargs):
"""


# Fake trio_asyncio module.
trio_asyncio_text = qtrio_text


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

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


def test_trio_asyncio_just_run_configuration(testdir):
testdir.makefile(".ini", pytest="[pytest]\ntrio_run = trio_asyncio\n")

testdir.makepyfile(trio_asyncio=trio_asyncio_text)

test_text = """
import pytest
import trio_asyncio
import trio
@pytest.mark.trio
async def test_fake_trio_asyncio_used():
await trio.sleep(0)
assert trio_asyncio.fake_used
"""
testdir.makepyfile(test_text)

result = testdir.runpytest()
result.assert_outcomes(passed=1)


def test_invalid_trio_run_fails(testdir):
run_name = "invalid_trio_run"

Expand Down
8 changes: 6 additions & 2 deletions pytest_trio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def pytest_addoption(parser):
)
parser.addini(
"trio_run",
"what runner should pytest-trio use? [trio, qtrio]",
"what runner should pytest-trio use? [trio, qtrio, trio_asyncio]",
default="trio",
)

Expand Down Expand Up @@ -515,10 +515,14 @@ def choose_run(config):
import qtrio

run = qtrio.run
elif run_string == "trio_asyncio":
import trio_asyncio

run = trio_asyncio.run
else:
raise ValueError(
f"{run_string!r} not valid for 'trio_run' config."
+ " Must be one of: trio, qtrio"
+ " Must be one of: trio, qtrio, trio_asyncio"
)

return run
Expand Down

0 comments on commit 242139d

Please sign in to comment.