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 Nov 30, 2020
1 parent 1bb7f30 commit 919638f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.rst
Expand Up @@ -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 <https://github.com/pytest-dev/pytest-asyncio/pull/163>`_

0.14.0 (2020-06-24)
~~~~~~~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion pytest_asyncio/plugin.py
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Expand Up @@ -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
5 changes: 5 additions & 0 deletions tests/test_dependent_fixtures.py
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 919638f

Please sign in to comment.