Declare a test
#Put TestXxx functions in a file ending _test.go. Fatal stops this test goroutine; Error records a failure and continues.
Fragment
Import testing.
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.
Go docs Package testing ↗Go fuzzing ↗
Put TestXxx functions in a file ending _test.go. Fatal stops this test goroutine; Error records a failure and continues.
Fragment
Import testing.
go test builds and runs tests. ./... includes packages below the current module directory.
Shell: go test ./...
-run selects matching test/subtest names. -count=1 bypasses the test result cache.
Shell: go test ./... -run TestDouble -count=1
t.Run names a subtest. A table-driven loop gives each input an independently reported case.
Fragment
Import fmt and testing. Go 1.22+ has per-iteration loop variables when enabled by the module language version.
Use == for comparable values. Slices/maps need a suitable comparison such as slices.Equal, maps.Equal or reflect.DeepEqual; these have different contracts.
Fragment
Import slices (Go 1.21+); this example compares []int.
Specify tolerance rather than expecting decimal arithmetic to be exact.
Fragment
Import math.
Check error identity or type with errors.Is/As when the function wraps errors.
Fragment
Import errors and io/fs.
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
t.Cleanup runs when the test and its subtests complete. Cleanup functions run last-in-first-out.
Fragment
Use a completion channel or synchronization, then assert in the test goroutine. Do not call Fatal from an unrelated worker goroutine.
Fragment
Use a deadline-aware operation and cleanup its context. The go test -timeout flag bounds the whole test binary separately.
Fragment
Import context and time; t.Context needs Go 1.24+ and is canceled before Cleanup functions run; load must observe ctx.
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
Go fuzz targets add seed inputs and call Fuzz. The runner retains discovered failures for reproduction.
Fragment
Shell: go test -fuzz=FuzzRoundTrip -fuzztime=10s. Restrict/validate inputs for the codec’s contract.
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