std.test - ZZ
Asserts, temp dirs, free ports, snapshots, black box. @test needs no import. All asserts work bare and as std.test.*. Doc-tests run from /// ```zz fences on both engines.
import std.testPurpose
Section titled “Purpose”std.test provides assertions, temporary directories, free port discovery, optimization barriers for benchmarks, and snapshot (golden-file) testing. All asserts work both bare (assert_eq(1+1, 2)) and qualified (std.test.assert_eq(...)).
Attributes
Section titled “Attributes”@testfunc test_add() { assert_eq(1 + 1, 2) }
@test(should_panic = true)func test_boom() { fail("boom") }
@test(ignore = true, reason = "flaky env")func test_skip() { }
@test(timeout = 500)func test_slow() { }
@test(retry = 3)func test_flaky() { }
@test(tag = "integration")func test_net() { }
@test(cases = [[1, 2], [2, 3]])func test_param(a: int, b: int) { assert(a < b) }
@setupfunc setup_module() { }
@teardownfunc teardown_module() { }Asserts
Section titled “Asserts”@testfunc test_all() { assert(1 < 2) assert_eq(1 + 1, 2) assert_ne(1, 2) assert_approx_eq(0.1 + 0.2, 0.3, 0.001) assert_contains("hello", "ell") assert_starts_with("hello", "he") assert_ends_with("hello", "lo") assert_len([1,2,3], 3) assert_gt(2, 1) assert_lt(1, 2) assert_is_some(.some(1)) assert_is_none(.none) assert_is_ok(.ok(1)) assert_is_err(.err("x")) assert_snapshot("hello", "home") fail("unreachable")}| Assert | Notes |
|---|---|
assert(cond) |
bool condition |
assert_eq/ne(left, right) |
structural diff on failure |
assert_approx_eq(a, b, eps) |
floats |
assert_contains(hay, needle) |
str; empty needle passes |
assert_starts_with/ends_with |
str |
assert_len(val, n) |
array/str/dict/bytes (chars for str) |
assert_gt/lt(a, b) |
numeric, mixed int+float ok |
assert_is_some/none/opt |
Option |
assert_is_ok/err(res) |
Result |
fail(msg) / panic(msg) |
abort test |
assert_snapshot(actual, name) |
golden files |
std.test helpers
Section titled “std.test helpers”Return Result so ?/match surface setup failures.
import std.testimport std.fs
@testfunc test_export() { d := "" match test.temp_dir("export") { .ok(v) => d = v, .err(_) => fail("temp_dir failed"), } f := "{d}/out.txt" assert_is_ok(fs.write(f, "hi")) assert(fs.exists(f)) assert_is_ok(fs.remove_dir_all(d))}temp_dir(prefix)— unique 0700 subdir under OS temp, per-call unique (pid+counter+nanos). Clean up in@teardown.free_port()— bind127.0.0.1:0, close, return port. TOCTOU: re-bind immediately, retry on collision, tagserial.black_box(v)— optimization barrier for benches (AOT cannot see through it).- No env/mock framework — use
std.env.set/remove+tag = "serial", inject func params as fakes.
Snapshots
Section titled “Snapshots”@testfunc test_render() { assert_snapshot("hi", "homepage")}Compares against <file-parent>/snapshots/<name>.snap (exact bytes). First run fails Snapshot missing … run with --update-snapshots; review, run zz test --update-snapshots, commit the .snap. Never pass --update-snapshots in CI. Names are single components (/, \, .. rejected).
Doc-tests
Section titled “Doc-tests”/// ```zz fences run as tests on both engines. Modifiers: ,ignore, ,should_panic, ,compile_fail (VM-only), ,no_run (check only), ,prelude (shared setup). # lines compile but hide from rendered docs. Reported as doctest <file>#<n>. --no-doctests skips them.
Gotchas
Section titled “Gotchas”temp_dir— always clean up, usually in@teardown.free_portis TOCTOU — re-bind immediately, retry on collision.black_boxprevents AOT from optimizing away benchmark values.- Snapshot names are single components only.
API reference
Section titled “API reference”20 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.
Full index: API reference · Standard library