Declare a test
#Mark test functions with #[test]. Unit tests commonly live in a cfg(test) module; a panic signals failure.
Fragment
Reference
Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.
TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.
Node’s built-in runner for JavaScript/compiled TypeScript, Go testing, Rust cargo test, and optional GoogleTest for C++. Excerpts assume a test body and named application functions unless marked as complete.
Rust docs Writing automated tests ↗cargo test ↗
Mark test functions with #[test]. Unit tests commonly live in a cfg(test) module; a panic signals failure.
Fragment
cargo test builds and runs unit, integration and documentation tests for the selected package/workspace.
Shell: cargo test; cargo test --workspace
The positional filter selects matching names; arguments after -- go to the test harness.
Shell: cargo test doubles -- --nocapture
Standard #[test] has no dynamic subtest result tree. Prefer separate test functions or a table whose assertion names the failing case.
Fragment
assert_eq! uses PartialEq and prints Debug representations. Equality semantics come from the value’s implementation.
Fragment
Machine epsilon alone is not a universal domain tolerance. Combine absolute/relative policy deliberately.
Fragment
Inspect Result’s error instead of converting an expected error into an accidental unwrap panic.
Fragment
A useful failure says which input failed, what was observed and what was expected. Assert public behavior; avoid pinning incidental private details that are free to change.
Fragment
Use RAII/Drop for owned resources, or explicit teardown where errors matter. A panic does not make an asynchronous cleanup future run automatically.
Fragment
Optional tempfile crate; the surrounding test returns Result. The guard removes the directory on drop.
The standard test harness is synchronous. An optional runtime such as Tokio supplies an async test attribute.
Fragment
Tokio dependency needs macros and rt features.
Tokio timeout drops the inner future on expiry; this does not automatically cancel detached tasks or an already-started blocking operation.
Fragment
Tokio time feature and a surrounding async Result test are required.
Use temporary directories, unique database names/keys, random free ports and explicit clocks/configuration. Restore global state in cleanup when isolation is impossible. Tests that run in parallel must not mutate the same process-wide state unsafely.
Fragment
A stub supplies answers, a fake implements simplified behavior and a mock verifies chosen interactions. Keep essential protocol semantics in the double; separately test the real adapter against the real dependency.
Fragment
Inject a clock/scheduler or use the runner’s documented fake-time facilities. Advance to an observable condition; sleeping a guessed duration creates slow and flaky tests. Real I/O and other threads may not use the fake clock.
Fragment
Cover ordinary input, empty/min/max values, malformed input, duplicates, missing values and meaningful ordering. Derive cases from the contract rather than repeating the implementation’s branches.
Fragment
Property testing checks a relationship over generated inputs and often shrinks a failure. The language’s test runner alone does not always supply a property framework. Define input constraints and preserve the failing seed/example.
Fragment
Property libraries such as proptest and fuzzing tools such as cargo-fuzz are optional, separate tools with different workflows.
Preserve the regression corpus and state any supported input domain; cargo test itself is not a coverage-guided fuzz engine.
Start the actual dependency with a known configuration, create isolated data, exercise one external contract and tear it down. Include serialization, error mapping, timeouts and authorization where they belong to that boundary.
Fragment
Coverage shows what ran under a selected instrumentation model. A high percentage does not prove correct assertions, all input combinations or absence of races. Inspect missing meaningful behavior rather than forcing a target number.
Fragment