Skip to content

Commit 2262350

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 1bb7f30 commit 2262350

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

README.rst

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ Changelog
166166
~~~~~~~~~~~~~~~~~~~
167167
- Add support for Python 3.9
168168
- Abandon support for Python 3.5. If you still require support for Python 3.5, please use pytest-asyncio v0.14 or earlier.
169+
- Set ``unused_tcp_port_factory`` fixture scope to 'session'.
169170

170171
0.14.0 (2020-06-24)
171172
~~~~~~~~~~~~~~~~~~~

pytest_asyncio/plugin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def unused_tcp_port():
219219
return _unused_tcp_port()
220220

221221

222-
@pytest.fixture
222+
@pytest.fixture(scope='session')
223223
def unused_tcp_port_factory():
224224
"""A factory function, producing different unused TCP ports."""
225225
produced = set()

tests/conftest.py

+7
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ async def just_a_sleep():
2020
event_loop.run_until_complete(just_a_sleep())
2121

2222
assert counter == 2
23+
24+
25+
@pytest.fixture(scope='session', name='factory_involving_factories')
26+
def factory_involving_factories_fixture(unused_tcp_port_factory):
27+
def factory():
28+
return unused_tcp_port_factory()
29+
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)