Skip to content

Commit

Permalink
plugin: Set unused_tcp_port_factory scope to 'session'
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Romain Létendart committed May 18, 2020
1 parent 7a255bc commit 5c01e01
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,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()
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,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
5 changes: 5 additions & 0 deletions tests/test_dependent_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 5c01e01

Please sign in to comment.