I have been having a lot of problems with pytest hanging when trying to test a fastapi server.
I finally narrowed down one clear case (though I think I am facing others, as well): if the websocket endpoint returns without accepting the websocket then the test will hang.
Steps to reproduce the behavior:
pytest test_trivial_hang.py
/opt/miniconda3/envs/web/lib/python3.12/site-packages/pytest_asyncio/plugin.py:208: PytestDeprecationWarning: The configuration option "asyncio_default_fixture_loop_scope" is unset.
The event loop scope for asynchronous fixtures will default to the fixture caching scope. Future versions of pytest-asyncio will default the loop scope for asynchronous fixtures to function scope. Set the default fixture loop scope explicitly in order to avoid unexpected behavior in the future. Valid fixture loop scopes are: "function", "class", "module", "package", "session"
warnings.warn(PytestDeprecationWarning(_DEFAULT_FIXTURE_LOOP_SCOPE_UNSET))
========================================================================================= test session starts ==========================================================================================
platform darwin -- Python 3.12.5, pytest-8.3.2, pluggy-1.5.0
rootdir: /Users/rowen/Library/Mobile Documents/com~apple~CloudDocs/Notes/Weaving/Software/minimal_websocket_example
plugins: asyncio-0.24.0, anyio-4.4.0, subtests-0.13.1
asyncio: mode=Mode.STRICT, default_loop_scope=None
collected 1 item
test_trivial_hang.py ^C
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! KeyboardInterrupt !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/opt/miniconda3/envs/web/lib/python3.12/selectors.py:566: KeyboardInterrupt
(to show a full traceback on KeyboardInterrupt use --full-trace)
======================================================================================== no tests ran in 2.19s =========================================================================================
^CException ignored in: <module 'threading' from '/opt/miniconda3/envs/web/lib/python3.12/threading.py'>
Traceback (most recent call last):
File "/opt/miniconda3/envs/web/lib/python3.12/threading.py", line 1594, in _shutdown
atexit_call()
File "/opt/miniconda3/envs/web/lib/python3.12/concurrent/futures/thread.py", line 31, in _python_exit
t.join()
File "/opt/miniconda3/envs/web/lib/python3.12/threading.py", line 1149, in join
self._wait_for_tstate_lock()
File "/opt/miniconda3/envs/web/lib/python3.12/threading.py", line 1169, in _wait_for_tstate_lock
if lock.acquire(block, timeout):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyboardInterrupt:
Task was destroyed but it is pending!
I should be able to run tests using pytest without the test hanging.
Add any other context about the problem here.
test_trivial_hang.py.zip
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too