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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
feat: pytest fixtures (#7654)
Dylan Madisettiad9e11c4f7ce
diff --git a/docs/guides/testing/pytest.md b/docs/guides/testing/pytest.mdindex c20e11248f..f5f0b6e5ca 100644--- a/docs/guides/testing/pytest.md+++ b/docs/guides/testing/pytest.md@@ -4,10 +4,10 @@By default, marimo discovers and executes tests inside your notebook.When the optional `pytest` dependency is present, marimo runs `pytest` on cells that-consist exclusively of test code - i.e. functions whose names start with `test_` or-classes whose names start with `Test`. If a cell mixes in anything else (helper-functions, constants, variables, imports, etc.), that cell is skipped by the test-runner (we recommend you move helpers to another cell).+consist exclusively of test code - i.e. functions whose names start with `test_`,+classes whose names start with `Test`, or functions decorated with `@pytest.fixture`.+If a cell mixes in anything else (helper functions, constants, variables, imports, etc.),+that cell is skipped by the test runner (we recommend you move helpers to another cell).For example,@@ -155,3 +155,86 @@ test_notebook.py:17: AssertionErrorFAILED test_notebook.py::test_fails - AssertionError: This test fails========================= 1 failed, 3 passed in 0.82s ==========================```++## Using Pytest Fixtures+++marimo supports pytest fixtures, with one limitation: fixtures defined in one cell cannot be used in another cell, unless the fixtures were defined in the setup cell.+For this reason, we recommend defining (or importing) fixtures in your notebook's setup cell, or defining fixtures in a pytest `conftest.py` file.++### Examples++**Fixtures defined in the setup cell**:++```python+# test_notebook.py+import marimo+app = marimo.App()++with app.setup:+ from fixtures import db_connection, sample_data++def _(sample_data):+ def test_data_loaded(sample_data):+ assert len(sample_data) > 0+```++**Fixtures in the same cell as tests**:++```python+def _():+ import pytest+ return pytest+++def _(pytest):+ @pytest.fixture+ def temp_file():+ import tempfile+ with tempfile.NamedTemporaryFile() as f:+ yield f++ def test_writes_to_file(temp_file):+ temp_file.write(b"hello")+ temp_file.seek(0)+ assert temp_file.read() == b"hello"+```++**Class fixtures**:++```python+def _():+ import pytest+ return pytest+++def _(pytest):+ class TestDatabase:+ @pytest.fixture(scope="class")+ def connection(self):+ return create_connection()++ def test_query(self, connection):+ result = connection.query("SELECT 1")+ assert result == 1+```++**`conftest.py` fixtures** work as expected - pytest discovers them automatically.++!!! info "Fixture Limitations"++ Fixtures defined in one cell **cannot** be used by tests in a different cell.+ This is because pytest collects tests **statically** by parsing the notebook+ file without executing it. During collection, pytest can see module-level+ fixtures (from `conftest.py` or imported modules) and fixtures defined in the+ same scope as the test, but it cannot see fixtures defined in other cells.++ **Why?** Running the entire notebook just for fixture discovery would be+ expensive, and static analysis cannot determine which fixtures will be+ available after cell execution since cell order is determined at runtime by+ marimo's dependency graph.diff --git a/marimo/_ast/compiler.py b/marimo/_ast/compiler.pyindex c33450239b..0325b7ea79 100644--- a/marimo/_ast/compiler.py+++ b/marimo/_ast/compiler.py@@ -24,6 +24,7 @@ from marimo._ast.cell import (SourcePosition,)from marimo._ast.names import SETUP_CELL_NAME, TOPLEVEL_CELL_PREFIX+from marimo._ast.pytest import has_fixture_decoratorfrom marimo._ast.transformers import ContainedExtractWithBlockfrom marimo._ast.variables import is_localfrom marimo._ast.visitor import ImportData, Name, ScopedVisitor@@ -101,7 +102,7 @@ def ends_with_semicolon(code: str) -> bool:def contains_only_tests(tree: ast.Module) -> bool:- """Returns True if the module contains only test functions."""+ """Returns True if the module contains only test functions or fixtures."""scope = tree.bodyfor node in scope:if isinstance(node, ast.Return):@@ -110,7 +111,9 @@ def contains_only_tests(tree: ast.Module) -> bool:node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):return False- if not node.name.lower().startswith("test"):+ if not node.name.lower().startswith(+ "test"+ ) and not has_fixture_decorator(node):return Falsereturn bool(scope)diff --git a/marimo/_ast/pytest.py b/marimo/_ast/pytest.pyindex e58e7e3f4d..0ef733bea6 100644--- a/marimo/_ast/pytest.py+++ b/marimo/_ast/pytest.py@@ -54,14 +54,17 @@ def build_stub_fn(allowed = [arg for arg in args.keys()]name = func_body.name- # Typing checks for mypy+ # Typing checks for mypy - template structure is knownfn = copy.deepcopy(PYTEST_BASE)body = fn.body[0]- assert isinstance(body, ast.FunctionDef)+ if not isinstance(body, ast.FunctionDef):+ raise ValueError("Invalid pytest scaffold template structure")returned = body.body[-1]- assert isinstance(returned, ast.Return)+ if not isinstance(returned, ast.Return):+ raise ValueError("Invalid pytest scaffold template structure")call = returned.value- assert isinstance(call, ast.Call)+ if not isinstance(call, ast.Call):+ raise ValueError("Invalid pytest scaffold template structure")# Using _pytest_scaffold as a template, the resultant function will look# like:@@ -95,15 +98,21 @@ def build_stub_fn(local = {"stub": basis, "Any": Any}eval(compile(fn, file, "exec"), local)- response = cast(Callable[..., Any], local[name])- assert callable(response)+ response = local[name]+ if not callable(response):+ raise ValueError(+ f"Expected callable for '{name}', got {type(response)}"+ )return responsedef wrap_fn_for_pytest(func: Fn, cell: Cell) -> Callable[..., Any]:func_ast = ast_parse(inspect.getsource(func))func_body = func_ast.body[0]- assert isinstance(func_body, (ast.FunctionDef, ast.AsyncFunctionDef))+ if not isinstance(func_body, (ast.FunctionDef, ast.AsyncFunctionDef)):+ raise ValueError(+ f"Expected function definition, got {type(func_body).__name__}"+ )args = {arg.arg: arg for arg in func_body.args.args}fixtures = [arg for arg in args.keys() if arg.endswith("_fixture")]@@ -117,6 +126,152 @@ def wrap_fn_for_pytest(func: Fn, cell: Cell) -> Callable[..., Any]:)+def is_pytest_decorator(decorator: ast.AST) -> tuple[bool, str | None]:+ """Check if decorator is a pytest.* call and return attr name."""+ # @pytest.fixture() or @pytest.mark.parametrize()+ if isinstance(decorator, ast.Call) and isinstance(+ decorator.func, ast.Attribute+ ):+ if (+ isinstance(decorator.func.value, ast.Name)+ and decorator.func.value.id == "pytest"+ ):+ return True, decorator.func.attr+ # @pytest.mark.parametrize() etc+ if (+ isinstance(decorator.func.value, ast.Attribute)+ and isinstance(decorator.func.value.value, ast.Name)+ and decorator.func.value.value.id == "pytest"+ ):+ return True, None # Nested attr, use eval+ # @pytest.fixture (no call)+ if isinstance(decorator, ast.Attribute):+ if (+ isinstance(decorator.value, ast.Name)+ and decorator.value.id == "pytest"+ ):+ return True, decorator.attr+ return False, None+++def has_fixture_decorator(node: ast.AST) -> bool:+ """Check if function has @pytest.fixture decorator."""+ if not hasattr(node, "decorator_list"):+ return False+ if len(node.decorator_list) != 1:+ return False+ decorator = node.decorator_list[0]+ # @fixture or @fixture()+ if isinstance(decorator, ast.Name) and decorator.id == "fixture":+ return True+ if isinstance(decorator, ast.Call):+ func = decorator.func+ if isinstance(func, ast.Name) and func.id == "fixture":+ return True+ # @pytest.fixture or @pytest.fixture()+ is_pytest, attr = is_pytest_decorator(decorator)+ return is_pytest and attr == "fixture"+++def _eval_fixture_decorator(+ decorator: ast.AST,+ local: dict[str, Any],+ file: str,+) -> Callable[..., Any]:+ """Evaluate @pytest.fixture decorator, return decorator fn."""+ import pytest # type: ignore++ if isinstance(decorator, ast.Call):+ fixture_args = [+ eval(compile(ast.Expression(arg), file, "eval"), local)+ for arg in decorator.args+ ]+ fixture_kwargs = {+ kw.arg: eval(+ compile(ast.Expression(kw.value), file, "eval"), local+ )+ for kw in decorator.keywords+ if kw.arg is not None+ }+ return cast(+ Callable[..., Any], pytest.fixture(*fixture_args, **fixture_kwargs)+ )+ else:+ return cast(Callable[..., Any], pytest.fixture)+++def _make_fails(var: str, e: Exception) -> Callable[..., NoReturn]:+ """Create a hook that raises an error explaining the failure."""+ err = copy.copy(e)++ def fails(*args: Any, **kwargs: Any) -> NoReturn:+ del args, kwargs+ raise ValueError(+ f"Failed to evaluate decorator for '{var}'. "+ "Consider exposing relevant variables in app.setup."+ ) from err++ return fails+++def _make_hook(+ var: str,+ run: Callable[+ [],+ tuple[Any, Mapping[str, Any]]+ | Awaitable[tuple[Any, Mapping[str, Any]]],+ ],+ use_wrapped: bool = False,+) -> Callable[..., Any]:+ """Single hook factory - handles both fixtures and tests."""++ def _hook(*args: Any, **kwargs: Any) -> Any:+ res = run()+ if isinstance(res, Awaitable):+ import asyncio++ loop = asyncio.new_event_loop()+ _, cell_defs = loop.run_until_complete(res)+ else:+ _, cell_defs = res++ target = cell_defs[var].__wrapped__ if use_wrapped else cell_defs[var]+ return target(*args, **kwargs)++ return _hook+++def _build_hook(+ test: ast.FunctionDef | ast.AsyncFunctionDef,+ var: str,+ run: Callable[..., Any],+ file: str,+ local: dict[str, Any],+ is_fixture: bool = False,+) -> Callable[..., Any]:+ """Build hook for test or fixture function."""+ hook = _make_hook(var, run, use_wrapped=is_fixture)++ stub_fn = build_stub_fn(test, file)+ functools.wraps(stub_fn)(hook)++ try:+ if is_fixture:+ # Only apply the outer fixture decorator+ decorator = test.decorator_list[0]+ fixture_decorator = _eval_fixture_decorator(decorator, local, file)+ return cast(Callable[..., Any], fixture_decorator(hook))+ else:+ # Apply all decorators in reverse order+ for decorator in test.decorator_list[::-1]:+ expr = ast.Expression(decorator)+ hook = eval(compile(expr, file, "eval"), local)(hook)+ except Exception as e:+ return _make_fails(var, e)++ return hook++def build_test_class(body: list[ast.stmt],run: Callable[@@ -157,28 +312,44 @@ def build_test_class("""tests = {}for node in body:- if isinstance(node, ast.Return):+ if isinstance(node, (ast.Return, ast.Expr)):continue- assert isinstance(+ if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)- ), (- "Invalid test compilation. "- " Please report to marimo-team/marimo/issues."- )+ ):+ if inner:+ continue+ # Unexpected, should be guarded to this point.+ raise RuntimeError(+ "Invalid test compilation. "+ " Please report to marimo-team/marimo/issues."+ )tests[node.name] = node- def hook(var: str) -> Callable[..., Any] | type[MarimoTest]:- def _hook(*args: Any, **kwargs: Any) -> Any:- 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)+ # Pre-compute base locals once to avoid repeated inspect.stack() calls+ # which are expensive on Windows.+ base_local: dict[str, Any] = {}+ try:+ import pytest # type: ignore++ base_local["pytest"] = pytest+ except ImportError:+ pass++ # Try to get context globals, or fall back to frame locals+ try:+ base_local.update(get_context().globals)+ except ContextNotInitializedError:+ # If not in runtime, we are running directly as a script. As+ # such, we need the values from the module frame.+ # Traverse frame upwards until we match the file.+ frames = inspect.stack(context=0)+ for frame in frames:+ if Path(frame.filename).resolve() == Path(file).resolve():+ base_local.update(frame.frame.f_locals)+ break+ def hook(var: str) -> Callable[..., Any] | type[MarimoTest]:test = tests.get(var, None)if test is None:@@ -192,78 +363,39 @@ def build_test_class(return failsif isinstance(test, (ast.FunctionDef, ast.AsyncFunctionDef)):- stub_fn = build_stub_fn(test, file)- functools.wraps(stub_fn)(_hook)- # Evaluate and run decorators.- local = {}- # Always expose pytest. Low hanging fruit, that should mitigate some- # user issues.- try:- import pytest # type: ignore-- local["pytest"] = pytest- except ImportError:- pass-- # If called from the runtime, we can populate the locals- try:- local.update(get_context().globals)- except ContextNotInitializedError:- # If not in runtime, we are running directly as a script. As- # such, we need the values from the module frame.- # Traverse frame upwards until we match the file.- frames = inspect.stack()- for frame in frames:- if Path(frame.filename).resolve() == Path(file).resolve():- local.update(frame.frame.f_locals)- break-- try:- for decorator in test.decorator_list[::-1]:- expr = ast.Expression(decorator)- _hook = eval(compile(expr, file, "eval"), local)(_hook)- if not inner:- _hook = staticmethod(_hook)- except Exception as _e:- # Python restricts scope of exceptions artificially.- e = copy.copy(_e)-- def fails(*args: Any, **kwargs: Any) -> NoReturn:- del args, kwargs- raise ValueError(- f"Failed to evaluate signature/decorator for {var} "- "during test collection. Consider exposing relevant "- "variables in the setup block, or rewriting your "- "tests to avoid runtime dependencies."- ) from e-- return fails+ # Use pre-computed locals+ local = base_local.copy()++ is_fixture = has_fixture_decorator(test)+ _hook = _build_hook(test, var, run, file, local, is_fixture)++ if not inner:+ _hook = staticmethod(_hook)return _hook+elif isinstance(test, ast.ClassDef):- defs = {- node.name- for node in test.body- if isinstance(- node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)- )- and node.name.lower().startswith("test")+ class_defs = {+ node.name for node in test.body if hasattr(node, "name")}def _run() -> tuple[Any, Any]:old_output, old_defs = run() # type: ignoreold_defs = dict(old_defs)- for d in defs:+ for d in class_defs:old_defs[d] = getattr(old_defs[var], d)return old_output, old_defs- return build_test_class(test.body, _run, file, var, defs, True)+ return build_test_class(+ test.body, _run, file, var, class_defs, True+ )raise ValueError("Improperly compiled as a test. Please report to""marimo-team/marimo/issues.")- return type(name, (MarimoTest,), {var: hook(var) for var in defs})+ attrs = {var: h for var in defs if (h := hook(var)) is not None}+ return type(name, (MarimoTest,), attrs)def process_for_pytest(func: Fn, cell: Cell) -> None:@@ -287,15 +419,19 @@ def process_for_pytest(func: Fn, cell: Cell) -> None:name = f"{MARIMO_TEST_STUB_NAME}_{next(block_incrementer)}"scope = tree.body[0]- assert isinstance(+ if not isinstance(scope, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)- )+ ):+ raise ValueError(+ f"Expected function or class definition, got {type(scope).__name__}"+ )cls = build_test_class(scope.body, run, inspect.getfile(func), name, cell.defs)+# Get first frame not in library to insert the class.# May be multiple levels if called from pytest or something.- frames = inspect.stack()+ frames = inspect.stack(context=0)# ensure marimo/_ not in frame path, using this file as a reference.library = Path(__file__).parent.parentdiff --git a/marimo/_runtime/pytest.py b/marimo/_runtime/pytest.pyindex 4536fb111e..fc402cfb79 100644--- a/marimo/_runtime/pytest.py+++ b/marimo/_runtime/pytest.py@@ -74,9 +74,14 @@ def _sub_function(return old_itemimport pytest # type: ignore+ from _pytest.fixtures import FuncFixtureInfo # type: ignore++ fixtureinfo = old_item._fixtureinfo+ param_names: set[str] = set()if hasattr(old_item, "callspec") and old_item.callspec:params: dict[str, Any] = old_item.callspec.params+ param_names = set(params.keys())def make_test_func(func_JYWB: Callable[..., Any], param_dict: dict[str, Any]@@ -85,8 +90,8 @@ def _sub_function(# removal.# Also no functools.wraps(func) because we need the empty# call signature- def test_wrapper() -> Any:- return func_JYWB(**param_dict)+ def test_wrapper(*args: Any, **kwargs: Any) -> Any:+ return func_JYWB(*args, **kwargs, **param_dict)# but copy attributes from the original functiontest_wrapper.__name__ = func_JYWB.__name__@@ -94,7 +99,31 @@ def _sub_function(return test_wrapperfn = make_test_func(fn, params)- pyfn = pytest.Function.from_parent(parent, name=old_item.name, callobj=fn)++ # Filter out parametrized args from fixtureinfo since they're baked in+ fixtureinfo = FuncFixtureInfo(+ argnames=tuple(+ a for a in fixtureinfo.argnames if a not in param_names+ ),+ initialnames=tuple(+ a for a in fixtureinfo.initialnames if a not in param_names+ ),+ names_closure=[+ a for a in fixtureinfo.names_closure if a not in param_names+ ],+ name2fixturedefs={+ k: v+ for k, v in fixtureinfo.name2fixturedefs.items()+ if k not in param_names+ },+ )++ pyfn = pytest.Function.from_parent(+ parent,+ name=old_item.name,+ callobj=fn,+ fixtureinfo=fixtureinfo, # Preserve fixture metadata (minus params)+ )# Attributes that need to be carried over.for attr in ["keywords", "own_markers"]:if hasattr(old_item, attr):@@ -120,7 +149,9 @@ class ReplaceStubPlugin:self.defs = defsself._result = MarimoPytestResult()- def pytest_collection_modifyitems(self, items: list[Any]) -> None:+ def pytest_collection_modifyitems(+ self, items: list[Any], session: Any+ ) -> None:"""Provided pytest has statically collected all the relevant tests:- Filter based on the expected defs of the cell context.- Sub in the function references in scope opposed to the pytest@@ -130,10 +161,40 @@ class ReplaceStubPlugin:# So don't import at the top level.import _pytest # type: ignore+ def is_fixture(obj: Any) -> bool:+ """Check if object is a pytest fixture."""+ return callable(obj) and hasattr(obj, "_pytestfixturefunction")++ # Register cell-scoped fixtures before processing items+ # Use names_closure (transitive deps) instead of just argnames+ # to handle fixture dependency chains+ fm = session._fixturemanager+ registered_fixtures: set[str] = set()+ for item in items:+ if hasattr(item, "_fixtureinfo"):+ for argname in item._fixtureinfo.names_closure:+ if (+ argname not in registered_fixtures+ and argname in self.lcls+ and is_fixture(self.lcls[argname])+ ):+ obj = self.lcls[argname]+ marker = obj._pytestfixturefunction+ fm._register_fixture(+ name=argname,+ func=obj,+ nodeid="", # Global visibility+ scope=marker.scope,+ params=marker.params,+ ids=marker.ids,+ autouse=marker.autouse,+ )+ registered_fixtures.add(argname)+to_collect = []# Filter tests, and create new "Functions" with the relevant references# where needed.- for i, item in enumerate(items):+ for item in items:head: Any = itempath: list[str] = []@@ -141,6 +202,18 @@ class ReplaceStubPlugin:path.append(head.name)head = head.parent+ # Handle @app.class_definition classes (not wrapped in stub class)+ # Check if head is a Function directly inside a Class whose name is in defs+ if isinstance(head, _pytest.python.Function) and isinstance(+ head.parent, _pytest.python.Class+ ):+ parent_name = getattr(+ head.parent, "originalname", head.parent.name+ )+ if parent_name in self.defs:+ path.append(head.name)+ head = head.parent+# For test name, helps keep names relative to the root.parent: Any = item.parentif not path:@@ -148,12 +221,12 @@ class ReplaceStubPlugin:name: str = getattr(head, "originalname", head.name)if name in self.defs:- obj: Any = self.lcls[name]+ value: Any = self.lcls[name]for attr in reversed(path):- if isinstance(obj, type):- obj = obj()- obj = getattr(obj, attr)- to_collect.append(_sub_function(items[i], parent, obj))+ if isinstance(value, type):+ value = value()+ value = getattr(value, attr)+ to_collect.append(_sub_function(item, parent, value))items[:] = to_collectdef pytest_terminal_summary(self, terminalreporter: Any) -> None:@@ -225,7 +298,10 @@ class ReplaceStubPlugin:report.longrepr.reprtraceback.reprentries[1:])for entry in report.longrepr.reprtraceback.reprentries:- entry.reprfileloc.path = _to_marimo_uri(entry.reprfileloc.path)+ if entry.reprfileloc is not None:+ entry.reprfileloc.path = _to_marimo_uri(+ entry.reprfileloc.path+ )def run_pytest(diff --git a/tests/_ast/test_pytest.py b/tests/_ast/test_pytest.pyindex 5b11fb513a..f67c3d668a 100644--- a/tests/_ast/test_pytest.py+++ b/tests/_ast/test_pytest.py@@ -23,6 +23,8 @@ async def _(x, y, Z):assert x + y == Zclass TestClass:+ """Has doc string"""+@pytest.mark.xfail(reason=("To ensure this doesn't just eval."),raises=AssertionError,diff --git a/tests/_ast/test_pytest_scoped.py b/tests/_ast/test_pytest_scoped.pyindex efd3c358b0..929e2f321c 100644--- a/tests/_ast/test_pytest_scoped.py+++ b/tests/_ast/test_pytest_scoped.py@@ -1,37 +1,84 @@import marimo-__generated_with = "0.11.25"+__generated_with = "0.18.4"app = marimo.App(width="medium")-with app.setup:import pytesttest_cases = [(1, 2), (1, 3), (1, 5)]+ @pytest.fixture+ def top_level_fixture():+ return 0++[email protected](("a", "b"), test_cases)+def test_function(a, b):+ assert a < b+++def inc(x):+ return x + 1+@app.cell-def _(test_cases):- @pytest.mark.parametrize(("a", "b"), test_cases)- def test_function(a, b):- assert a < b+def collection_of_tests():+ @pytest.fixture+ def scoped_fixture():+ return 0- return (test_function,)+ @pytest.mark.parametrize(("x", "y"), [(3, 4), (4, 5)])+ def test_answer_base(x, y):+ assert inc(x) == y, "These tests should pass."++ @pytest.mark.parametrize(("x", "y"), test_cases)+ def test_answer_toplevel(x, y, top_level_fixture):+ assert x < y + top_level_fixture, "These tests should pass."++ @pytest.mark.parametrize(("x", "y"), [(3, 4), (4, 5)])+ def test_answer_scoped(x, y, scoped_fixture):+ assert inc(x) == y + scoped_fixture, "These tests should pass."++ class TestScopedFixture:+ @pytest.fixture(scope="class")+ def example(self):+ return "value"++ def test_uses_fixture(self, example):+ assert example == "value"++ return@app.celldef _():- def inc(x):- return x + 1+ return- return (inc,)[email protected]_definition+class TestClassDefinitionWithFixtures:+ """Test class defined via @app.class_definition with fixtures."""-def collection_of_tests(inc, pytest):- @pytest.mark.parametrize(("x", "y"), [(3, 4), (4, 5)])- def test_answer(x, y):- assert inc(x) == y, "These tests should pass."+ @pytest.fixture(scope="class")+ def class_scoped_fixture(self):+ return "class_scoped"++ @pytest.fixture+ def instance_fixture(self):+ return "instance"++ def test_uses_class_fixture(self, class_scoped_fixture):+ assert class_scoped_fixture == "class_scoped"++ def test_uses_instance_fixture(self, instance_fixture):+ assert instance_fixture == "instance"++ @staticmethod+ def test_static_uses_class_fixture(class_scoped_fixture) -> None:+ assert class_scoped_fixture == "class_scoped"if __name__ == "__main__":diff --git a/tests/_ast/test_pytest_toplevel.py b/tests/_ast/test_pytest_toplevel.pyindex 1ab8010fff..ed84671919 100644--- a/tests/_ast/test_pytest_toplevel.py+++ b/tests/_ast/test_pytest_toplevel.py@@ -10,6 +10,12 @@ with app.setup:import pytest+def example_fixture():+ return "example"++@app.functiondef add(a, b):return a + b@@ -32,8 +38,15 @@ def test_add_bad(a, b, c):assert add(a, b) == c+def test_example_fixture(example_fixture):+ assert example_fixture == "example"++@app.class_definitionclass TestClassWorks:+ """Has doc string"""+def test_sanity(self):assert True@@ -47,5 +60,32 @@ class TestClassWorks:assert add(a, b) == c[email protected]_definition+class TestClassWithFixtures:+ """Has doc string"""++ @pytest.fixture(scope="class")+ def yields_value(self):+ return "value"++ @pytest.fixture+ def return_value(self):+ return "value"++ def test_yield(self, yields_value) -> None:+ assert yields_value == "value"++ @staticmethod+ def test_yield_static(yields_value) -> None:+ assert yields_value == "value"++ def test_return(self, return_value) -> None:+ assert return_value == "value"++ @staticmethod+ def test_return_static(return_value) -> None:+ assert return_value == "value"++if __name__ == "__main__":app.run()diff --git a/tests/_runtime/script_data/contains_tests.py b/tests/_runtime/script_data/contains_tests.pyindex 7a4d382b7a..64e4ea4766 100644--- a/tests/_runtime/script_data/contains_tests.py+++ b/tests/_runtime/script_data/contains_tests.py@@ -18,6 +18,17 @@ with app.setup:test_cases = [(1, 2), (1, 3), (1, 5)]+ @pytest.fixture+ def top_level_fixture():+ return "top"+++# Fixture defined as @app.function+def function_fixture():+ return "function"+@app.functiondef inc(x):@@ -76,5 +87,117 @@ def test_using_var_in_toplevel(a, b):assert a < b+def test_uses_top_level_fixture(top_level_fixture):+ assert top_level_fixture == "top"+++# Test parametrize + top-level fixture combination[email protected](("a", "b"), [(1, 2), (2, 3)])+def test_parametrize_with_toplevel_fixture(a, b, top_level_fixture):+ assert a < b+ assert top_level_fixture == "top"+++# Test using @app.function fixture+def test_uses_function_fixture(function_fixture):+ assert function_fixture == "function"+++# Test class defined with @app.class_definition that has fixtures[email protected]_definition+class TestClassDefinitionWithFixtures:+ """Class with fixtures defined via @app.class_definition"""++ @pytest.fixture(scope="class")+ def class_scoped_fixture(self):+ return "class_scoped"++ @pytest.fixture+ def instance_fixture(self):+ return "instance"++ def test_uses_class_fixture(self, class_scoped_fixture):+ assert class_scoped_fixture == "class_scoped"++ def test_uses_instance_fixture(self, instance_fixture):+ assert instance_fixture == "instance"++ @staticmethod+ def test_static_uses_class_fixture(class_scoped_fixture) -> None:+ assert class_scoped_fixture == "class_scoped"+++def _():+ @pytest.fixture+ def scoped_fixture():+ return "scoped"++ def test_uses_scoped_fixture(scoped_fixture):+ assert scoped_fixture == "scoped"++ # Test parametrize + scoped fixture combination+ @pytest.mark.parametrize(("x", "y"), [(3, 4), (4, 5)])+ def test_parametrize_with_scoped_fixture(x, y, scoped_fixture):+ assert x < y+ assert scoped_fixture == "scoped"+++def _():+ class TestWithClassFixture:+ @pytest.fixture(scope="class")+ def class_fixture(self):+ return "class"++ def test_uses_class_fixture(self, class_fixture):+ assert class_fixture == "class"++ return (TestWithClassFixture,)+++# Test fixture dependency chains+def _():+ @pytest.fixture+ def base_fixture():+ return "base"++ @pytest.fixture+ def dependent_fixture(base_fixture):+ """Fixture that depends on another cell-scoped fixture."""+ return base_fixture + "_extended"++ def test_fixture_dependency_chain(dependent_fixture):+ """Test only requests dependent_fixture, but base_fixture must also work."""+ assert dependent_fixture == "base_extended"+++# Null case: fixture defined in one cell, used in another (should error)+def _():+ @pytest.fixture+ def isolated_fixture():+ return "isolated"+++def _():+ def test_cross_cell_fixture_fails(isolated_fixture):+ """Test uses fixture from different cell - should fail with fixture not found."""+ assert isolated_fixture == "isolated"+++# Null case: fixture doesn't exist (should error)+def _():+ def test_missing_fixture(this_fixture_does_not_exist):+ """Test uses non-existent fixture - should fail with fixture not found."""+ pass++if __name__ == "__main__":app.run()diff --git a/tests/_runtime/test_pytest_runtime.py b/tests/_runtime/test_pytest_runtime.pyindex eddeba2108..5353471a7b 100644--- a/tests/_runtime/test_pytest_runtime.py+++ b/tests/_runtime/test_pytest_runtime.py@@ -12,15 +12,36 @@ def test_smoke_test():_, lcls = app.run()lcls = dict(lcls)+ # Format: (passed, skipped, failed, errors)def_count = {- "TestParent": (2, 0, 0),- "test_failure": (0, 0, 1),- "test_parameterized": (3, 0, 0),- "test_parameterized_collected": (2, 0, 0),- "test_sanity": (1, 0, 0),- "test_skip": (0, 1, 0),- "test_using_var_in_scope": (3, 0, 0),- "test_using_var_in_toplevel": (3, 0, 0),+ # fixtures, not tests+ "function_fixture": (0, 0, 0, 0),+ "scoped_fixture": (0, 0, 0, 0),+ "isolated_fixture": (0, 0, 0, 0),+ # tests+ "TestParent": (2, 0, 0, 0),+ "test_failure": (0, 0, 1, 0),+ "test_parameterized": (3, 0, 0, 0),+ "test_parameterized_collected": (2, 0, 0, 0),+ "test_sanity": (1, 0, 0, 0),+ "test_skip": (0, 1, 0, 0),+ "test_using_var_in_scope": (3, 0, 0, 0),+ "test_using_var_in_toplevel": (3, 0, 0, 0),+ # Fixtures - these now work with fixture preservation+ "test_uses_scoped_fixture": (1, 0, 0, 0),+ "test_parametrize_with_scoped_fixture": (2, 0, 0, 0),+ "TestWithClassFixture": (1, 0, 0, 0),+ "TestClassDefinitionWithFixtures": (3, 0, 0, 0),+ "test_uses_top_level_fixture": (1, 0, 0, 0),+ "test_parametrize_with_toplevel_fixture": (2, 0, 0, 0),+ "test_uses_function_fixture": (1, 0, 0, 0),+ # Fixture dependency chain test+ "base_fixture": (0, 0, 0, 0), # fixture, not a test+ "dependent_fixture": (0, 0, 0, 0), # fixture, not a test+ "test_fixture_dependency_chain": (1, 0, 0, 0),+ # Null cases - fixture not in scope / doesn't exist (errors)+ "test_cross_cell_fixture_fails": (0, 0, 0, 1),+ "test_missing_fixture": (0, 0, 0, 1),}path = Path(__file__).parent / "script_data/contains_tests.py"@@ -41,6 +62,7 @@ def test_smoke_test():response.passed,response.skipped,response.failed,+ response.errors,) == tuple(map(sum, zip(*[def_count[d] for d in cell.defs]))), (response.output)@@ -50,4 +72,5 @@ def test_smoke_test():# put it back on# Assert all cases captured, and nothing missed.- assert total == sum(map(sum, def_count.values())) == 16+ # Total: 0+0+0+2+1+3+2+1+1+3+3+1+2+1+3+1+2+1+0+0+1+1+1 = 30+ assert total == sum(map(sum, def_count.values())) == 30diff --git a/tests/conftest.py b/tests/conftest.pyindex 0a67a20ad8..7a4e94df79 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -697,7 +697,13 @@ def pytest_make_collect_report(collector):return reportfor cls in classes:if not (- cls.startswith("MarimoTestBlock") or cls == "TestClassWorks"+ cls.startswith("MarimoTestBlock")+ or cls+ in (+ "TestClassWorks",+ "TestClassWithFixtures",+ "TestClassDefinitionWithFixtures",+ )):report.outcome = "failed"report.longrepr = (