|
| 1 | +# LLM Test Writing Notes |
| 2 | + |
| 3 | +When generating/editing tests in this directory: |
| 4 | + |
| 5 | +## File Structure |
| 6 | + |
| 7 | +- Start every `.py` file with the full Apache 2.0 license header, then a one-line |
| 8 | + module docstring. |
| 9 | +- Use `from __future__ import annotations` as the first application-level import. |
| 10 | +- Keep module names as `test_<domain>.py`; avoid mixed catch-all files. |
| 11 | +- Place behavior in the closest module-specific file. |
| 12 | + |
| 13 | +## Docstrings and Types |
| 14 | + |
| 15 | +- Use reST docstrings and explicit type annotations in tests/helpers. |
| 16 | + Omit `:returns: None` when the return type is `-> None`. |
| 17 | + Omit `:raises AssertionError:` on test functions (every test raises on failure). |
| 18 | +- **Every LLM-generated or LLM-modified test must include** the following note in |
| 19 | + its docstring, on its own line after the summary: |
| 20 | + `NOTE: LLM-generated test -- verify for correctness.` |
| 21 | + Remove the note only after a human has verified the test logic. |
| 22 | + |
| 23 | +## Test Design |
| 24 | + |
| 25 | +- Write unit tests only; no external services or mutable environment assumptions. |
| 26 | +- Keep test names behavior-focused; prefer short names (`<30`, hard cap `<50`). |
| 27 | +- Each test should exercise one logical behavior; split tests with more than ~3 |
| 28 | + arrange/act/assert cycles. |
| 29 | +- Prefer parametrization for repeated patterns; use `pytest.param(..., id="...")` |
| 30 | + for readable test IDs. Avoid copy-paste duplication. |
| 31 | +- Do not duplicate tests that already exist in a domain-specific module; search for |
| 32 | + existing coverage before adding a new test. |
| 33 | + |
| 34 | +## Shared Infrastructure |
| 35 | + |
| 36 | +- Use `make_lab()` / `make_lab_with_topology()` from `helpers.py` for lab setup. |
| 37 | +- Use `lab._create_node_local()`, `_create_interface_local()`, `_create_link_local()`, |
| 38 | + `_create_annotation_local()`, `_create_smart_annotation_local()` for custom |
| 39 | + topologies. Prefer `make_lab_with_topology()` when the standard shape suffices. |
| 40 | +- Use conftest fixtures: `FAKE_HOST`, `FAKE_HOST_API`, `CURRENT_VERSION`, |
| 41 | + `reset_env`, `client_library_server_*`, `mocked_session`, `test_data_dir`, |
| 42 | + `respx_mock_with_labs`, `client_library`. |
| 43 | +- Place JSON fixtures in `test_data/`; access via the `test_data_dir` fixture. |
| 44 | +- Assign side-effect-only fixtures to `_`: `_ = client_library_server_current`. |
| 45 | + |
| 46 | +## Assertions and Exceptions |
| 47 | + |
| 48 | +- Assert concrete behavior (exact calls, exact exception types/messages). |
| 49 | +- Use `pytest.raises(ExcType, match="regex")` for exception assertions. |
| 50 | +- Capture expected warnings with `pytest.warns` / `pytest.deprecated_call`. |
| 51 | +- Capture log output with `caplog.at_level(...)` and assert on `caplog.text`. |
| 52 | + |
| 53 | +## Mocking |
| 54 | + |
| 55 | +- Do not use `@respx.mock` unless the test body configures at least one route. |
| 56 | +- Three valid `respx` patterns: fixture (`respx_mock: MockRouter`), decorator |
| 57 | + (`@respx.mock`), or context manager (`with respx.mock(...)`). |
| 58 | +- Patch at the import site, not the definition site |
| 59 | + (e.g. `patch("virl2_client.models.node.time.sleep")`, not `patch("time.sleep")`). |
| 60 | +- Use `patch.object(Cls, "attr")` as a context manager; never assign directly |
| 61 | + (e.g. `Lab.sync = Mock()`) without cleanup. |
| 62 | +- `assert_called_once_with(...)` already verifies call count — do not follow it with a |
| 63 | + redundant `assert_called_once()`. |
| 64 | +- Extract module-local helpers (`_make_node()`, `_new_event()`) for repeated setup; |
| 65 | + promote to `helpers.py`/`conftest.py` only when shared across modules. |
| 66 | + |
| 67 | +## Environment and Imports |
| 68 | + |
| 69 | +- Never place `import` statements inside test function or fixture bodies; put them at |
| 70 | + module level. |
| 71 | +- Fixtures must not call `os.chdir` or directly mutate `os.environ`; use |
| 72 | + `monkeypatch.chdir` / `monkeypatch.setenv` instead so teardown is guaranteed. |
| 73 | +- For flaky paths (time/async/threading), patch clocks/sleeps and use controlled mocks. |
| 74 | + |
| 75 | +## Optional Dependencies |
| 76 | + |
| 77 | +- Gate modules requiring optional packages with `pytest.importorskip("pkg")` at |
| 78 | + module level. |
| 79 | + |
| 80 | +## Coverage |
| 81 | + |
| 82 | +- Keep coverage complete for touched branches and verify with: |
| 83 | + - `pytest -n auto --cov=virl2_client --cov-report=term-missing` |
| 84 | + |
| 85 | +## File Placement |
| 86 | + |
| 87 | +Place new tests in the closest domain-specific `test_<domain>.py` file. |
| 88 | +The `_runtime` suffix marks runtime/integration-level tests for the same domain. |
| 89 | +See [Test Module Reference](README.md#test-module-reference) for the full |
| 90 | +module-to-scope mapping. |
| 91 | + |
| 92 | +> For full explanations and examples, see [README.md](README.md). |
0 commit comments