Running Tests
Workspace Tests
Platform-Specific Tests
Direct Cargo Commands
You can also run tests directly with cargo:Many team members use
cargo nextest for better test isolation and log output behavior:Interactive WebAssembly Tests
For interactive testing of a specific package:Browser SDK Tests
Test Log Output Control
LibXMTP provides several environment variables to control test log output:Contextual Logging
Output test logs in an async-aware, context-specific tree format:Contextual logging supports
TestLogReplace for replacing IDs with human-readable names.Filtering by Crate
Control log levels for specific crates:Structured JSON Logging
Output logs in JSON format for inspection with third-party viewers:Making Logs More Readable
Replace InboxIds/InstallationIds/EthAddresses with human-readable names in logs.This only works with the
CONTEXTUAL=1 flag enabled.Method 1: TestLogReplace
Add aTestLogReplace declaration at the top of your test:
TestLogReplace object is dropped.
Method 2: TesterBuilder with Names
Build theTesterBuilder with a name:
- InboxIds with
"alix" - InstallationIds with
"alix_installation" - Identifiers with
"alix_identifier"
Writing Tests
Test Macro
ALWAYS use
#[xmtp_common::test(unwrap_try = true)] instead of #[test]This ensures tests run in both native and WASM environments.unwrap_try = true parameter automatically unwraps ? operators, providing better error messages.
Parameterized Tests
Userstest for parameterized tests:
Tests Requiring Wallets
Use thetester! macro for tests that need a wallet:
Code Coverage
Generate and View Coverage
Run tests and open the coverage report in your browser:cargo llvm-cov to generate both lcov and HTML reports.
IDE Integration
If you have the “Coverage Gutters” extension installed in VS Code, you can view coverage information directly in your IDE.CI Integration
Code coverage is automatically generated in CI usingcargo llvm-cov and reported to codecov.
Code Quality Checks
Before running tests, ensure your code passes all quality checks:Best Practices
- Use cargo nextest: Provides better test isolation and output
- Enable CONTEXTUAL logging: Makes async test logs easier to read
- Filter logs by crate: Reduce noise by disabling irrelevant logs
- Use descriptive test names: Use
TesterBuilder.with_name()for better log output - Run coverage locally: Check coverage before submitting PRs
