From 919638f301b30c24ebdbb3d626472e2d03779a5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20L=C3=A9tendart?= Date: Mon, 18 May 2020 17:06:21 +0200 Subject: [PATCH] plugin: Set unused_tcp_port_factory scope to 'session' Factories in pytest usually have a scope greater than 'function' to let one use the same factory within bigger scopes. Let us allow unused_tcp_port_factory to be used throughout the same session scope. This will let other session-scoped factories depend on unused_tcp_port_factory without getting a "ScopeMismatch" error. --- README.rst | 2 ++ pytest_asyncio/plugin.py | 2 +- tests/conftest.py | 7 +++++++ tests/test_dependent_fixtures.py | 5 +++++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 2f282b5e..aa757b68 100644 --- a/README.rst +++ b/README.rst @@ -166,6 +166,8 @@ Changelog ~~~~~~~~~~~~~~~~~~~ - Add support for Python 3.9 - Abandon support for Python 3.5. If you still require support for Python 3.5, please use pytest-asyncio v0.14 or earlier. +- Set ``unused_tcp_port_factory`` fixture scope to 'session'. + `#163 `_ 0.14.0 (2020-06-24) ~~~~~~~~~~~~~~~~~~~ diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 4b7d6fd0..0f29be12 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -219,7 +219,7 @@ def unused_tcp_port(): return _unused_tcp_port() -@pytest.fixture +@pytest.fixture(scope='session') def unused_tcp_port_factory(): """A factory function, producing different unused TCP ports.""" produced = set() diff --git a/tests/conftest.py b/tests/conftest.py index 1ca63fe5..9e046123 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -20,3 +20,10 @@ async def just_a_sleep(): event_loop.run_until_complete(just_a_sleep()) assert counter == 2 + + +@pytest.fixture(scope='session', name='factory_involving_factories') +def factory_involving_factories_fixture(unused_tcp_port_factory): + def factory(): + return unused_tcp_port_factory() + return factory diff --git a/tests/test_dependent_fixtures.py b/tests/test_dependent_fixtures.py index db2252b2..2876255b 100644 --- a/tests/test_dependent_fixtures.py +++ b/tests/test_dependent_fixtures.py @@ -6,3 +6,8 @@ async def test_dependent_fixture(dependent_fixture): """Test a dependent fixture.""" await asyncio.sleep(0.1) + + +@pytest.mark.asyncio +async def test_factory_involving_factories(factory_involving_factories): + factory_involving_factories()