Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: allow test cells in the presence of async cells (#6355)

Dylan Madisetti0076848871a8
diff --git a/marimo/_ast/pytest.py b/marimo/_ast/pytest.py
index 2a5ec8b57a..2bc38fc6bb 100644
--- a/marimo/_ast/pytest.py
+++ b/marimo/_ast/pytest.py
@@ -6,6 +6,7 @@ import copy
import functools
import inspect
import itertools
+from collections.abc import Awaitable
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, NoReturn, TypeVar, cast
@@ -13,7 +14,7 @@ from marimo._ast.cell import Cell
from marimo._runtime.context import ContextNotInitializedError, get_context
if TYPE_CHECKING:
- from collections.abc import Awaitable, Mapping
+ from collections.abc import Mapping
Fn = TypeVar("Fn", bound=Callable[..., Any])
@@ -167,7 +168,14 @@ def build_test_class(
def hook(var: str) -> Callable[..., Any] | type[MarimoTest]:
def _hook(*args: Any, **kwargs: Any) -> Any:
- _, defs = run() # type: ignore
+ res = run()
+ if isinstance(res, Awaitable):
+ import asyncio
+
+ loop = asyncio.new_event_loop()
+ _, defs = loop.run_until_complete(res)
+ else:
+ _, defs = res
return defs[var](*args, **kwargs)
test = tests.get(var, None)
diff --git a/tests/_ast/test_pytest.py b/tests/_ast/test_pytest.py
index 89d3c7a84b..5b11fb513a 100644
--- a/tests/_ast/test_pytest.py
+++ b/tests/_ast/test_pytest.py
@@ -1,14 +1,16 @@
from __future__ import annotations
-import pytest
-
import marimo
app = marimo.App()
+with app.setup:
+ import pytest
+
+
@app.cell
-async def _(pytest, x, y, Z):
+async def _(x, y, Z):
@pytest.mark.xfail(
reason=("To ensure this doesn't just eval."),
raises=AssertionError,
@@ -45,6 +47,7 @@ async def _(pytest, x, y, Z):
def test_static() -> None:
assert x + y == Z
+ @pytest.mark.asyncio
async def test_async_cell():
assert True
@@ -56,9 +59,7 @@ def imports():
# Suffixed with _fixture should have corresponding definitions in conftest.py
mo_fixture = None
- import pytest
-
- return mo, mo_fixture, pytest
+ return mo, mo_fixture
@app.cell
@@ -66,11 +67,18 @@ def test_cell_is_invoked():
assert True
@app.cell
async def test_async_cell():
assert True
+async def test_async_function():
+ assert True
+
+
@app.function
def test_top_level_function():
assert True
@@ -143,7 +151,7 @@ def test_cell_args_resolved_by_name(mo): # noqa: ARG001
@app.cell
-def test_cell_assert_rewritten(pytest):
+def test_cell_assert_rewritten():
a = 1
b = 2