Skip to content

Deprecate unused_tcp_port, unused_tcp_port_factory, unused_udp_port and unused_udp_port_factory fixtures - #1626

Open
saifmodan2006 wants to merge 2 commits into
pytest-dev:mainfrom
saifmodan2006:deprecate-unused-port-fixtures-1614
Open

saifmodan2006 wants to merge 2 commits into
pytest-dev:mainfrom
saifmodan2006:deprecate-unused-port-fixtures-1614

Conversation

@saifmodan2006

Copy link
Copy Markdown

Fixes #1614

The unused port fixtures bind a temporary socket to port 0 and close it before returning the port number, so another process can claim the port before the test server starts. This deprecates all four fixtures and documents how to migrate.

Changes:

  • pytest_asyncio/plugin.py: each of unused_tcp_port, unused_tcp_port_factory, unused_udp_port and unused_udp_port_factory now emits a PytestDeprecationWarning on use, with migration guidance in the message (bind port 0 and read the assigned port from the server socket or transport).
  • docs/reference/fixtures/index.rst: deprecation notes with migration guidance for each fixture (TCP: pass port=0 to asyncio.start_server() and read server.sockets[0].getsockname(); UDP: pass local_addr=("127.0.0.1", 0) to loop.create_datagram_endpoint() and read transport.get_extra_info("sockname")).
  • changelog.d/1614.deprecated.rst: changelog fragment.

Tests run:

  • python -m pytest tests/test_port_factories.py: 9 passed. The two existing duplicate-port tests now expect the warning, and new tests assert all four fixtures emit PytestDeprecationWarning (directly for the function-scoped fixtures, and in a real pytest run via pytester for all four).
  • Full suite python -m pytest tests/: 263 passed, 3 skipped, 1 failed. The single failure is test_set_event_loop.py::test_asyncio_run_after_async_fixture_does_not_leak_loop, which fails identically on unmodified main under the same editable install (PytestAssertRewriteWarning under -W error in the inner subprocess run), so it is unrelated to this change.
  • pre-commit (ruff, shed, mypy and the rest) passes on the changed files.

…nd unused_udp_port_factory fixtures

The fixtures bind a temporary socket to port 0 and close it before
returning the port number, so another process can claim the port before
the test server starts. Emit a PytestDeprecationWarning on use and
document the migration: bind port 0 and read the assigned port from the
server socket or transport.

Fixes pytest-dev#1614
…er warning defaults

Add configuration for asyncio default fixture loop scope
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.89%. Comparing base (be39d14) to head (d3a91d4).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1626      +/-   ##
==========================================
+ Coverage   94.86%   95.89%   +1.03%     
==========================================
  Files           2        2              
  Lines         506      512       +6     
  Branches       60       60              
==========================================
+ Hits          480      491      +11     
+ Misses         21       16       -5     
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deprecate the unused TCP and UDP port fixtures

2 participants