Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/dev/rust-conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,5 @@ Unit tests live in `#[cfg(test)] mod tests;` with a sibling `tests.rs`, so a
module with tests is `foo/mod.rs` and `foo/tests.rs`. A test block of 30 lines or
fewer may stay inline. A test file that outgrows one read becomes a `tests/`
directory: `tests/mod.rs` holds the shared fixtures, and each flow gets a file.
[Testing](testing.md#write-a-focused-test) says which tests to write, and that
they wait for events, not time.
21 changes: 21 additions & 0 deletions docs/dev/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,29 @@ that regenerates it.
- **Test one behaviour, and name the test for what it proves**, such as
`google_key_fetch_is_a_client_span_of_its_request`. Its doc comment says why
the behaviour matters.
- **Test it once, at the lowest layer that fails without the change**: not
again in a wrapper, at each call site or on each surface.
- **Extend before you add.** When an existing test or table covers the
behaviour, add a row or an assertion to it; write a new test only when no case
fits.
- **A fix comes with a test that fails without it, written first**, when it
fixes logic, data, state or a protocol. A purely visual fix, such as spacing,
colour or alignment, is shown with before and after screenshots in the pull
request instead, and gets no test.
- **Assert the outcome**, not that something ran. A test that only checks a page
loads or a command exits 0 proves little and still has to be maintained.
- **Wait for events, not time.** No `thread::sleep` or `tokio::time::sleep` to
let something happen, no assertions on how much real time passed, and no
retries: await the channel, notification or state change that marks the
milestone, such as a `watch` the test's fake sends on, or take a lock with
`Database::exclusive` to wait for its holder. Move tokio's clock with
`#[tokio::test(start_paused = true)]`, or with `pause`, `advance` and `resume`
between database calls, so the clock is never paused across database I/O,
and end an expiry PostgreSQL judges at once, as `SessionRepository::expire`
and `RecoveryRepository::expire` do. When nothing signals the milestone, as for a
real subprocess or network peer, a lock wait PostgreSQL shows only in its
catalog views (`until_blocked`), or production code that reads the system
clock, poll or sleep and say why on the line above: `// wall-clock: <why>`.
- **Don't test that a removed feature stays removed.** Once the code is gone,
there is nothing left to break.
- **Keep browser tests few.** Each spec says at its top what it covers. Auth's
Expand Down
Loading