A test is an item: test, a name in quotes, and a block. atoll test finds
every one, runs each, and reports each result and a summary.
fn add(a int, b int) int {
return a + b
}
test "adds two numbers" {
assert_eq(add(2, 3), 5)
}$ atoll test calc.at
PASS [ 0.001s] calc.at: adds two numbers
------------
Summary [ 0.012s] 1 test run: 1 passed, 0 failed
Slowest: 0.001s calc.at: adds two numbersEach result is said as its test finishes: its status, how long it ran, and the test’s file and name, which is what the test is known by. A summary follows, under it every test that did not pass, in the order the tests are written, and the slowest tests.
The name is what atoll test lists, filters and reports, so write it as a
sentence. It is a plain string on one line, with no ${…}, and no two tests
of a package share one, whatever their modules or files. A test is written at the top of a file, takes no
modifiers or decorators, and returns nothing: return ends it, and return v
is refused (ATOLL1103).
test, bench and skip are words only at the start of a top-level item.
Anywhere else they are names, so fn test(), test := 1 and x.bench mean
what they always did.
Where tests live
Beside the code. A test in an ordinary file is part of that file: it
sees the file’s private functions and the module’s items, so a helper can
be tested without being exported.
private fn clamp(n int, lo int, hi int) int {
if n < lo {
return lo
}
if n > hi {
return hi
}
return n
}
pub fn percent(part int, whole int) int {
return clamp(part * 100 / whole, 0, 100)
}
test "clamps to its bounds" {
assert_eq(clamp(-5, 0, 100), 0)
assert_eq(clamp(500, 0, 100), 100)
assert_eq(clamp(42, 0, 100), 42)
}In a _test.at file. In a package, a file whose name ends in _test.at
holds what only tests use: integration tests that call the package the way
its users do, and the fixtures and helpers several tests share —
src/main_test.at beside src/main.at. atoll test and atoll check read
those files; atoll build and atoll run do not. The program may not use
what only a test file declares, and naming one from any other file is
ATOLL1088, where it is named. A test beside the code may use a test file’s
helper, because a build leaves that test out too.
A build strips its tests. atoll build and atoll run skip each test
item to its closing brace and build nothing of it, so a test never ships in
the program, and a test that does not type-check fails atoll check and
atoll test but not a build. An edit inside a test re-checks nothing a build
kept.
Assertions
assert(condition, message), assert_eq(actual, expected) and
assert_ne(actual, expected) are in the stdlib. A failed one fails its test,
and says enough to find the cause without running anything again: the
condition as it is written, the value of each expression it looked at, where
the call is (file, line, column, and the function that holds it), and the line
of source with the call underlined:
fn add(a int, b int) int {
return a + b
}
test "totals" {
xs := [add(1, 1), add(2, 2)]
limit := 5
assert(xs.len() == limit - 1, "two sums")
assert(add(2, 2) < limit - 4 || xs.contains(5))
assert_eq(add(2, 2), 5)
}$ atoll test calc.at
FAIL [ 0.002s] calc.at: totals
two sums
assertion failed: xs.len() == limit - 1
xs.len(): 2
limit - 1: 4
at calc.at:8:5, in test "totals"
8 | assert(xs.len() == limit - 1, "two sums")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
assertion failed: add(2, 2) < limit - 4 || xs.contains(5)
add(2, 2): 4
limit - 4: 1
xs: [2, 4]
at calc.at:9:5, in test "totals"
9 | assert(add(2, 2) < limit - 4 || xs.contains(5))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
assertion failed: add(2, 2) == 5
expected: 5
found: 4
at calc.at:10:5, in test "totals"
10 | assert_eq(add(2, 2), 5)
| ^^^^^^^^^^^^^^^^^^^^^^^
------------
Summary [ 0.042s] 1 test run: 0 passed, 1 failed
FAIL [ 0.002s] calc.at: totals
Slowest: 0.002s calc.at: totalsThere is one assertion, and no family of them, by design. assert takes a
condition, and the compiler takes the condition apart: for a comparison, a
call, a !, or the && and || of those, it evaluates each operand once into
a value of its own, tests the condition from those values, and keeps them for
the report. assert(a < b) costs exactly what if !(a < b) does, and the
values are written into the report only when it fails. There is no
assert_lt, assert_contains or assert_some to learn, because
assert(a < b), assert(text.contains(part)) and assert(found.is_some())
say what they check and show the values they checked. assert_eq and
assert_ne remain because a comparison of two values wants expected and
found said that way.
What the condition is, and what the report shows:
| The condition | The report lists |
|---|---|
a < b, a == b, a != b, … |
both sides, unless a side is a literal or a constant |
s.starts_with(p), f(a, b) |
the receiver and each argument |
!e |
what e lists |
a && b, a || b |
the operands of each side that was reached; a side that short-circuited was not evaluated and lists nothing |
| anything else | the condition alone |
An operand is evaluated once, in the order it is written, and what the call does with it is what it did: an operand that is a name is read again for the report, and any other is bound to a name the condition is tested from. A value of a type that cannot be written is listed by its text alone. A message that is anything but a literal or a name is evaluated by the plain call, as it always was, and the values are not listed.
Two strings or two lists compared with ==, in an assert or an assert_eq,
also say where they first differ: for strings that are long or on more than one
line, the byte and its line and column; for lists, the first index whose
elements differ, or the index where the shorter one ends:
assertion failed: lines == want
lines: "one\ntwo\nthree"
want: "one\ntwo\nthree!"
first difference: byte 13 (line 3, column 6)A failure is one call into the standard library, handed the values the
condition looked at, however many: the report is written there, each value by
its own type’s write_debug_to into a line of its own.
A failed assertion does not end the test. The test goes on, and every assertion that fails in it is said, so a test with three wrong answers shows all three. What a test prints is shown under its own result, after its failures.
assert_eq and assert_ne compare any two values of a type that can be
compared and has a Debug form, as Rust’s do: the numbers, string, bool,
char, lists, maps, sets, options, results, and the structs, tuples and enums
made of them, whose Debug is derived where it is asked, Point { x: 1, y: 2 }
or Rect(Point { x: 1, y: 2 }, "q"). Every value a report lists is written in
its Debug form: strings and characters in quotes, with \n and \" written
as escapes. A type with a Display of its own and no Debug is said as it
displays, and one with neither by its text alone. An assert’s message is
optional; it heads the report, above the condition.
An assertion works outside a test too. Under atoll run, a failed one is said
on stderr where it fails, the program goes on, and the run exits 1.
Writing your own
A parameter marked @call_site is filled by the compiler with a reference to a
record of where each call is, and @text(other) beside it puts the source text
of the argument given for other in the record. @text(a, "and", b) writes the
text of a, and and the text of b, one space between. Pass the record you
were given on, and a helper’s failures say where the helper was called, and what
it was handed:
import site
fn expect_even(n int, @call_site @text(n) at &site.CallSite) void {
assert(n % 2 == 0, "${n} is odd, in ${at.function}", at)
}
test "evens" {
expect_even(2 + 2)
}site.CallSite is a struct with the fields file, line, column,
function, text (the text @text asked for) and operands (the text of each
value an assert kept); place() is file:line:column. The compiler writes
one record for each distinct call into the program’s constant data and passes
its address, so a call that holds builds nothing and passes one pointer.
As data
atoll test --format json carries each failed assertion beside the report: an
assertions array on the test, each with its message (the head of the
report), details (the lines under it), condition (the source text), the
operands (the text of each expression and its value, or null for a value
that cannot be written), file, line, column, function and the report.
The MCP server’s test tool answers the same document.
A test that stops
A test that stops the program, with a failed index, a division by zero, a
panic or an unwrap of nothing, fails, and the next test runs. Its result
says what failed and the calls that led there, as atoll run does:
fn pick(xs []int, i int) int {
return xs[i]
}
test "reads past the end" {
assert_eq(pick([1, 2, 3], 3), 0)
}$ atoll test calc.at
FAIL [ 0.002s] calc.at: reads past the end
index out of range
at pick (calc.at:2:12)
2 | return xs[i]
| ^^^^^
at test "reads past the end" (calc.at:6:5)atoll test --format json gives the test a trap object: the message, the
kind (index_out_of_range, divide_by_zero, panic, …), the file, line,
column and function of the first frame of the program’s own, and the
frames, each with its function, file, line, column, whether it is
library code and whether it is waiting. The MCP server’s test tool answers
the same document. See Panics.
? in a test
A test may use ? on anything that can fail: a function, a method, a type’s
own function such as fs.Dir.create, or a local holding a result. Its error is
inferred from what the checked body passes on, so it needs no annotation; two
different errors join in a union, as a function’s inferred errors do. An error
that reaches the end of the test fails it, and the result names the variant:
error ParseError {
Empty
}
fn first_word(s string) string ! ParseError {
if s == "" {
error Empty
}
return (s.split(" ").next() ?? s).clone()
}
test "reads a word" {
w := first_word("hello world")?
assert_eq(w, "hello")
}
test "an empty line is no word" {
w := first_word("")?
assert_eq(w, "")
}$ atoll test words.at
PASS [ 0.001s] words.at: reads a word
FAIL [ 0.001s] words.at: an empty line is no word
returned an error: Empty
------------
Summary [ 0.014s] 2 tests run: 1 passed, 1 failed
FAIL [ 0.001s] words.at: an empty line is no word
Slowest: 0.001s words.at: reads a word, 0.001s words.at: an empty line is no wordSkipping a test
skip in front of a test keeps it compiled and checked, lists it, and does
not run it. It travels with the test, so the reason a test is off is beside
it.
fn add(a int, b int) int {
return a + b
}
skip test "a million additions" {
mut total := 0
for i in 0..1000000 {
total = add(total, i)
}
assert(total > 0)
}With the three tests of this page in one calc.at, after add:
$ atoll test --list calc.at
calc.at:5 adds two numbers
calc.at:9 totals
calc.at:16 a million additions (ignored)
3 test(s) discovered--run-skipped runs the skipped tests too, as any other test.
--include-ignored is the same flag.
Running a subset
| Flag | Effect |
|---|---|
--list |
Print discovered tests and exit without running them |
--filter <SUBSTR> |
Only run (or list) tests whose name contains the substring |
atoll test <INPUT> <FILTER>… |
The same, for each FILTER: a test whose name contains any of them |
-E <EXPR> |
Only run (or list) the tests a filter expression selects; repeated, any of them |
-j <N>, --test-threads <N> |
Run at most N tests at once (default: the machine’s parallelism) |
--slow-timeout <DURATION> |
Say a test is slow each time it runs this long (default 60s) |
--terminate-after <N> |
End a test after N slow periods and fail it as timed out (default 5; 0 never) |
--fail-fast |
Start no test after the first failure; those running finish, the rest are not run |
--no-fail-fast |
Run every test whatever fails (the default) |
--retries <N> |
Run a failed test again, up to N more times; one that then passes is FLAKY and passes the run |
--partition count:K/N, hash:K/N |
Run (or list) the Kth of N shares of the selected tests, for CI shards |
--failed |
Run (or list) only the tests that did not pass the last time they ran; all of them when no run has been kept |
--run-skipped |
Also run each skip test |
--format json |
One JSON document on stdout instead: each test’s name, file, line, outcome (ok, flaky, failed, timed-out, ignored, not-run), message, duration_ms, attempts and failed_attempts, and the counts |
--slowest <N> |
Name the N slowest tests under the summary (default 5; 0 none) |
--junit <PATH> |
Also write the run as JUnit XML for CI: a testsuite per file, a testcase per test with its time, its failure and what it printed, and a flakyFailure (or rerunFailure) for each failed attempt of a retried test |
--no-tests <WHAT> |
What a run that selects no test does: warn (the default) says so and exits 0, pass exits 0, fail exits 4 |
-O <LEVEL> |
Compile at the given optimization level |
$ atoll test --filter adds calc.at
PASS [ 0.001s] calc.at: adds two numbers
------------
Summary [ 0.012s] 1 test run: 1 passed, 0 failed
Slowest: 0.001s calc.at: adds two numbersPassing a directory instead of a file compiles the package as one project and runs every test of its own files, each known by its file and its name. A dependency’s tests are its own package’s to run: a dependency is compiled as a build is, with its tests stripped.
Filter expressions
-E selects tests by what they are:
$ atoll test -E 'test(adds) & file(src/*)'
$ atoll test -E 'file(src/db/**) - test(slow)'
$ atoll test --list -E 'skipped()'| Filter | Selects |
|---|---|
test(M) |
Tests whose name M matches |
file(M) |
Tests in a file whose path M matches |
module(M) |
Tests in a module whose path M matches |
kind(test), kind(bench) |
Tests, or benchmarks |
skipped() |
skip items |
all(), none() |
Everything, nothing |
A matcher M is =text for the whole text, ~text for a part of it,
/regex/ for a regular expression found in it, or #glob for a glob the whole
text matches, where * and ? stay within one /-separated part and **
crosses them. A bare text is a part for test() and module(), and a glob
for file(). \) writes a ) in a text.
Filters combine with | or or, & or and, - (a - b is a & not b),
! or not, and parentheses. Several -Es select what any of them selects,
and a name substring, --filter’s or a positional one, narrows that. --list
lists what a run would run. A malformed expression is refused with exit code
2, and the column where it went wrong.
[test] default-filter in atoll.toml is the expression a run without -E
uses: default-filter = "not test(slow)" leaves a class of tests out of every
plain run, and -E 'all()' brings them back.
Rerunning what failed
Every run keeps what each test it ran came to: a package’s in
target/atoll-test/last-run.json, a lone file’s in the cache directory, under
its path. --failed runs only the selected tests that did not pass the last
time they ran, those that failed, timed out or were not run, and a test that
has never run; with no record it runs them all. A run of some of the tests
leaves what the record says of the others, so fixing failures one
--failed at a time narrows to what still fails:
$ atoll test
...
Summary [ 0.420s] 212 tests run: 209 passed, 3 failed
$ atoll test --failed
...
Summary [ 0.031s] 3 tests run: 2 passed, 1 failed
$ atoll test --failed
...
Summary [ 0.012s] 1 test run: 0 passed, 1 failedSharding a run
--partition splits the selected tests into N shares, so N CI jobs can
each run one. count:K/N takes every Nth test from the Kth, in identity
order; hash:K/N takes the tests whose identity, file: name, hashes to the
Kth share, so a test stays in its share when others are added. K counts
from 1. The shares never overlap, and together they are every selected test;
--list lists one.
$ atoll test --partition hash:1/3
$ atoll test --partition hash:2/3
$ atoll test --partition hash:3/3Running side by side
The program is compiled once, and each test then runs on a fresh instance of
it, with its own heap and its own leak check, on a worker thread: as many at
once as the machine has cores, or -j N. What a test prints and the
assertions it fails are said under its own result, whatever ran beside it.
Tests that share something outside the program, a file or a port, can run one
at a time with -j 1, or, better, in a group of their own.
Groups and overrides
A group bounds how many of its tests run at once, and an override puts tests in one, or sets their timeouts and retries, by a filter expression:
[test.groups.database]
max-threads = 1
[[test.overrides]]
filter = "file(src/db/*)"
test-group = "database"
slow-timeout = "120s"
retries = 2The tests of src/db now run one at a time, beside every other test, which
-j alone bounds. An override may set slow-timeout, terminate-after,
retries and test-group, and the first override whose filter selects a test
and that sets a setting sets it for that test; one it does not set comes from a
later override, then from [test]. A flag beats them all. An override needs a
filter, and a group it names must be declared; otherwise the manifest is
refused.
Slow and stuck tests
A test still running a minute in is said to be SLOW, once a minute, and one
still running five minutes in is ended and fails as TIMEOUT, so a test that
never ends cannot hang a run. A test spinning in a loop is stopped where it
stands; one asleep or waiting on a host call is stopped at the deadline.
SLOW [> 60.000s] net.at: reconnects
TIMEOUT [ 300.010s] net.at: reconnects--slow-timeout sets the period (500ms, 30s, 2m) and --terminate-after
how many of them a test may run, 0 for no end. A package sets them in its
atoll.toml, and a flag beats the table:
[test]
test-threads = 8
slow-timeout = "30s"
terminate-after = 4
retries = 1
fail-fast = trueA key [test] does not hold is refused, so a misspelt one cannot leave a run
without the deadline it meant to set.
Retries and flaky tests
--retries N, or [test] retries = N, runs a failed test again, on a fresh
instance, up to N more times. A test ended at its deadline has failed an
attempt too. One that fails and then passes is FLAKY: it passes the run, its
line says on which attempt of how many it passed, and each attempt that failed
is said under it. The summary counts and names it, so a flaky test does not
pass unnoticed:
$ atoll test --retries 2 net.at
FLAKY 2/3 [ 0.020s] net.at: reconnects
try 1 FAIL [ 0.011s]
net.at:9:5: expected 200, found 503
------------
Summary [ 0.034s] 1 test run: 1 passed (1 flaky), 0 failed
FLAKY 2/3 [ 0.020s] net.at: reconnectsA test that fails every attempt fails, with each earlier attempt said under
it. In a JSON document a flaky test’s outcome is flaky, attempts says
how many it took and failed_attempts what each failed one said; a JUnit
report gives each failed attempt a flakyFailure, or a rerunFailure for a
test that never passed.
Exit codes
| Code | Meaning |
|---|---|
0 |
Every selected test passed, or none was selected |
1 |
A test failed, timed out, or leaked |
2 |
A usage, I/O or configuration error: a bad flag, filter, [test] table or input |
3 |
The program did not compile, so no test ran |
4 |
No test was selected, under --no-tests fail |
That makes atoll test usable directly as a CI gate, and lets the gate tell a
red build from a red test.
Benchmarks
bench is the same item for atoll bench, which runs each one repeatedly and
reports its mean, spread, minimum and maximum in nanoseconds. atoll test
runs no bench, and atoll bench runs no test. A skip bench is listed and
never run. --list, --filter and --format json work as for tests. Benchmarks
run one at a time, so that no measurement measures another.
fn add(a int, b int) int {
return a + b
}
bench "sums a thousand numbers" {
mut total := 0
for i in 0..1000 {
total = add(total, i)
}
}$ atoll bench --iterations 10 sums.at
bench sums a thousand numbers: mean 10305ns ±8053.6ns (min 7275ns, max 34400ns) over 10 iter
result: 1 ok, 0 failedBenchmark at -O2 (the default) unless you are specifically measuring the
effect of a lower optimization level — -O0 skips most of the pipeline and its
numbers do not describe a shipped build.
Testing comptime code and golden files
A package that writes decorators or generators tests them with ordinary test
items and std/meta/testing. The package is linked under the package whose
files import it, so a build, atoll check and atoll lint of a package that
imports it only in tests are as they were; atoll test is the only command that
runs what it says. Each call below writes a scratch package that depends on the
package under test, runs the atoll that is running the tests on it, in a
process of its own, and deletes the scratch package when the call returns. It
needs a POSIX sh.
import std/meta/testing
test "noted adds a note function" {
result := testing.expand(r"""
import example.dev/deco/gen.{noted}
@noted("fast")
fn a() int { return 1 }
""")
assert(result.ok(), result.diagnostics())
testing.expect_file("tests/noted.expanded.at", result.text())
}
test "noted refuses two arguments" {
result := testing.expand(r"""
import example.dev/deco/gen.{noted}
@noted("fast", 2)
fn a() int { return 1 }
""")
assert(result.has_error("takes 0 to 1 argument"), result.diagnostics())
}| Call | What it does |
|---|---|
testing.expand(source) |
Compiles source as main.at of the scratch package and answers what atoll build --expand prints. A refusal is a result: ok() is false and has_error(text) reads it. |
testing.expand_files(files, manifest) |
The same over several files (testing.file(path, text), or testing.from_package(path) for one of the package’s own) and a manifest added to the scratch package’s atoll.toml: a [comptime] grant, or a second dependency. |
testing.run(source) |
Compiles the scratch package and runs its main. text() is its output, diagnostics() what it wrote to standard error, and ok() whether it compiled and ended with status 0. |
testing.regenerate(files, manifest) |
Builds the scratch package with atoll build --wait, so the generators in it run now, and reads back what they wrote: files() lists the *.gen.at files and atoll.gen, and file(path) is one of them. A generator that calls a model needs an endpoint that answers from a file (provider = "fixture"). |
testing.expect_file(path, actual) |
Compares actual with the file at path, relative to the package under test, and fails the test at the first line that differs. |
A result carries ok(), status_code(), text(), diagnostics() (the
scratch directory is written <scratch>), has_error(text) and
has_warning(text), which look in one diagnostic at a time, and files() and
file(path). The failure of an expect_file is said at the call, as an
assertion’s is.
A decorator is compiled with the comptime code of its module, and a module that
declares comptime code holds no test: keep the decorators in a module of their
own (gen/) and the tests in another (tests/); atoll test says so when
a test is in the module of a decorator, a generator or a deriver.
The calls spawn the toolchain and write files, so a [host] list in
atoll.toml must grant what they reach (std/process, std/fs and the
entropy and eprintln operations of std), as it must for any code that
spawns a process.
Golden files and --bless
expect_file never writes under a plain atoll test: a missing file or a
difference fails the test, and says what to run.
FAIL [ 0.357s] tests/expands_test.at: noted adds a note function
tests/expands_test.at:11:5: golden file `tests/noted.expanded.at` differs at line 6:
expected: return "slow"
found: return "fast"
`atoll test --bless` writes the found textatoll test --bless writes each golden file that differs, with its
directories, and moves it into place whole; the test passes and says blessed tests/noted.expanded.at. A file that already holds the text is not written, so
a run that changes nothing changes no file’s time. Review the change as a diff
and commit it: a golden file is updated deliberately, never by a test run that
happens to be red. The comparison is byte for byte, the final newline included.
A path that is absolute, leaves the package with .., is empty or names a
directory is refused, with --bless as well.
The retired spellings
Tests used to be functions marked @test, with @ignore to skip one and
@bench for a benchmark. Each is refused now wherever it is written —
ATOLL1104, ATOLL1106 and ATOLL1105 — because a spelling that still
compiled would keep a test in every build.
fn add(a int, b int) int {
return a + b
}
@test
fn adds() void {
assert_eq(add(2, 3), 5)
}atoll fix rewrites them: a marked function that takes no parameters and
returns nothing becomes the item, named by the function’s name, with any error
type it declared dropped for the inferred one, and @ignore beside @test
becomes skip test. The function above becomes:
fn add(a int, b int) int {
return a + b
}
test "adds" {
assert_eq(add(2, 3), 5)
}A test that returned a value to fail, or one with parameters or another
decorator, has no item to become; atoll fix leaves it and says why, and its
checks are written as assertions by hand.