Skip to content

Commit 5c01e01

Browse files
author
Romain Létendart
committed
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.
1 parent 7a255bc commit 5c01e01

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

pytest_asyncio/plugin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def unused_tcp_port():
198198
return _unused_tcp_port()
199199

200200

201-
@pytest.fixture
201+
@pytest.fixture(scope='session')
202202
def unused_tcp_port_factory():
203203
"""A factory function, producing different unused TCP ports."""
204204
produced = set()

tests/conftest.py

+7
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ async def just_a_sleep():
2626
event_loop.run_until_complete(just_a_sleep())
2727

2828
assert counter == 2
29+
30+
31+
@pytest.fixture(scope='session', name='factory_involving_factories')
32+
def factory_involving_factories_fixture(unused_tcp_port_factory):
33+
def factory():
34+
return unused_tcp_port_factory()
35+
return factory

tests/test_dependent_fixtures.py

+5
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@
66
async def test_dependent_fixture(dependent_fixture):
77
"""Test a dependent fixture."""
88
await asyncio.sleep(0.1)
9+
10+
11+
@pytest.mark.asyncio
12+
async def test_factory_involving_factories(factory_involving_factories):
13+
factory_involving_factories()

0 commit comments

Comments
 (0)