Skip to content

Commit

Permalink
Fix: Avoid warning on latest Pytest versions
Browse files Browse the repository at this point in the history
Pytest added a deprecation warning to signal fixtures being called
directly as functions.

Ref: pytest-dev/pytest#3661
Ref: https://docs.pytest.org/en/latest/deprecations.html#calling-fixtures-directly
  • Loading branch information
JoseKilo committed Sep 27, 2018
1 parent 1ada096 commit ade2121
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,25 +174,29 @@ def event_loop(request):
loop.close()


@pytest.fixture
def unused_tcp_port():
def _unused_tcp_port():
"""Find an unused localhost TCP port from 1024-65535 and return it."""
with contextlib.closing(socket.socket()) as sock:
sock.bind(('127.0.0.1', 0))
return sock.getsockname()[1]


@pytest.fixture
def unused_tcp_port():
return _unused_tcp_port()


@pytest.fixture
def unused_tcp_port_factory():
"""A factory function, producing different unused TCP ports."""
produced = set()

def factory():
"""Return an unused port."""
port = unused_tcp_port()
port = _unused_tcp_port()

while port in produced:
port = unused_tcp_port()
port = _unused_tcp_port()

produced.add(port)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def mock_unused_tcp_port():
else:
return 10000 + counter

monkeypatch.setattr(pytest_asyncio.plugin, 'unused_tcp_port',
monkeypatch.setattr(pytest_asyncio.plugin, '_unused_tcp_port',
mock_unused_tcp_port)

assert unused_tcp_port_factory() == 10000
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ minversion = 2.5.0

[testenv]
extras = testing
commands = coverage run -m pytest {posargs}
commands = coverage run -m pytest -W error {posargs}

[testenv:coverage-report]
deps = coverage
Expand Down

0 comments on commit ade2121

Please sign in to comment.