Pular para o conteúdo

Testes e Benchmarks com o Runner

Nesta página

@test marca uma função para ser descoberta por zolo test. A função não recebe argumentos nem retorna valor; use assert_eq para verificar resultados. Vários testes no mesmo arquivo são executados independentemente — uma falha não interrompe as demais:

Testes de adição, multiplicação e divisão; assert com mensagem opcional.

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 registra a função no runner de benchmarks (zolo bench). Ao contrário de @benchmark — que imprime o tempo de cada chamada avulsa —, @bench integra ao pipeline de CI para detectar regressões de performance entre revisões. Os dois podem ser combinados:

Bench de dois algoritmos de soma e composição @bench + @benchmark.

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

Nota: @bench e @test registram a função no runner; em alguns runtimes ela fica fora do escopo global. Se precisar reutilizá-la em código comum, extraia a lógica para uma função auxiliar separada e chame-a de ambos os lados.

Buscar no Zolo

9 resultados

enespt-br