pytest-dev/pytest-asyncio

PytestConfigWarning: Unknown config option: asyncio_default_test_loop_scope

voelcker-itsc opened this issue · 4 comments

When using the option asyncio_default_test_loop_scope in my pytest.ini as described in How to change the default event loop scope of all tests, the option is ignored.
The warning PytestConfigWarning: Unknown config option: asyncio_default_test_loop_scope is shown.

I have tried to follow the instructions in the documentation.
Not sure if I am doing something wrong or if this is a bug.

Minimal Example

pytest.ini

[pytest]
asyncio_mode = auto
asyncio_default_test_loop_scope = session
asyncio_default_fixture_loop_scope = session

test/test_loop_scope.py

import asyncio

loop: asyncio.AbstractEventLoop


async def test_remember_loop():
    global loop
    loop = asyncio.get_running_loop()


async def test_assert_same_loop():
    global loop
    assert asyncio.get_running_loop() is loop

Output

$ pytest -v    
================================================================================================================================================= test session starts ==================================================================================================================================================
platform darwin -- Python 3.13.2, pytest-8.3.5, pluggy-1.5.0 -- <home>/.local/share/virtualenvs/test_loop_scope-zc4eW47F/bin/python
cachedir: .pytest_cache
rootdir: <home>/test_loop_scope
configfile: pytest.ini
plugins: asyncio-0.25.3
asyncio: mode=Mode.AUTO, asyncio_default_fixture_loop_scope=session
collected 2 items                                                                                                                                                                                                                                                                                                      

tests/test_loop_scope.py::test_remember_loop PASSED                                                                                                                                                                                                                                                              [ 50%]
tests/test_loop_scope.py::test_assert_same_loop FAILED                                                                                                                                                                                                                                                           [100%]

======================================================================================================================================================= FAILURES =======================================================================================================================================================
________________________________________________________________________________________________________________________________________________ test_assert_same_loop _________________________________________________________________________________________________________________________________________________

    async def test_assert_same_loop():
        global loop
>       assert asyncio.get_running_loop() is loop
E       assert <_UnixSelectorEventLoop running=True closed=False debug=False> is <_UnixSelectorEventLoop running=False closed=True debug=False>
E        +  where <_UnixSelectorEventLoop running=True closed=False debug=False> = <built-in function get_running_loop>()
E        +    where <built-in function get_running_loop> = asyncio.get_running_loop

tests/test_loop_scope.py:16: AssertionError
=================================================================================================================================================== warnings summary ===================================================================================================================================================
../../../.local/share/virtualenvs/test_loop_scope-zc4eW47F/lib/python3.13/site-packages/_pytest/config/__init__.py:1441
  <home>/.local/share/virtualenvs/test_loop_scope-zc4eW47F/lib/python3.13/site-packages/_pytest/config/__init__.py:1441: PytestConfigWarning: Unknown config option: asyncio_default_test_loop_scope
  
    self._warn_or_fail_if_strict(f"Unknown config option: {key}\n")

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=============================================================================================================================================== short test summary info ================================================================================================================================================
FAILED tests/test_loop_scope.py::test_assert_same_loop - assert <_UnixSelectorEventLoop running=True closed=False debug=False> is <_UnixSelectorEventLoop running=False closed=True debug=False>
======================================================================================================================================== 1 failed, 1 passed, 1 warning in 0.02s ========================================================================================================================================

/.local/share/virtualenvs/test_loop_scope-zc4eW47F/lib/python3.13/site-packages/_pytest/config/init.py:1441: PytestConfigWarning: Unknown config option: asyncio_default_test_loop_scope
The other options are recognized and seem to work as expected.

Failing test

I cloned the pytest-asyncio repository, installed the dependencies in 'dependencies/default/constraints.txt' and ran the tests.
The test test_asyncio_marker_fallbacks_to_configured_default_loop_scope_if_not_set failed with a similar error. All the other tests passed.

$ pytest tests/test_asyncio_mark.py 
=========================================================================================================================================== test session starts ===========================================================================================================================================
platform darwin -- Python 3.13.2, pytest-8.3.5, pluggy-1.5.0
rootdir: <home>/Projects/Packages/pytest-asyncio
configfile: pyproject.toml
plugins: asyncio-0.25.3, hypothesis-6.127.5
asyncio: mode=Mode.AUTO, asyncio_default_fixture_loop_scope=function
collected 9 items                                                                                                                                                                                                                                                                                         

