From 50537b99cfcfe6adbd753881c930fb4b2fe08178 Mon Sep 17 00:00:00 2001 From: Ralf Juengling Date: Mon, 10 Aug 2026 14:02:23 -0700 Subject: [PATCH] test(cuda.core): add note about using multiple conftest modules --- cuda_core/tests/AGENTS.md | 27 +++++++++++++++++++++++++++ cuda_core/tests/graph/__init__.py | 0 2 files changed, 27 insertions(+) create mode 100644 cuda_core/tests/graph/__init__.py diff --git a/cuda_core/tests/AGENTS.md b/cuda_core/tests/AGENTS.md index 39472d745b5..7b74a487743 100644 --- a/cuda_core/tests/AGENTS.md +++ b/cuda_core/tests/AGENTS.md @@ -65,3 +65,30 @@ by `cuCtxSynchronize()` before popping the context. Tests should not rely on that as a substitute for cleaning up explicitly: prefer context managers for resources whose lifetime fits a single scope, and keep pool lifetimes inside the test that creates them. + +## A per-directory `conftest.py` needs an `__init__.py` +Under pytest's default `prepend` import mode, a test file's `sys.path` entry is +the first parent directory *without* an `__init__.py`; the module is then named +by its path relative to that directory. So for `tests/memory/test_x.py`: + +| | `sys.path` entry | module name | `import conftest` finds | +|---|---|---|---| +| no `__init__.py` | `tests/memory` | `test_x` | `tests/memory/conftest.py` | +| with `__init__.py` | `tests` | `memory.test_x` | `tests/conftest.py` | + +Adding the `__init__.py` moves the insertion point up one level and solves two problems at once: + +1. Many modules do `from conftest import `, meaning the root + `tests/conftest.py`. Keeping the subdirectory off `sys.path` is what makes + that unambiguous. Otherwise a new per-directory `conftest.py` silently + captures the name and the import fails with `ImportError: cannot import name + ...` — reported in the *other* modules in that directory, not in the file you + added. +2. Module names become hierarchical, so a basename reused under another + directory cannot collide. Flat names make `graph/test_memory.py` alongside + `tests/test_memory.py` an "import file mismatch" error. + +Split the two kinds of shared test code accordingly: fixtures and hooks go in +`conftest.py`, which pytest discovers and nothing imports by name; constants and +helper functions go in `tests/helpers/`, which tests import explicitly. Following +that split also keeps the name collision from mattering in the first place. diff --git a/cuda_core/tests/graph/__init__.py b/cuda_core/tests/graph/__init__.py new file mode 100644 index 00000000000..e69de29bb2d