Skip to content

Commit

Permalink
Merge pull request #97 from JoseKilo/fix/pytest-warning-calling-fixtu…
Browse files Browse the repository at this point in the history
…re-as-function

Fix: Avoid warning on latest Pytest versions
  • Loading branch information
Tinche committed Oct 2, 2018
2 parents 1ada096 + 7087f57 commit 923fe9a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 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
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ show_missing = true
[tool:pytest]
addopts = -rsx --tb=short
testpaths = tests
filterwarnings = error

[metadata]
# ensure LICENSE is included in wheel metadata
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

0 comments on commit 923fe9a

Please sign in to comment.