writing-rust-tests
Skill127 starsChanged 11 days agoMITView on GitHub
Use when writing, modifying, or reviewing Rust tests in crates/, apps/backend, or bindings/ - covers the test macro and its options, where tests live, tester! fixtures, the ephemeral backend, assertions and wait helpers, WASM compatibility, and how to run tests
In this repository21 other files it gives its agents.
- AGENTS.mdAGENTS.md
- docs/AGENTS.mdAGENTS.md
- .github/workflows/AGENTS.mdAGENTS.md
- nix/AGENTS.mdAGENTS.md
- proto/AGENTS.mdAGENTS.md
- sdks/AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- .github/workflows/CLAUDE.mdCLAUDE.md
- nix/CLAUDE.mdCLAUDE.md
- proto/CLAUDE.mdCLAUDE.md
- sdks/CLAUDE.mdCLAUDE.md
- authoring-specsSkill
- check-ciSkill
- checking-spec-complianceSkill
- reviewing-specsSkill
- smoke-checkSkill
- working-with-nixSkill
- working-with-worktreesSkill
- writing-agent-docsSkill
- writing-rustSkill
- writing-typescriptSkill
The file
We split this skill into the rules it gives an agent
7 rules. About 0 are common, 3 are distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 10
Always #[xmtp_common::test], never #[test] or #[tokio::test].
- Very specific: line 50
No crate-wide tests/ directory of unrelated modules.
- Distinctive: line 63
Every new API endpoint gets an integration test: happy path, each error, each limit.
- Distinctive: line 66
Never disable a test to hide a bug.
- Distinctive: line 83
Test behaviour, not implementation.
- Very specific: line 85
Use tester! and the convenience methods.
- Very specific: line 86
Use the wait helpers. Never a bare sleep loop.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Writing Rust Tests in libxmtp
Essentials
Always #[xmtp_common::test], never #[test] or #[tokio::test]. It picks
tokio::test on native and wasm_bindgen_test on wasm, and installs logging.
The only exception is a crate xmtp_common depends on, such as xmtp_logging.
#[xmtp_common::test(unwrap_try = true)]
async fn test_something() {
tester!(alix);
tester!(bo);
let group = alix.create_group(None, None)?;
group.invite(&bo).await?;
group.send_msg(b"hello").await; // MlsGroupExt; unwraps internally
bo.sync_welcomes().await?;
let bo_group = bo.group(&group.group_id)?;
let msg = bo_group.test_last_message_bytes().await?;
assert_eq!(msg.unwrap(), b"hello");
}Macro options (crates/xmtp_macro/src/test_macro.rs); an unknown key is a
compile error:
| Option | Default | Use |
unwrap_try = true | off | ? becomes .unwrap() in a test returning () |
flavor = "multi_thread" | current_thread | tests that need parallel tokio workers |
worker_threads = 4 | CPU count | with multi_thread only |
disable_logging = true | off | quiet one test; XMTP_TEST_LOGGING=false or CI=true quiets all |
tester!(name) builds a registered client whose name appears in logs. The
macro and every helper here sit behind xmtp_common's test-utils feature or
cfg(test).
Where tests live
- Beside the module they exercise. A short file keeps
#[cfg(test)] mod testsinline. A small suite gets a module-localtests.rs. A large suite gets atests/directory split by behaviour (crates/xmtp_mls_engine/src/groups/tests/). No crate-widetests/directory of unrelated modules. - The
xmtp_mls_*crates below the facade build no client: notester!orClientBuilderunder theirsrc/. Unit tests that read private state stay beside their code and build real-backend peers throughxmtp_mls_engine::groups::test_support::EngineTestPeer. Suites that need a client go in that crate'stests/directory, through itsxmtp_mlsdev-dependency. Contracts ofClient, its builder, workers and task runner stay incrates/xmtp_mls/src/client/tests/on real clients. - One test-support module per crate:
crates/xmtp_mls/src/utils/test/,apps/backend/src/test_support.rs. #[cfg(test)]for helpers only this crate uses;#[cfg(any(test, feature = "test-utils"))]when another crate imports them, and forward the feature to dependencies.- Every new API endpoint gets an integration test: happy path, each error, each limit. Integration tests cross real service boundaries.
- Moving a test preserves its assertions, coverage, and runner inclusion.
- Never disable a test to hide a bug.
Running
just test # workspace, nextest profile ci
just test workspace test_send_message # one test by name
just test crate xmtp_mls xmtp_db # one or more crates
just wasm test # configured wasm crates
just backend test --lib config # apps/backend module; cargo test, needs `just backend db-up`Client tests need the stack: just backend up. Ports differ per worktree; see
the working-with-worktrees skill. Filters and profiles: running.md.
Philosophy
- Test behaviour, not implementation. Skip trivial getters and derive impls.
- One focus per test. Do not duplicate coverage an existing test gives.
- Use
tester!and the convenience methods. Never build a client by hand. - Use the wait helpers. Never a bare
sleeploop.
Reference
- fixtures.md —
tester!options, convenience methods,MlsGroupExt, mobile fixtures - backend.md —
apps/backendfixtures,EphemeralBackendforxmtp_mlstests - assertions.md —
assert_ok!,assert_err!,wait_for_*, notify helpers - utilities.md — generators, logging control,
LogCapture, retry, toxiproxy - parametrized.md — rstest cases and fixtures, timeouts, attribute order
- wasm.md — skipping on wasm, platform macros, cross-platform time
- running.md — nextest filters, profiles, env vars, CI, coverage
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

