Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Configuration option for setting default loop_scope for tests #1035

Merged

Conversation

Novakov
Copy link
Contributor

@Novakov Novakov commented Jan 6, 2025

New configuration option, asyncio_default_test_loop_scope, provides default value for loop_scope argument of asyncio marker. This can be used to use the same event loop in auto mode without need to use modifyitems hook.

Test functions can still override loop_scope by using asyncio marker.

Implements #793

@Novakov Novakov force-pushed the config-option-default-test-loop-scope branch 4 times, most recently from cb06135 to 5272cc4 Compare January 6, 2025 11:53
@codecov-commenter
Copy link

codecov-commenter commented Jan 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.27%. Comparing base (2188cdb) to head (6c3a941).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1035      +/-   ##
==========================================
+ Coverage   91.13%   91.27%   +0.13%     
==========================================
  Files           2        2              
  Lines         564      573       +9     
  Branches       74       75       +1     
==========================================
+ Hits          514      523       +9     
  Misses         30       30              
  Partials       20       20              

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

Copy link
Contributor

@seifertm seifertm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I really like how you changed _get_marked_loop_scope and how you used functions to keep the code readable.

Can you please add an entry to docs/reference/changelog.rst where you link to #793 ?
Assume that this PR bumps the minor version and set the date to UNRELEASED.

docs/how-to-guides/index.rst Show resolved Hide resolved
@seifertm seifertm linked an issue Jan 8, 2025 that may be closed by this pull request
@Novakov Novakov force-pushed the config-option-default-test-loop-scope branch 2 times, most recently from 787a67d to aa2339d Compare January 8, 2025 16:17
New configuration option, asyncio_default_test_loop_scope, provides
default value for loop_scope argument of asyncio marker. This can be
used to use the same event loop in auto mode without need to use
modifyitems hook.

Test functions can still override loop_scope by using asyncio marker.
@Novakov Novakov force-pushed the config-option-default-test-loop-scope branch from aa2339d to 6c3a941 Compare January 8, 2025 16:20
@Novakov
Copy link
Contributor Author

Novakov commented Jan 8, 2025

Thanks for kind words. I've rebased this branch and applied modifications you requested.

@seifertm seifertm self-requested a review January 15, 2025 05:51
Copy link
Contributor

@seifertm seifertm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@seifertm seifertm added this pull request to the merge queue Jan 15, 2025
@seifertm seifertm added this to the v0.26 milestone Jan 15, 2025
Merged via the queue into pytest-dev:main with commit 5a0c567 Jan 15, 2025
15 checks passed
@Novakov Novakov deleted the config-option-default-test-loop-scope branch January 15, 2025 07:20
@michaelxiacs
Copy link

Thanks!

Is this feature currently available in pytest-asyncio>=0.25.2?

I try to set asyncio_default_test_loop_scope = session in pytest.ini and it doesn't seem to work.

pytest.ini

[pytest]
addopts = -p no:cacheprovider
asyncio_mode = auto
asyncio_default_fixture_loop_scope = session
asyncio_default_test_loop_scope = session

test_demo.py

import asyncio


async def test1():
    loop_id = id(asyncio.get_running_loop())
    print(test1.__name__, loop_id)
    assert True


async def test2():
    loop_id = id(asyncio.get_running_loop())
    print(test2.__name__, loop_id)
    assert True

Here is the result when I run it

> uv run pytest -s
========================================================= test session starts ==========================================================
platform win32 -- Python 3.11.11, pytest-8.3.4, pluggy-1.5.0
rootdir: C:\Users\Abon\Documents\Code\Projects\python\dev\tests
configfile: pytest.ini
plugins: asyncio-0.25.2
asyncio: mode=Mode.AUTO, asyncio_default_fixture_loop_scope=session
collected 2 items

test_demo.py test1 1592080607312
.test2 1592108632464
.

=========================================================== warnings summary =========================================================== 
..\.venv\Lib\site-packages\_pytest\config\__init__.py:1441
  C:\Users\Abon\Documents\Code\Projects\python\dev\.venv\Lib\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
===================================================== 2 passed, 1 warning in 0.03s ===================================================== 

@Novakov
Copy link
Contributor Author

Novakov commented Jan 25, 2025

Is this feature currently available in pytest-asyncio>=0.25.2?

Unfortunately it was merged after 0.25.2 was released.

@jassa
Copy link

jassa commented Jan 29, 2025

looks like it also didn't make it into 0.25.3 but this made my tests much cleaner

# requirements.txt
pytest-asyncio @ git+https://github.com/pytest-dev/pytest-asyncio@e8de4dc37a707be7d040609c47ec477bab681f29
# pytest.ini
[pytest]
asyncio_mode = auto
asyncio_default_test_loop_scope = session
asyncio_default_fixture_loop_scope = session
# contest.py
@pytest.fixture(scope="session", autouse=True)
async def my_test_session_helper():
    ...
$ pytest
configfile: pytest.ini
plugins: asyncio-0.25.3.dev12+ge8de4dc, anyio-4.4.0
asyncio: mode=auto, asyncio_default_fixture_loop_scope=session, asyncio_default_test_loop_scope=session
collected ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Configuration option for default loop scope
5 participants