Skip to content

ant test ​

ant test finds test files, runs their tests, and reports the ones that failed. A test file's name ends in _test.ant; what goes in one is on the test page.

sh
ant test [flags] [path ...]

Details ​

  • A path is a test file or a directory. A directory is searched recursively for test files, leaving out directories whose names begin with .. With no path, ant test searches ..
  • Flags come before the paths.
  • Files run one after another, in the order of the paths, and a directory's files in lexical order. Each file runs as a program of its own, so module state never carries from one file to the next.
  • Before any test runs, ant test type-checks the test files with their imports, as ant check does. An error stops the run before the first test. -nocheck leaves the check out.
  • A passing test prints nothing. What a test prints is shown when it fails, or under -v.
  • Output printed while a file's modules initialize, outside any test, is not collected: it goes to standard output, or to standard error under -json.

Basic examples ​

A library, hex.ant, reads decimal and hexadecimal numbers:

ant
export func parse(text: string): int | null {
  if text.startsWith("0x") { text.trimPrefix("0x").toInt(16) } else { text.toInt() }
}

Its tests, in hex_test.ant. ant test hex_test.ant runs both tests, and when every test passes, it prints one line:

ant
import "./hex.ant" { parse }

test "parse" {
  assert(parse("42") == 42)
  assert(parse("0xff") == 255)
}

test "rejects words" {
  assert(parse("ff") == null)
}
text
ok    2 passed (0.00s)

ant test with no path runs every test file under the current directory. In a project that holds only hex.ant and hex_test.ant, it prints the same line:

sh
ant test
text
ok    2 passed (0.00s)

A failing test is reported with its file, its full name, and the failure. This table has a row the library gets wrong, so ant test broken_test.ant prints the failure, then the counts, and exits with status 1:

ant
import "./hex.ant" { parse }

test "parse" {
  for input in ["0x1f", "0X1F"] {
    test "${input}" {
      assert(parse(input) == 31)
    }
  }
}
text
--- FAIL: broken_test.ant: parse/0X1F (0.00s)
    assertion failed: parse(input) == 31 (null == 31)
      input = "0X1F"
      at test "0X1F" (broken_test.ant:6:13)
FAIL  1 failed, 1 passed (0.00s)

The counts are of tests without subtests: the two rows, not the test parse that holds them. A parent that fails only because a subtest did is not reported again.

Flags ​

FlagMeaning
-run regexpRun only the tests whose full name matches, one part of the pattern per level
-skip regexpLeave out the tests that match, the same way
-list regexpList the top-level tests whose names match, and run none
-vPrint every test as it starts and ends, with its output
-jsonPrint events as JSON lines instead of the report
-failfastStart no test after the first failure
-nocheckRun the tests without type-checking the test files first
-parallel nThe most tests that run at once after Test.parallel(); the default is the CPUs it may use, Os.cpus
-timeout dEnd the run after d, such as 30s or 2m; the default is 10m, and 0 means no limit

Scope ​

Directories ​

A directory runs every test file under it. With -v, each test prints a line when it starts and when it ends. The file is named by the path it was found by. ant test -v strings runs trim_test.ant before upper_test.ant, in lexical order, and shows what the test printed, indented, as it comes:

ant
test "upper" {
  assert("ant".toUpper() == "ANT")
}
ant
test "trim" {
  println("trimming")
  assert("  ant ".trim() == "ant")
}
text
=== RUN   strings/trim_test.ant: trim
    trimming
--- PASS: strings/trim_test.ant: trim (0.00s)
=== RUN   strings/upper_test.ant: upper
--- PASS: strings/upper_test.ant: upper (0.00s)
ok    2 passed (0.00s)

Choosing tests with -run ​

The pattern is split on / into parts, one per level of subtests. Each part is a regular expression matched anywhere in the name at its level. ant test -v -run parse/0x run_test.ant runs the rows of parse whose names contain 0x. The parent parse runs too, since its body is what starts the rows:

ant
import "./hex.ant" { parse }

test "parse" {
  for input in ["7", "0x10", "0xff"] {
    test "${input}" {
      assert(parse(input) != null)
    }
  }
}

test "rejects words" {
  assert(parse("seven") == null)
}
text
=== RUN   run_test.ant: parse
=== RUN   run_test.ant: parse/0x10
--- PASS: run_test.ant: parse/0x10 (0.00s)
=== RUN   run_test.ant: parse/0xff
--- PASS: run_test.ant: parse/0xff (0.00s)
--- PASS: run_test.ant: parse (0.00s)
ok    2 passed (0.00s)

