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

Bumps pytest and pytest-asyncio to the latest versions #15523

Merged
merged 2 commits into from
Sep 27, 2024

Conversation

chrisguidry
Copy link
Collaborator

We were blocked from upgrading pytest because we were redefining the
event_loop fixture, which has been deprecated for some time in
pytest-asyncio. Here we remove that fixture and set the appropriate
configuration per their docs:

  • Automatically marks all async tests as being marked with the asyncio fixture
    set to `loop_scope="session"
  • Make the default fixture loop scope session as well (in setup.cfg)
  • In the case where we need to switch to a different policy, like for Windows
    support, override the event_loop_policy fixture

One issue emerged here, where any test that called asyncio.run or anyio.run
would leave the test clearing the event loop from the 'MainThread', thus
poisoning the environment for all subsequent tests. Adding a fixture that
restores the session-level pytest-asyncio event loop after each test function
prevents that issue.

Fixes #14559

Checklist

  • This pull request references any related issue by including "closes <link to issue>"
    • If no issue exists and your change is not a small fix, please create an issue first.
  • If this pull request adds new functionality, it includes unit tests that cover the changes
  • If this pull request removes docs files, it includes redirect settings in mint.json.
  • If this pull request adds functions or classes, it includes helpful docstrings.

We were blocked from upgrading `pytest` because we were redefining the
`event_loop` fixture, which has been deprecated for some time in
`pytest-asyncio`.  Here we remove that fixture and set the appropriate
configuration per their [docs][1]:

* Automatically marks all async tests as being marked with the `asyncio` fixture
  set to `loop_scope="session"
* Make the default fixture loop scope `session` as well (in `setup.cfg`)
* In the case where we need to switch to a different policy, like for Windows
  support, override the `event_loop_policy` fixture

One issue emerged here, where any test that called `asyncio.run` or `anyio.run`
would leave the test clearing the event loop from the `'MainThread'`, thus
poisoning the environment for all subsequent tests.  Adding a fixture that
restores the session-level `pytest-asyncio` event loop after each test function
prevents that issue.

Fixes #14559

[1]: https://pytest-asyncio.readthedocs.io/en/latest/how-to-guides/run_session_tests_in_same_loop.html
@github-actions github-actions bot added the upstream dependency An upstream issue caused by a bug in one of our dependencies label Sep 27, 2024
@@ -1,24 +0,0 @@
import pytest
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I discovered this file was a no-op (there is a fixture, but no tests, so I just removed the whole thing)

pytest > 7, < 9
pytest-asyncio >= 0.18.2, != 0.22.0, < 0.23.0 # Cannot override event loop in 0.23.0. See https://github.com/pytest-dev/pytest-asyncio/issues/706 for more details.
pytest >= 8.3
pytest-asyncio >= 0.24
Copy link
Collaborator

Choose a reason for hiding this comment

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

💅

Copy link

codspeed-hq bot commented Sep 27, 2024

CodSpeed Performance Report

Merging #15523 will not alter performance

Comparing pytest-asyncio-bump (4071db2) with main (05ec2b9)

Summary

✅ 3 untouched benchmarks

@desertaxle desertaxle added the development Tech debt, refactors, CI, tests, and other related work. label Sep 27, 2024
Copy link
Member

@desertaxle desertaxle left a comment

Choose a reason for hiding this comment

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

🔥

@chrisguidry chrisguidry merged commit 1f9890c into main Sep 27, 2024
31 checks passed
@chrisguidry chrisguidry deleted the pytest-asyncio-bump branch September 27, 2024 19:34
@alexted
Copy link

alexted commented Oct 1, 2024

Have you considered getting rid of the use of pytest-asyncio altogether?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
development Tech debt, refactors, CI, tests, and other related work. upstream dependency An upstream issue caused by a bug in one of our dependencies
Projects
None yet
Development

Successfully merging this pull request may close these issues.

We are redefining the event_loop fixture of pytest-asyncio in a deprecated way
4 participants