Fix pytest-asyncio deprecation warning by setting default loop scope - #4802
Fix pytest-asyncio deprecation warning by setting default loop scope#4802Vishal Goyal (CodeVishal-17) wants to merge 21 commits into
Conversation
|
Hi Vishal Goyal (@CodeVishal-17) have you been able to run I've been able to fix them by using @pytest.fixture instead of @pytest_asyncio.fixture for those tests setups Those test setups aren't async) Also I've added a pyproject.toml at the root to specify options for bandit and pytest in a single file as when running |
James Chapman (JC-wk)
left a comment
There was a problem hiding this comment.
This works with pytest-asyncio v0.24.0 current devcontainer version but not with v1.3.0 (latest)
try with pip install --upgrade pytest-asyncio
|
Thanks for the clarification 👍 That makes sense — I was testing against pytest-asyncio==0.24.0, which explains why it passes locally but fails with v1.3.0. I’ll upgrade to the latest version and adjust the fixtures accordingly. |
|
@microsoft-github-policy-service agree |
|
James Chapman (@JC-wk) Thanks for the clarification! I upgraded locally to pytest-asyncio==1.3.0 and reproduced the issue. I’ve fixed the async fixtures to use @pytest_asyncio.fixture (including making the app fixture async) and verified that the pytest-asyncio deprecation / loop-scope issue is resolved under asyncio_mode=strict. Local test collection failures are due to missing optional runtime dependencies (Azure SDKs / FastAPI / Starlette), which are provided in CI and unrelated to this change. Please let me know if anything else needs adjustment. |
There was a problem hiding this comment.
the other change required is in, then all the tests pass for me
api_app/tests_ma/test_api/test_routes/test_airlock.py
change line 130 to @pytest.fixture(autouse=True, scope='class')
change line 306 to @pytest.fixture(autouse=True, scope='class')
see main...JC-wk:AzureTRE:fix/pytest-asyncio-deprecation#diff-5013d846e8403adeed48f3a9b720e7c4b610b02a783a54854bc0432d84bcd22aL130-R130
|
James Chapman (@JC-wk) thanks again for the detailed review! I’ve applied the last requested change (switching the remaining fixtures to pytest.fixture with the suggested scopes), and all tests now pass locally for me under pytest-asyncio >= 1.3.0. Could you please take another look when you get a chance? Happy to adjust anything further if needed. |
Hi, there are still some comments which have not been addressed as far as I can see? Thanks |
There was a problem hiding this comment.
Pull request overview
This PR attempts to fix a pytest-asyncio deprecation warning by configuring the asyncio mode and default fixture loop scope. However, the implementation contains several critical issues and includes unrelated changes that should be addressed separately.
Changes:
- Added pytest configuration for asyncio mode and default loop scope in pytest.ini
- Converted pytest_asyncio.fixture decorators to pytest.fixture for non-async fixtures
- Modified the AsyncClient fixture implementation in conftest.py
- Added unrelated bandit security tool configuration and CI/CD integration
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| api_app/pytest.ini | Added asyncio_mode and asyncio_default_fixture_loop_scope configuration (inconsistent with PR description) |
| api_app/tests_ma/test_api/test_routes/test_airlock.py | Removed pytest_asyncio import, changed fixture decorators, removed pytestmark, reformatted patch statements |
| api_app/tests_ma/test_api/conftest.py | Changed fixture decorators, modified AsyncClient fixture to remove async context manager |
| api_app/tests_ma/conftest.py | Changed no_database fixture from async to sync |
| pyproject.toml | Added bandit security tool configuration (unrelated to PR purpose) |
| .github/workflows/build_validation_develop.yml | Added bandit security checks to CI/CD (unrelated to PR purpose) |
…odeVishal-17/AzureTRE into fix/pytest-asyncio-deprecation
| import pytest | ||
| import pytest_asyncio | ||
|
|
||
| pytestmark = pytest.mark.asyncio |
|
|
||
| the_app = get_application() | ||
| return the_app | ||
|
|
| asyncio_mode = strict | ||
| asyncio_default_fixture_loop_scope = function |
|
Closing this mixed branch. A clean PR will be opened with only the pytest-asyncio loop-scope configuration change. |
|
moved to #5055 Vishal Goyal (@CodeVishal-17) |
Resolves #4778
Summary
This PR fixes a pytest-asyncio deprecation warning by explicitly configuring the asyncio mode and default event loop scope for test fixtures.
Details
Pytest emits a
PytestDeprecationWarningindicating that the configuration optionasyncio_default_fixture_loop_scopeis unset. With newer versions ofpytest-asyncio(>= 1.3.0), this will become an error unless explicitly configured.How is this addressed
asyncio_mode = strictinpytest.ini(intentional and aligned with pytest-asyncio >= 1.3.0)asyncio_default_fixture_loop_scope = functionAll tests pass locally under
pytest-asyncio >= 1.3.0.