-run parse alone runs parse with all its rows. A test that is left out is neither run nor counted.

Listing tests ​

-list prints the top-level tests whose names match a regular expression, without running them. . matches every test:

ant
test "split" {
  for input in ["a b", "a  b"] {
    test "${input}" {
      assert(input.fields() == ["a", "b"])
    }
  }
}

test "join" {
  assert(String.join(["a", "b"], " ") == "a b")
}
text
list_test.ant: split
list_test.ant: join

Subtests are not listed, since a test's body computes their names as it runs. Under -json, each test is a line such as {"action":"list","file":"list_test.ant","test":"split"}.

Leaving tests out with -skip ​

-skip leaves out the tests that match, with their subtests. ant test -skip network skip_test.ant runs only parse:

ant
test "parse" {
  assert("42".toInt() == 42)
}

test "network" {
  test "fetch" {
    let addr = Os.env("SERVICE_ADDR") ?? "localhost:8080"
    assert(addr.contains(":"))
  }
}
text
ok    1 passed (0.00s)

Stopping at the first failure ​

-failfast starts no test after one fails. ant test -failfast failfast_test.ant reports first and never starts second:

ant
test "first" {
  assert(1 + 1 == 3)
}

test "second" {
  assert(2 + 2 == 5)
}
text
--- FAIL: failfast_test.ant: first (0.00s)
    assertion failed: 1 + 1 == 3 (2 == 3)
      at test "first" (failfast_test.ant:2:9)
FAIL  1 failed, 0 passed (0.00s)

Skipped tests ​

A test that calls Test.skip stops there and counts as skipped. Under -v, the report gives the reason on the line after --- SKIP:, and the counts end with the skipped tests:

ant
test "db" {
  Test.skip("no database")
}

test "parse" {
  assert("42".toInt() == 42)
}
text
=== RUN   db_test.ant: db
--- SKIP: db_test.ant: db (0.00s)
    no database
=== RUN   db_test.ant: parse
--- PASS: db_test.ant: parse (0.00s)
ok    1 passed, 1 skipped (0.00s)

Time limit ​

-timeout ends a run that takes too long:

ant
test "waits" {
  Time.sleep(1m)
}

The command exits with status 1 and writes the tests still running to standard error:

sh
ant test -timeout 100ms slow_test.ant
text
test timed out after 100ms
running tests:
    slow_test.ant: waits

Under -json, each test still running also gets a "fail" event with the message test timed out after 100ms, and the run ends with the "done" event, which counts them as failed.

JSON events ​

Under -json, ant test writes one JSON object per line to standard output and nothing else. Each has an action: "run", "output", "pass", "skip", "fail", and last "done":

sh
ant test -json broken_test.ant
text
{"action":"run","file":"broken_test.ant","test":"parse"}
{"action":"run","file":"broken_test.ant","test":"parse/0x1f"}
{"action":"pass","file":"broken_test.ant","test":"parse/0x1f","elapsed":0.000021125}
{"action":"run","file":"broken_test.ant","test":"parse/0X1F"}
{"action":"fail","file":"broken_test.ant","test":"parse/0X1F","elapsed":0.00006125,"message":"assertion failed: parse(input) == 31 (null == 31)\n  input = \"0X1F\"\n  at test \"0X1F\" (broken_test.ant:6:13)"}
{"action":"fail","file":"broken_test.ant","test":"parse","elapsed":0.000208667}
{"action":"done","elapsed":0.000576584,"passed":1,"failed":1,"skipped":0}

elapsed is in seconds. A "fail" event's message holds the test's failures as the report shows them. When the checker stops the run, each of its errors is an "error" event with the fields of an ant check -json error, and there is no "done" event. A file that cannot be loaded when it is run is reported with the same event, and the run goes on to its "done" event.

Applications ​

Summarize failures for a CI job ​

Run ant test -json from a script and print one line per failure:

ant
let run = try Cmd.capture(Cmd.new("ant", ["test", "-json", "hex_test.ant", "broken_test.ant"]))
for line in run.stdout.lines() {
  let event = try Json.parse(line)
  if !(event is Map) { continue }
  let message = event["message"]
  if event["action"] == "fail" && message is string {
    println("${event["file"]}: ${event["test"]}: ${message.lines()[0]}")
  }
}
println("exit status ${run.status}")
text
broken_test.ant: parse/0X1F: assertion failed: parse(input) == 31 (null == 31)
exit status 1

