agentleFS
xmtpxmtp/libxmtp/.agents/skills/writing-rust-tests/SKILL.md

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

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
  1. Very specific:

    Always #[xmtp_common::test], never #[test] or #[tokio::test].

    line 10
  2. Very specific:

    No crate-wide tests/ directory of unrelated modules.

    line 50
  3. Distinctive:

    Every new API endpoint gets an integration test: happy path, each error, each limit.

    line 63
  4. Distinctive:

    Never disable a test to hide a bug.

    line 66
  5. Distinctive:

    Test behaviour, not implementation.

    line 83
  6. Very specific:

    Use tester! and the convenience methods.

    line 85
  7. Very specific:

    Use the wait helpers. Never a bare sleep loop.

    line 86

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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:

OptionDefaultUse
unwrap_try = trueoff? becomes .unwrap() in a test returning ()
flavor = "multi_thread"current_threadtests that need parallel tokio workers
worker_threads = 4CPU countwith multi_thread only
disable_logging = trueoffquiet 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 tests inline. A small suite gets a module-local tests.rs. A large suite gets a tests/ directory split by behaviour (crates/xmtp_mls_engine/src/groups/tests/). No crate-wide tests/ directory of unrelated modules.
  • The xmtp_mls_* crates below the facade build no client: no tester! or ClientBuilder under their src/. Unit tests that read private state stay beside their code and build real-backend peers through xmtp_mls_engine::groups::test_support::EngineTestPeer. Suites that need a client go in that crate's tests/ directory, through its xmtp_mls dev-dependency. Contracts of Client, its builder, workers and task runner stay in crates/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 sleep loop.

Reference

  • fixtures.md — tester! options, convenience methods, MlsGroupExt, mobile fixtures
  • backend.md — apps/backend fixtures, EphemeralBackend for xmtp_mls tests
  • 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

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.