Testing
Tests live in test files, whose names end in _test.ant, and ant test runs them. A test declaration names a test and holds its body. The body checks what it expects with assert, and a failed assert fails the test. The Test namespace holds the rest a test body needs: skipping, running in parallel, a temporary directory, a context, and the test's name. Each Test function acts on the current test, so a helper function can call it without being given a handle.
A library, notes.ant, and its tests. Each test gets its own temporary directory from Test.tempDir, which ant test removes when the test ends:
export func save(dir: string, name: string, text: string): Result[string, Error] {
if name.isEmpty() { return Result.fail("empty note name") }
let path = Path.join([dir, "${name}.txt"])
try File.write(path, text)
Result.ok(path)
}import "./notes.ant" { save }
test "save rejects an empty name" {
let r = save(Test.tempDir(), "", "text")
assert(r is Err)
assert(r.error.message == "empty note name")
}
test "save writes the file" {
let path = try save(Test.tempDir(), "todo", "buy milk")
let text = try File.read(path)
assert(text == "buy milk")
}ok 2 passed (0.00s)A failed assert reports the condition, the values it compared, the names it read, and the message. Here total has an off-by-one bug and skips the last price:
func total(prices: Array[int]): int {
var sum = 0
for i in Array.range(0, prices.length() - 1) {
sum += prices[i]
}
sum
}
test "total" {
let prices = [1, 2, 3]
assert(total(prices) == 6, "every price is counted")
}--- FAIL: total_test.ant: total (0.00s)
assertion failed: total(prices) == 6 (3 == 6): every price is counted
prices = [1, 2, 3]
at test "total" (total_test.ant:11:9)
FAIL 1 failed, 0 passed (0.00s)A failed assert ends the test's body at once, after its deferred blocks run. A failed subtest does not end its parent: the parent goes on to its next subtest and fails when it ends.
Functions by job
Checking
- assert: panic, and so fail the test, when a condition is false
- Panic.catch: call a function and return the panic that left it, to test that code panics
Controlling a test
- Test.skip: end the current test as skipped, with a reason
- Test.parallel: run the current test alongside its parent's other parallel tests
Resources for a test
- Test.tempDir: a new directory, removed with its contents when the test ends
- Test.context: a context done when the test's body ends, to stop blocking calls and goroutines
Naming
- Test.name: the current test's full name, such as
parse/0x10
Applications
A goroutine that a test starts must end with the test. Give its blocking calls Test.context(), so they return once the body ends:
test "worker stops with the test" {
let jobs: Channel[int] = Channel.new()
let ctx = Test.context()
go {
while true {
let job = jobs.receive(ctx: ctx) ?? break
assert(job > 0)
}
}
jobs.send(1)
}ok 1 passed (0.00s)Subtests that call Test.parallel run alongside each other, after the subtests of the same parent that do not call it. Test.name gives each its full name:
test "names" {
for name in ["a", "b"] {
test "${name}" {
Test.parallel()
assert(Test.name() == "names/${name}")
}
}
}ok 2 passed (0.00s)To test that code panics, call it through Panic.catch and check the Err:
func pop(stack: Array[int]): int {
stack.pop() ?? panic("pop from an empty stack")
}
test "pop on an empty stack panics" {
let r = Panic.catch({ pop([]) })
assert(r is Err)
assert(r.error.message.contains("empty"))
}ok 1 passed (0.00s)A test that needs something the environment may lack skips with Test.skip in a guard, before its main path. A helper can do the guard, since Test.skip acts on the current test. A skip is not a panic, so Panic.catch does not stop it:
func databaseUrl(): string {
Os.env("DATABASE_URL") ?? Test.skip("DATABASE_URL is not set")
}
test "connects to the database" {
let url = databaseUrl()
assert(url.startsWith("postgres://"))
}ok 0 passed, 1 skipped (0.00s)