Exit status ​

StatusWhen
0No test failed, including when tests were skipped and when there were none
1A test failed, the checker reported an error, or the time ran out
2A file could not be loaded, such as for a syntax error, a missing module, or a name that only a declaration file declares, or the usage is wrong

A file the check cannot load is reported on standard error, and no test runs. ant test syntax_test.ant sum_test.ant reports the syntax error, prints nothing on standard output, and exits with status 2:

ant
test "sum" {
  assert(1 + == 2) // error: unexpected '=='
}
ant
test "sum" {
  assert(1 + 1 == 2)
}

Under -nocheck the other files still run: the same command with -nocheck runs sum_test.ant, prints FAIL 1 passed, and exits with status 2.

Os.exit in a test ends the whole command with its status, as it ends a program. The tests after it do not run:

ant
test "exits" {
  Os.exit(3)
}

test "never runs" {
  assert(false)
}

ant test exit_test.ant prints nothing and exits with status 3.

Possible issues ​

A type error stops the run ​

ant test checks the test files before it runs them, so a compile error anywhere in them, or in a module they import, runs no test. ant test typed_test.ant writes the error to standard error, as ant check does, and exits with status 1:

ant
test "count" {
  let count: int = "3" // error: cannot use string where int is expected
  assert("${count}" == "3")
}

Types are erased when a program runs, so ant test -nocheck typed_test.ant runs the test, and it passes. Use -nocheck to run tests while part of the program does not check yet.

A panic while a file loads ​

A panic while a test file's modules are initialized, such as in a top-level let, stops that file: none of its tests run, and the file counts as one failed test. The panic goes to standard error, as under ant run, and standard output has only the counts:

ant
let ports = [80, 443]
let third = ports[2]

test "third port" {
  assert(third == 8080)
}
text
FAIL  1 failed, 0 passed (0.00s)

A test that needs a host ​

A declaration file beside a test file is read by the check, so a test that reads a host's namespace type-checks. ant test still cannot run it, since the file declares the names and does not implement them. Here store.d.ant declares a constant of a host:

ant
// Store.LIMIT is the most bytes a value may hold.
let Store.LIMIT: int

ant test greet_test.ant passes the check and then refuses the file when it loads:

ant
test "limit" {
  assert(Store.LIMIT > 0)
}
text
FAIL  no tests (0.00s)

The reason goes to standard error:

text
greet_test.ant:2:10: Store is declared in store.d.ant, which does not implement it: the program runs only in a host that provides Store [undefined]
   │
 2 │   assert(Store.LIMIT > 0)
   │          ^^^^^

1 error

No test of that file runs, the tests of the other files still run, and the command exits with status 2, with or without -nocheck. Under -json the reason is an "error" event on standard output. Run such tests in the host that provides the namespace.

A name that holds / ​

-run splits its pattern on every /, so a test named v1/users is matched by v1 alone, and its subtests by users, which none of them match. ant test -v -run v1/users routes_test.ant runs the parent's body and none of its subtests:

ant
test "v1/users" {
  test "empty" {
    assert("".isEmpty())
  }
  test "one" {
    assert("a".length() == 1)
  }
}
text
=== RUN   routes_test.ant: v1/users
--- PASS: routes_test.ant: v1/users (0.00s)
ok    1 passed (0.00s)

Escape the slash, -run 'v1\/users', or put it in brackets, -run 'v1[/]users', to match it as part of a name.

A pattern that matches nothing ​

A -run pattern that matches no test is not an error: ant test prints ok no tests (0.00s), writes ant test: warning: no tests to run to standard error, and exits with status 0. Patterns are case-sensitive, so -run Parse matches no test named parse. Use -list with the same first part to see which tests a pattern selects.

A file that is not a test file ​

A path named directly must be a test file, whose name ends in _test.ant. Naming the library instead of its tests, as in ant test words.ant, is a usage error: nothing runs, and the command exits with status 2.

ant
export func count(text: string): int {
  text.fields().length()
}

ant test writes the reason to standard error:

text
ant test: words.ant is not a test file, whose name ends in _test.ant

See also ​

  • test: writing tests and subtests
  • Testing: how test files, subtests, and Test fit together
  • assert: the check a test makes
  • Test: skip, run in parallel, temporary directories
  • ant check: type-check every file, including the scripts no test imports