Test.name
func Test.name(): stringTest.name returns the current test's full name, its names from the top-level test down joined with /, as "parse/0x10".
Details
- The current test is the test whose body runs the call, or the test that owns the goroutine that makes it. A helper function called from a test gets that test's name without being given it.
- When two subtests of one test get the same name, the second is named with
#2, the third with#3, andTest.namereturns that name. - The full name does not include the test file: tests in two files can have the same full name.
- Called outside any test,
Test.namepanics. That includes the initialization of a test file's top-level variables, which runs before any test.
Basic examples
In a top-level test, Test.name returns the name the test declares:
test "parse" {
assert(Test.name() == "parse")
}ok 1 passed (0.00s)In a subtest, it returns the full name, with the parent's name first:
test "parse" {
test "0x10" {
assert(Test.name() == "parse/0x10")
}
}ok 1 passed (0.00s)Scope
Subtests from a table
A subtest's name may interpolate, and Test.name returns the name as it was evaluated. Under ant test -v, each test's output appears below the === RUN line that ant test prints for it, after the file name. ant test puts the file name before the full name; Test.name leaves it out:
test "parse" {
for input in ["1", "0x10"] {
test "${input}" {
println(Test.name())
}
}
}=== RUN table_test.ant: parse
=== RUN table_test.ant: parse/1
parse/1
--- PASS: table_test.ant: parse/1 (0.00s)
=== RUN table_test.ant: parse/0x10
parse/0x10
--- PASS: table_test.ant: parse/0x10 (0.00s)
--- PASS: table_test.ant: parse (0.00s)
ok 2 passed (0.00s)Nested subtests
Subtests nest to any depth, and the full name holds every level:
test "server" {
test "routes" {
test "GET /users" {
assert(Test.name() == "server/routes/GET /users")
}
}
assert(Test.name() == "server")
}ok 1 passed (0.00s)A name may itself hold a /, as GET /users does here, so the full name cannot always be split back into its levels.
Repeated names
When two subtests of one test get the same name, the second is named with #2, and Test.name returns that name:
test "words" {
let names: Array[string] = []
for word in ["a", "b", "a"] {
test "${word}" {
names.push(Test.name())
}
}
assert(names == ["words/a", "words/b", "words/a #2"])
}ok 3 passed (0.00s)Helper functions
A function called from a test reads the name of the test that calls it:
func where(): string {
"in ${Test.name()}"
}
test "first" {
assert(where() == "in first")
}
test "second" {
test "inner" {
assert(where() == "in second/inner")
}
}ok 2 passed (0.00s)Goroutines
A goroutine started in a test belongs to it, so Test.name there returns the test's name:
test "worker" {
let names: Channel[string] = Channel.new()
go { names.send(Test.name()) }
assert(names.receive() == "worker")
}ok 1 passed (0.00s)Applications
Golden files
Keep the expected output of each test in a file named after it. A subtest's name holds /, so replace it to keep the files in one directory:
func golden(): string {
let file = Test.name().replace("/", "_")
File.read("golden/${file}.txt").unwrap().trim()
}
test "greet" {
test "hello" {
assert(golden() == "Hello, Ann")
}
test "bye" {
assert(golden() == "Goodbye, Ann")
}
}ok 2 passed (0.00s)Labeled log lines
A logging helper can tag each line with the test that wrote it, without each test passing its name:
func log(message: string) {
println("[${Test.name()}] ${message}")
}
test "half" {
for n in [4, 7] {
test "${n}" {
log("halving ${n}")
assert(n % 2 == 0)
}
}
}--- FAIL: log_test.ant: half/7 (0.00s)
[half/7] halving 7
assertion failed: n % 2 == 0 (1 == 0)
n = 7
at test "7" (log_test.ant:9:13)
FAIL 1 failed, 1 passed (0.00s)Properties and relations
A subtest's name starts with its parent's name and a /:
test "outer" {
let parent = Test.name()
test "inner" {
assert(Test.name() == "${parent}/inner")
}
}ok 1 passed (0.00s)Possible issues
Outside a test there is no current test, and Test.name panics:
println(Test.name()) // panic: Test.name: not in a testA test file's top-level variables are set before any test runs, so Test.name panics there too. Call it inside the test body:
let name = Test.name() // panic: Test.name: not in a test
test "t" {
assert(name == "t")
}FAIL 1 failed, 0 passed (0.00s)See also
- test: tests, subtests, and their names
- Test.skip: end the current test as skipped
- Test.tempDir: a directory for the current test
- Test.context: a context that ends with the current test
- ant test: run tests by name with
-run, or list the top-level tests a pattern selects with-list
