From 242139d633d7896692f0e913d352b5c67c7ca7c6 Mon Sep 17 00:00:00 2001 From: Julian Mehnle Date: Mon, 12 Aug 2024 22:52:49 +0000 Subject: [PATCH] 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. https://github.com/python-trio/pytest-trio/issues/71 --- docs/source/reference.rst | 3 ++- pytest_trio/_tests/test_trio_mode.py | 25 +++++++++++++++++++++++++ pytest_trio/plugin.py | 8 ++++++-- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/docs/source/reference.rst b/docs/source/reference.rst index 351e8c1..4c61406 100644 --- a/docs/source/reference.rst +++ b/docs/source/reference.rst @@ -393,7 +393,8 @@ such as via :ref:`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 diff --git a/pytest_trio/_tests/test_trio_mode.py b/pytest_trio/_tests/test_trio_mode.py index 19efdc1..26adbd4 100644 --- a/pytest_trio/_tests/test_trio_mode.py +++ b/pytest_trio/_tests/test_trio_mode.py @@ -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") @@ -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" diff --git a/pytest_trio/plugin.py b/pytest_trio/plugin.py index ebfcbee..3b8eea6 100644 --- a/pytest_trio/plugin.py +++ b/pytest_trio/plugin.py @@ -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", ) @@ -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