Replies: 3 comments
-
Disclaimer: I'm one of the maintainers and I cannot answer for the author of pytest-asyncio. Maybe @Tinche can chime in on this. The default behavior of pytest-asyncio is to run each test case in a separate asyncio event loop. This should guarantee isolation between tests. However, there are valid use cases, where the event loop should persist across multiple test cases, for example when the user starts a web server and runs API requests against that server to verify its behavior. The officially supported way to achieve this is to redefine the pytest-asyncio v0.21.0 has the additional limitation that event_loop fixture scopes must not overlap or they will trigger a DeprecationWarning (see #531 ). As such, it is preferred to set a scope that is narrower than session, especially if you want to run unit tests (with per-test event loop isolation) and larger-than-unit tests (with larger event loop scope) in the same session. |
Beta Was this translation helpful? Give feedback.
-
Second this as a useful feature for using pytest-asyncio with an aioodbc testcontainer. If the event loop is not scoped to the session, the teardown procedure attempts to close an event loop that is already closed and returns a RuntimeError
|
Beta Was this translation helpful? Give feedback.
-
Session-scoped loops have been available for some time via overrides of the event_loop fixture. Starting from pytest-asyncio v0.24, there's a configuration option to run all async fixtures in the same loop and #793 tracks a similar configuration option for all async tests. In my opinion, this should be enough to make session-scoped loops a first-class feature. |
Beta Was this translation helpful? Give feedback.
-
Originally posted here: #531 (comment) by @serpulga
I encountered the issue function scoped fixture event_loop with a session scoped request object, which forces me to create an event loop fixture that is session scoped.
My question is, how does the author(s) feel about this approach? It seems the intended use-case was for the event loop to be function-scoped. Is it common enough to call for "official" support for session-scoped even loops?
Beta Was this translation helpful? Give feedback.
All reactions