Skip to content

Tests

Tests live next to the code they test:

fn add(a: i64, b: i64) -> i64 {
return a + b
}
test "adds numbers" {
assert_eq(add(2, 3), 5)
assert(add(-1, 1) == 0)
}
  • brass test file.brass runs every test in that file and in the modules it imports; brass test file.brass -- name runs those whose name contains name.
  • Each test runs in its own process, so a panic fails only that test.
  • assert(cond) and assert_eq(a, b) (for i64, f64, bool, Str, showing both values when they differ) can be used anywhere.
  • Tests are type-checked by brass check and brass build too, so they cannot silently rot.

A property test takes generated values:

fn sorted(xs: &Vec<i64>) -> Vec<i64> {
let mut out = xs.clone()
out.sort()
return out
}
test "sorting keeps every element" for xs: Vec<i64> {
assert_eq(sorted(&xs).len(), xs.len())
}

It runs 100 times (BRASS_CASES) with values of integer types, bool, f64, Str (with quotes, line breaks and multi-byte characters mixed in), and Vec, Option and tuples of those. Each value is built from a sequence of random choices; when a case fails, that sequence is shrunk (choices dropped, zeroed or halved, each try run in its own process) until no smaller one fails, and the smallest input is reported, like xs = [0, 0], with the seed to repeat the run (BRASS_SEED).

bench "name" { ... } blocks are skipped by brass test and run by brass test --bench [-- filter], which repeats each body (doubling the count) until it has taken a fifth of a second and prints the time per run.

brass test --coverage counts which lines of code ran in any test and ends with a report per file: how many lines ran, and which did not.