Testing
A test is a named block of statements the runner can execute on its own. Tests live inline with the code they exercise, and mach test collects every one in the artifact under test, builds it, and runs it.
Declaring a test
A test is a declaration at module scope, the same level as fun, rec, and val. It takes no parameters and is not callable from ordinary code. It exists only for the runner to invoke.
test <identifier> { ... }
The name is an identifier, following the ordinary identifier rules. A string in its place (test "label" { ... }) is refused with a test is named with an identifier, not a string, as in `test name { ... }`. The body is an ordinary block that may use anything in scope in the enclosing module, just like a function body.
use std.print;
use std.runtime;
use std.types.bool.bool;
fun is_leap_year(y: i64) bool {
if (y % 400 == 0) { ret 1; }
if (y % 100 == 0) { ret 0; }
ret y % 4 == 0;
}
test is_leap_year__centuries {
if (!is_leap_year(2000)) { ret 1; }
if (is_leap_year(1900)) { ret 2; }
ret 0;
}
test is_leap_year__fails_on_purpose {
print.println("checking 2023");
if (is_leap_year(2023)) { ret 1; }
ret 3;
}
#[symbol("main")]
fun main(argc: i64, argv: **u8) i64 {
ret 0;
}
Tests live in their own namespace in each module. A test's name is never in scope in code, so test str_len { ... } and fun str_len in one module do not conflict, and no code can name, call, or reference a test. Two tests with the same name in one module are a compile error.
Related tests group under a common subject as subject__case (str_len__empty, str_len__multibyte). This is a convention the compiler does not check.
Every build resolves and type-checks each test body in the modules its artifact reaches, and ordinary builds omit test bodies from IR and object files.
A pub modifier is syntactically accepted before test but carries no meaning. A test is never part of a module's public surface.
Qualified names
A test's qualified name is its module path, #, and its name: app.bin.main#is_leap_year__centuries. # appears in no identifier or module path, so a qualified name never collides with another symbol. It is the test's symbol, and it is the name --list, --filter, the readout and the test records show. A debugger takes it unquoted: break app.bin.main#is_leap_year__centuries in gdb.
Test-only helpers
A helper or fixture that exists only for tests is marked #[testing]. It gets the same treatment as a test body: checked in every build, omitted from ordinary ones, and emitted under mach test. Only a test body or another #[testing] declaration may reference it.
use std.types.bool.bool;
fun is_leap_year(y: i64) bool {
if (y % 400 == 0) { ret 1; }
if (y % 100 == 0) { ret 0; }
ret y % 4 == 0;
}
#[testing]
fun sample_year() i64 { ret 2000; }
test is_leap_year__sample_year {
if (!is_leap_year(sample_year())) { ret 1; }
ret 0;
}
Reporting pass, skip and fail
A test body is checked against an i32 return type, and its result is the test process's exit status, in 0..255. There are three outcomes:
ret 0: pass. Falling off the end of the body returns0, so a body that never returns explicitly is a pass.ret 77: skip. The test could not check anything on this host, so it neither passes nor fails. It does not fail the run, and the runner counts it apart from the passes. 77 is the skip status automake, meson and TAP use.- any other
ret NwithNin1..255: fail, reported as(exit N). Use distinct values to tell which check failed.
The range is part of the protocol. A ret whose value is a literal (or a literal-shaped expression) outside 0..255 is a compile error:
error[test.status_range] test result 256 is outside the status range 0..255
@ src/bin/main.mach:36:5
36 | ret 256;
| ^^^^^^^^
note a test's result is its exit status, where 0 passes, 77 skips and any other value up to 255 fails
A value computed at run time that lands outside the range is folded to 255 and always fails, so a result of 256 can never read as a pass. std's panic exits with 255, so a crash never reads as a skip. The compiler provides no assertion or skip intrinsics.
A test that can run only on some hosts sits in a declaration-level $if, so on any other host it is absent rather than reported as a pass. A test whose gate is only known at run time checks it first and returns 77:
use std.types.bool.bool;
fun cpu_has_aes() bool { ret 0; }
test crypto__aes128_matches_fips_197 {
if (!cpu_has_aes()) { ret 77; } # nothing to check on this host: skip
ret 0;
}
$if ($mach.build.sys != $mach.sys.windows) {
test spawn__a_shell_script_runs {
ret 0;
}
}
Where tests live
Tests are not tied to a single file. Write test name { } declarations directly in the relevant src/ module. mach test builds exactly what mach build builds for the same artifact, the closure its entry reaches through use and fwd, and runs the tests declared there. A test is found through the import chain from an artifact's entry, never by the manifest, so a module no selected artifact reaches is not loaded under test and its tests do not run. There is no test-only artifact and no module that exists only to gather tests.
Only the current project's own tests are collected by default, so a dependency's suite never runs (or fails) as part of your mach test. --include-deps collects dependency tests as well, which is useful when working on a dependency in-tree.
Running with mach test
mach test <path> [options]
mach test is mach build with a different goal. It builds the artifact's objects exactly as mach build does, adds a test object beside each module that declares tests, links one test dispatcher executable covering the selected tests, then runs each of them as its own process and reports the results. A test build always links an executable, even for a library artifact, whose entry states use std.runtime; as mach init --lib writes it.
The readout writes one row per module, once the module's last test finishes: the module, its wall time, and its counts of ok, skip and FAIL. When the run ends, each failure is shown once, in collection order: its qualified name, file:line and its exit code, signal or timeout, the test's captured output indented beneath, and the exact rerun: command. A passing or skipped test stays quiet. The verdict line closes the run. Here is a project with one passing and one failing test:
$ mach test .
app.bin.main 558us 1 ok 1 FAIL
FAIL app.bin.main#is_leap_year__fails_on_purpose src/bin/main.mach:17 (exit 3)
checking 2023
rerun: ./out/linux-x86_64/debug/test/app/app 1
failed 1 of 2 tests 638us
Test objects and the dispatcher
A module that declares tests or #[testing] declarations gets a test object, obj/<project>/<module>.test.o, beside its normal object. The test object holds the module's tests under their qualified names and only what the module's object lacks, so the module's object never changes for tests. After mach build, mach test recompiles no module object, and editing a test recompiles only its module's test object and relinks.
Each run links a small dispatcher of only the selected tests, test/<artifact>/<artifact> under the output directory, and spawns it once per test with the test's index. Each test's stdout and stderr are captured to a file under test/<artifact>/log/. A passing or skipped test's file is removed on the spot, and a failing test's file stays.
Selecting and listing
| Flag | Effect |
|---|---|
--filter <substr> | run only tests whose qualified name contains <substr> |
--include-deps | also run tests declared in dependency modules |
--list | list the collected tests and exit, running nothing |
--threads <n> | compile and link on n threads and run up to n test processes at once (default: host CPUs) |
--timeout <duration> | stop each test after the duration (default: 60s) |
--runner <cmd> | launch each test through <cmd>, in place of the target's runner |
--allow-unrunnable | succeed when nothing selected can run on this host |
--report <human|json|lines> | also write the run's records to stderr as NDJSON or record lines |
--list prints each selected test's qualified name and the test object that holds it, and exits without linking or running anything:
$ mach test . --list
app.bin.main#is_leap_year__centuries ./out/linux-x86_64/debug/obj/app/bin/main.test.o
app.bin.main#is_leap_year__fails_on_purpose ./out/linux-x86_64/debug/obj/app/bin/main.test.o
--filter selects before the dispatcher links, so the dispatcher holds only the selected tests and what they reach, and changing the filter relinks without recompiling:
$ mach test . --filter centuries
app.bin.main 174us 1 ok
passed 1 test 255us
The selection flags -a, -t and -p and the build flags apply too. Tests run once per selected target and profile. A target whose sys and arch are the host's runs each test directly, a target that declares a runner in the manifest runs its tests through it, and any other target is built and reported on a skipped line. --runner replaces the target's runner for one invocation, launching each test as <cmd> <exe> <index>, and the runner's exit status is judged as a native test's is. A finished-module target such as vulkan-spirv links no executable, so it is built and reported as skipped.
Timeouts
Every test runs under a bound, 60 seconds unless --timeout <duration> gives another. The bound applies to each spawned test process independently, from its own spawn, on its whole process group: the group is asked to stop, given one second, and then killed, so no process the test started outlives it. A test that exceeds the bound is the distinct outcome timed out: it is counted separately on the verdict line and still fails the run. With a skipping test and a spinning one added to the project above:
test cpu__needs_a_feature {
ret 77;
}
test spins {
for {}
}
$ mach test . --timeout 1s
app.bin.main 1.0s 1 ok 1 skip 2 FAIL
FAIL app.bin.main#is_leap_year__fails_on_purpose src/bin/main.mach:17 (exit 3)
checking 2023
rerun: ./out/linux-x86_64/debug/test/app/app 1
FAIL app.bin.main#spins src/bin/main.mach:32 (timed out after 1s)
rerun: ./out/linux-x86_64/debug/test/app/app 3
failed 2 of 4 tests (1 timed out), 1 skipped 1.0s
<duration> is a positive integer followed by a unit: ms, s, m, or h (30ms, 30s, 5m, 1h). A bare number, a fraction, zero, or any other unit is a usage error naming the accepted forms, so no value removes the bound.
Records
--report json or --report lines writes the run as records on stderr beside the build's diagnostics: a run_start, one test per result as it finishes, a run_end for each target and profile that runs, a skip for one built and not run, a case per test under --list, and the closing summary. A test or case record names its test by qualified name in name, beside its module, file, line and dispatcher index, and a test record carries its outcome: pass, fail, skip or timeout. The human readout stays on stdout.
Exit codes
0- every test that ran passed or skipped. A project that declares no test reportspassed 0 tests.1- at least one test failed, was killed by a signal, or timed out. Also a user error, such as an unknown flag, a rejected manifest or a test body that does not compile, and a run that executed nothing.2- an internal error of the compiler before the tests could run, or a test that failed for an infrastructure reason.3- an environment failure before the tests could run, such as storage or a file operation the machine refused.
See also
- CLI - the full
machcommand surface, includingmach test - Decorators -
#[testing]for declarations that exist only for tests - Functions - a test body is checked like a function body
- Statements -
if,ret, and the blocks a test body uses - Project layout - where
src/modules and tests live