tests/test_asyncio_mark.py .......F.                                                                                                                                                                                                                                                                [100%]

================================================================================================================================================ FAILURES =================================================================================================================================================
________________________________________________________________________________________________________________ test_asyncio_marker_fallbacks_to_configured_default_loop_scope_if_not_set ________________________________________________________________________________________________________________
<home>/Projects/Packages/pytest-asyncio/tests/test_asyncio_mark.py:186: in test_asyncio_marker_fallbacks_to_configured_default_loop_scope_if_not_set
    result.assert_outcomes(passed=1)
E   AssertionError: assert {'errors': 0,...pped': 0, ...} == {'errors': 0,...pped': 0, ...}
E     
E     Omitting 4 identical items, use -vv to show
E     Differing items:
E     {'failed': 1} != {'failed': 0}
E     {'passed': 0} != {'passed': 1}
E     Use -v to get more diff
------------------------------------------------------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------------------------------------------------------
============================= test session starts ==============================
platform darwin -- Python 3.13.2, pytest-8.3.5, pluggy-1.5.0
rootdir: /private/var/folders/zj/1gymhj_x4f57xml0lmwdl_7w0000gp/T/pytest-of-name/pytest-5/test_asyncio_marker_fallbacks_to_configured_default_loop_scope_if_not_set0
configfile: tox.ini
plugins: asyncio-0.25.3, hypothesis-6.127.5
asyncio: mode=Mode.AUTO, asyncio_default_fixture_loop_scope=function
collected 1 item

test_asyncio_marker_fallbacks_to_configured_default_loop_scope_if_not_set.py F [100%]

=================================== FAILURES ===================================
____________________________________ test_a ____________________________________

session_loop_fixture = None

    async def test_a(session_loop_fixture):
        global loop
>       assert asyncio.get_running_loop() is loop
E       assert <_UnixSelectorEventLoop running=True closed=False debug=False> is <_UnixSelectorEventLoop running=False closed=False debug=False>
E        +  where <_UnixSelectorEventLoop running=True closed=False debug=False> = <built-in function get_running_loop>()
E        +    where <built-in function get_running_loop> = asyncio.get_running_loop

test_asyncio_marker_fallbacks_to_configured_default_loop_scope_if_not_set.py:14: AssertionError
=============================== warnings summary ===============================
<home>/.local/share/virtualenvs/test_loop_scope-zc4eW47F/lib/python3.13/site-packages/_pytest/config/__init__.py:1441
  <home>/.local/share/virtualenvs/test_loop_scope-zc4eW47F/lib/python3.13/site-packages/_pytest/config/__init__.py:1441: PytestConfigWarning: Unknown config option: asyncio_default_test_loop_scope
  
    self._warn_or_fail_if_strict(f"Unknown config option: {key}\n")

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=========================== short test summary info ============================
FAILED test_asyncio_marker_fallbacks_to_configured_default_loop_scope_if_not_set.py::test_a
========================= 1 failed, 1 warning in 0.09s =========================

System

I am using macOS 15.3.1, python 3.13.2 and pipenv.
But I have also tested it in a docker container (python:3.X.X-alpine) with pip directly. The result was the same.

I've tested for different pytest-asyncio, pytest and python versions, but could not find a combination where the option was recognized.

Workaround

Using @pytest.mark.asyncio(loop_scope="session") or pytestmark = pytest.mark.asyncio(loop_scope="session") works as expected.

Thanks for the detailed issue!

The referenced How-To Guide links to the latest version of the documentation. Unlike the stable documentation, latest may contain unreleased changes, such as the asyncio_default_test_loop_scope configuration option.

This confusion already affected several user, which is unfortunate. I'm open to suggestions how the docs can be improved in that regard.

I'm currently unable to release a new version pytest-asyncio, but you can expect a release in week 12.

Thanks for the response, that might explain my issues.

Since the default version is 'latest', this is also what you find via Google. And in my experience 'latest' usually refers to the latest stable version.
I am not really familiar with Read the Docs or your workflows, but maybe it is possible to push the unreleased Changes only as a separate version like 'unstable'? And then only promote it to 'latest' once it is stable, so that the default version 'latest' is always the latest stable release.
Or maybe just push the docs for stable releases at all?

I made "stable" the default version and marked "latest" as hidden, which removes it from the version selection and from the search indexing.