Skip to content

Commit 49ab064

Browse files
authored
Merge pull request #1134 from neutrons/docs/trim-claude-md
Move the test-tier section of CLAUDE.md to tests/CLAUDE.md
2 parents 9fd090e + 9386ace commit 49ab064

3 files changed

Lines changed: 78 additions & 73 deletions

File tree

‎CLAUDE.md‎

Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,6 @@ Full examples (a function, a test, module and class docstrings): [docs/developer
122122

123123
Every test follows Arrange-Act-Assert, with `# Arrange`, `# Act` and `# Assert` comments.
124124

125-
Always cover:
126-
1. **Normal cases** — typical inputs.
127-
2. **Edge cases** — empty inputs, single items, max values.
128-
3. **Error cases** — invalid inputs, type errors.
129-
4. **Integration** — components working together.
130-
131125
Naming: `test_<function_name>_<scenario>_<expected_outcome>`, **kept under 60
132126
characters**. Omit any part the module or class name already carries — a test in
133127
`test_config.py` needs no `config_` prefix — and drop `_returns_*` tails when the
@@ -145,72 +139,10 @@ test_init_strain_without_filenames_raises
145139

146140
### Test tiers, markers, and locations
147141

148-
Tests are split into four tiers so the fast ones can be run without waiting on
149-
real data, a full ring calibration, or GUI pop-ups. **Put every new test in the tier
150-
matching what it actually touches, and apply the marker that tier requires.**
151-
152-
| Tier | Location | Marker | Run with |
153-
|---|---|---|---|
154-
| Unit | `tests/unit/<module-path>/`, mirroring the `pyrs/` package layout | *none* | `pixi run test-unit` |
155-
| Ring calibration | `tests/unit/<module-path>/`, beside the unit tests | `@pytest.mark.ring_calibration` | `pixi run test-ring-calibration` |
156-
| Integration | `tests/integration/` (flat) | `@pytest.mark.integration` | `pixi run test-integration` |
157-
| GUI | `tests/ui/` | `@pytest.mark.gui` **and** `@pytest.mark.integration` | `pixi run test-gui` |
158-
| By-hand scripts | `tests/scripts/` | *n/a — never collected* | run the file directly |
159-
160-
Marker definitions, registered in `pyproject.toml`:
161-
162-
- **`integration`** — exercises real file I/O (`tests/data`, the `/HFIR`
163-
archive) or a multi-component workflow.
164-
- **`ring_calibration`** — runs a full ring calibration of the synthetic run, or
165-
uses the shared one (`tests/util/ring_calibration_helpers.py`:
166-
`converged_synthetic_calibration`, `use_cached_calibrate_rings`). That
167-
calibration takes ~30 s and is cached per process, so the tier runs serially
168-
and computes it once; in the parallel unit tier every worker would compute it
169-
again (#1101).
170-
- **`gui`** — requires a display (xvfb or offscreen). Constructing or driving a
171-
Qt widget is the usual reason, but it is not the only one: taking the `qapp`
172-
fixture is enough on its own (it builds a real `QApplication`, which aborts
173-
with no display), as is importing any module that pulls in matplotlib's
174-
`QtAgg` backend at module scope. If the module cannot be *imported* headless,
175-
it is `gui` — `python -c 'import <module>'` with `DISPLAY` and
176-
`QT_QPA_PLATFORM` unset is the check.
177-
178-
Notes:
179-
180-
- Markers are enforced by `addopts = "--strict-markers"`: an unregistered marker
181-
is an error, not a silent no-op. Register new markers in `pyproject.toml`.
182-
- `test-gui` selects `-m gui`; `test-integration` selects
183-
`-m 'integration and not gui'`. GUI tests therefore carry **both** markers —
184-
`gui` alone would drop them from the integration tier.
185-
- A test is a *unit* test only if it needs no real file and no widget. If it
186-
loads a fixture file purely for convenience, prefer rewriting it against a
187-
synthetic fixture and keeping it in the unit tier.
188-
- Apply a whole-module marker with `pytestmark = pytest.mark.integration` rather
189-
than decorating every function.
190-
- `tests/util/` holds shared helper modules and fixtures, not tests of its own
191-
(beyond tests *for* those helpers); `tests/scripts/` is excluded from
192-
collection via `norecursedirs`.
193-
- `test-gui` sets `QT_QPA_PLATFORM=offscreen` itself, so no window ever appears.
194-
The full `pixi run test` (all tiers, serial, with coverage) does **not**. Export
195-
`QT_QPA_PLATFORM=offscreen` yourself before running it on a workstation with a
196-
real desktop session, or it will stall on the GUI tier until the timeout below
197-
fires — see [docs/ground_truths.md](docs/ground_truths.md).
198-
- **The unit and integration tiers run in parallel** (`pytest-xdist`, `-n auto
199-
--dist loadscope`: one worker per core, at most 8 locally; a module's top-level test functions, or a
200-
test class, all go to one worker, so a module-scoped fixture used by functions is
201-
built once). The GUI tier stays serial: its tests share one window per module.
202-
So a test must not depend on another test's order or write a fixed file name
203-
into the working folder; use `tmp_path`. To debug with `--pdb` or `-s`, run
204-
serially: `pixi run test-unit -n 0`. The ring-calibration tier is serial (see its marker). CI runs
205-
each tier as its own job (`test-ci-unit`, `test-ci-ring-calibration`, `test-ci-integration`,
206-
`test-ci-gui`, under `xvfb-run`, #1100, #1101).
207-
- **Every test has a 300-second timeout** (`timeout`/`timeout_method` in
208-
`pyproject.toml`, via `pytest-timeout`). A hang is a failure, not an infinite
209-
wait. `timeout_method = "thread"` is deliberate: a test blocked inside Qt's C++
210-
event loop never returns to the interpreter, so the default `signal` method
211-
cannot interrupt it — verified. The watchdog dumps every thread's stack, which
212-
names the blocking line. Override per-test with `@pytest.mark.timeout(N)` for a
213-
genuinely long-running case rather than raising the global limit.
142+
Put every new test in the tier matching what it touches (unit, ring calibration, integration, GUI)
143+
and apply that tier's marker. **Read [tests/CLAUDE.md](tests/CLAUDE.md) before adding a test**: it
144+
holds the tier table, marker rules, parallel-run rules and timeout policy, and loads when you work
145+
under `tests/`.
214146

215147
<!-- cherry-pick 30231c18: BEGIN added block "Pytest markers" -->
216148
**Pytest markers** — apply the correct marker(s) when a test is *written*,

‎docs/developer/source/code_style.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Tests
5959
-----
6060

6161
Every test follows Arrange-Act-Assert, with ``# Arrange``, ``# Act`` and ``# Assert`` comments. Test naming, the
62-
test tiers and their markers are in ``CLAUDE.md`` ("Testing Guidelines").
62+
test tiers and their markers are in ``CLAUDE.md`` ("Testing Guidelines") and ``tests/CLAUDE.md``.
6363

6464
.. code-block:: python
6565

‎tests/CLAUDE.md‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Test conventions
2+
3+
Loaded when working under `tests/`; the root [CLAUDE.md](../CLAUDE.md) keeps the test naming rule
4+
and the "Pytest markers" block.
5+
6+
## Test tiers, markers, and locations
7+
8+
Tests are split into four tiers so the fast ones can be run without waiting on
9+
real data, a full ring calibration, or GUI pop-ups. **Put every new test in the tier
10+
matching what it actually touches, and apply the marker that tier requires.**
11+
12+
| Tier | Location | Marker | Run with |
13+
|---|---|---|---|
14+
| Unit | `tests/unit/<module-path>/`, mirroring the `pyrs/` package layout | *none* | `pixi run test-unit` |
15+
| Ring calibration | `tests/unit/<module-path>/`, beside the unit tests | `@pytest.mark.ring_calibration` | `pixi run test-ring-calibration` |
16+
| Integration | `tests/integration/` (flat) | `@pytest.mark.integration` | `pixi run test-integration` |
17+
| GUI | `tests/ui/` | `@pytest.mark.gui` **and** `@pytest.mark.integration` | `pixi run test-gui` |
18+
| By-hand scripts | `tests/scripts/` | *n/a — never collected* | run the file directly |
19+
20+
Marker definitions, registered in `pyproject.toml`:
21+
22+
- **`integration`** — exercises real file I/O (`tests/data`, the `/HFIR`
23+
archive) or a multi-component workflow.
24+
- **`ring_calibration`** — runs a full ring calibration of the synthetic run, or
25+
uses the shared one (`tests/util/ring_calibration_helpers.py`:
26+
`converged_synthetic_calibration`, `use_cached_calibrate_rings`). That
27+
calibration takes ~30 s and is cached per process, so the tier runs serially
28+
and computes it once; in the parallel unit tier every worker would compute it
29+
again (#1101).
30+
- **`gui`** — requires a display (xvfb or offscreen). Constructing or driving a
31+
Qt widget is the usual reason, but it is not the only one: taking the `qapp`
32+
fixture is enough on its own (it builds a real `QApplication`, which aborts
33+
with no display), as is importing any module that pulls in matplotlib's
34+
`QtAgg` backend at module scope. If the module cannot be *imported* headless,
35+
it is `gui` — `python -c 'import <module>'` with `DISPLAY` and
36+
`QT_QPA_PLATFORM` unset is the check.
37+
38+
Notes:
39+
40+
- Markers are enforced by `addopts = "--strict-markers"`: an unregistered marker
41+
is an error, not a silent no-op. Register new markers in `pyproject.toml`.
42+
- `test-gui` selects `-m gui`; `test-integration` selects
43+
`-m 'integration and not gui'`. GUI tests therefore carry **both** markers —
44+
`gui` alone would drop them from the integration tier.
45+
- A test is a *unit* test only if it needs no real file and no widget. If it
46+
loads a fixture file purely for convenience, prefer rewriting it against a
47+
synthetic fixture and keeping it in the unit tier.
48+
- Apply a whole-module marker with `pytestmark = pytest.mark.integration` rather
49+
than decorating every function.
50+
- `tests/util/` holds shared helper modules and fixtures, not tests of its own
51+
(beyond tests *for* those helpers); `tests/scripts/` is excluded from
52+
collection via `norecursedirs`.
53+
- `test-gui` sets `QT_QPA_PLATFORM=offscreen` itself, so no window ever appears.
54+
The full `pixi run test` (all tiers, serial, with coverage) does **not**. Export
55+
`QT_QPA_PLATFORM=offscreen` yourself before running it on a workstation with a
56+
real desktop session, or it will stall on the GUI tier until the timeout below
57+
fires — see [docs/ground_truths.md](../docs/ground_truths.md).
58+
- **The unit and integration tiers run in parallel** (`pytest-xdist`, `-n auto
59+
--dist loadscope`: one worker per core, at most 8 locally; a module's top-level test functions, or a
60+
test class, all go to one worker, so a module-scoped fixture used by functions is
61+
built once). The GUI tier stays serial: its tests share one window per module.
62+
So a test must not depend on another test's order or write a fixed file name
63+
into the working folder; use `tmp_path`. To debug with `--pdb` or `-s`, run
64+
serially: `pixi run test-unit -n 0`. The ring-calibration tier is serial (see its marker). CI runs
65+
each tier as its own job (`test-ci-unit`, `test-ci-ring-calibration`, `test-ci-integration`,
66+
`test-ci-gui`, under `xvfb-run`, #1100, #1101).
67+
- **Every test has a 300-second timeout** (`timeout`/`timeout_method` in
68+
`pyproject.toml`, via `pytest-timeout`). A hang is a failure, not an infinite
69+
wait. `timeout_method = "thread"` is deliberate: a test blocked inside Qt's C++
70+
event loop never returns to the interpreter, so the default `signal` method
71+
cannot interrupt it — verified. The watchdog dumps every thread's stack, which
72+
names the blocking line. Override per-test with `@pytest.mark.timeout(N)` for a
73+
genuinely long-running case rather than raising the global limit.

0 commit comments

Comments
 (0)