Skip to content

Tests and Benchmarks with the Runner

@test marks a function to be discovered by zolo test. The function takes no arguments and returns no value; use assert_eq to check results. Multiple tests in the same file run independently — one failure does not stop the others:

Tests for addition, multiplication, and division; assert with optional message.

08-test.zolo
Playground
// Feature: `@test` — function recognized by the test runner
// Syntax: `@test` before the `fn` (no args, no return). Use `assert_eq`
// inside to validate.
// When to use: unit tests in the same file as the code; discovered by
// `zolo test`.

fn add(a: int, b: int) -> int {
  return a + b
}

fn mul(a: int, b: int) -> int {
  return a * b
}

fn div(a: int, b: int) -> int {
  if b == 0 { panic("div by zero") }
  return a / b
}

@test
fn test_add() {
  assert_eq(add(2, 3), 5)
}

@test
fn test_add_negative() {
  assert_eq(add(-1, 1), 0)
}

@test
fn test_mul() {
  assert_eq(mul(3, 4), 12)
}

@test
fn test_div_ok() {
  assert_eq(div(10, 2), 5)
}

// Optional message shown if the assert fails.
@test
fn test_with_message() {
  assert_eq(add(10, 20), 30, "10 + 20 should be 30")
}

// `main` still runs when you use `zolo run` instead of `zolo test`.
print("module loaded")
// expected: module loaded

@bench registers the function with the benchmark runner (zolo bench). Unlike @benchmark — which prints the time of each individual call — @bench integrates into the CI pipeline to detect performance regressions across revisions. The two can be combined:

Bench of two sum algorithms and @bench + @benchmark composition.

09-bench.zolo
Playground
// Feature: `@bench` — function recognized by the benchmark runner

// Syntax: `@bench` before the `fn`. Unlike `@benchmark` (which measures

// any single call), `@bench` is discovered by `zolo bench`.

// When to use: performance regressions in CI, comparing variants.


fn slow_sum(n: int) -> int {
  var total = 0
  for i in 1..=n {
    total += i
  }
  return total
}

fn fast_sum(n: int) -> int {
  return n * (n + 1) / 2
}

@bench
fn bench_slow_sum() {
  let _ = slow_sum(10000)
}

@bench
fn bench_fast_sum() {
  let _ = fast_sum(10000)
}

// A @bench can also mix with @benchmark to print every call.

@bench
@benchmark
fn bench_combined() {
  let _ = slow_sum(1000)
}

print("benches registered")
// expected: benches registered

Note: @bench and @test register the function with the runner; in some runtimes it falls outside the global scope. If you need to reuse it in common code, extract the logic into a separate helper function and call it from both sides.

enespt-br