Skip to content

Commit 492b8f9

Browse files
mishushakovclaude
andcommitted
Migrate conftest.py to pytest-asyncio 1.x
The session-scoped event_loop fixture override is no longer honored in pytest-asyncio 1.x. Replace it with `asyncio_default_fixture_loop_scope` and `asyncio_default_test_loop_scope` set to session in pytest.ini, and convert async_sandbox_factory to an async fixture that yields and awaits sandbox cleanup directly instead of calling run_until_complete from a sync finalizer. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 20ef3ce commit 492b8f9

2 files changed

Lines changed: 11 additions & 25 deletions

File tree

python/pytest.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
[pytest]
33
markers =
44
skip_debug: skip test if E2B_DEBUG is set.
5-
asyncio_mode=auto
5+
asyncio_mode = auto
6+
asyncio_default_fixture_loop_scope = session
7+
asyncio_default_test_loop_scope = session
68

79
addopts = "--import-mode=importlib" "--numprocesses=2"

python/tests/conftest.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import asyncio
21
import os
32

43
import pytest
@@ -43,39 +42,24 @@ def sandbox(sandbox_factory):
4342
return sandbox_factory()
4443

4544

46-
# override the event loop so it never closes
47-
# this helps us with the global-scoped async http transport
48-
@pytest.fixture(scope="session")
49-
def event_loop():
50-
try:
51-
loop = asyncio.get_running_loop()
52-
except RuntimeError:
53-
loop = asyncio.new_event_loop()
54-
yield loop
55-
loop.close()
56-
57-
5845
@pytest.fixture
59-
def async_sandbox_factory(request, template, sandbox_test_id, event_loop):
46+
async def async_sandbox_factory(template, sandbox_test_id):
47+
sandboxes: list[AsyncSandbox] = []
48+
6049
async def factory(*, template_name: str = template, **kwargs):
6150
kwargs.setdefault("timeout", 60)
6251

6352
metadata = kwargs.setdefault("metadata", dict())
6453
metadata.setdefault("sandbox_test_id", sandbox_test_id)
6554

6655
sandbox = await AsyncSandbox.create(template_name, **kwargs)
67-
68-
def kill():
69-
async def _kill():
70-
await sandbox.kill()
71-
72-
event_loop.run_until_complete(_kill())
73-
74-
request.addfinalizer(kill)
75-
56+
sandboxes.append(sandbox)
7657
return sandbox
7758

78-
return factory
59+
yield factory
60+
61+
for sandbox in sandboxes:
62+
await sandbox.kill()
7963

8064

8165
@pytest.fixture

0 commit comments

Comments
 (0)