Skip to content

Chapter 19: Testing

Wyn has built-in test blocks - no external framework needed.

Test Blocks

wyn
test "addition works" {
    assert_eq(2 + 2, 4)
}

test "string contains" {
    var s = "hello world"
    assert(s.contains("world"))
}

Run tests with wyn run file.wyn - test blocks are discovered and executed automatically.

Assertions

FunctionDescription
assert(condition)Fails if false
assert_eq(a, b)Fails if a ≠ b
assert_true(condition)Fails if false
assert_false(condition)Fails if true

Test Output

  ✓ addition works
  ✓ string contains

2 tests passed

Testing Functions

wyn
fn fibonacci(n: int) -> int {
    if n <= 1 { return n }
    return fibonacci(n - 1) + fibonacci(n - 2)
}

test "fib base cases" {
    assert_eq(fibonacci(0), 0)
    assert_eq(fibonacci(1), 1)
}

test "fib recursive" {
    assert_eq(fibonacci(10), 55)
}

Test-Driven Development

Write tests first, then implement:

wyn
// 1. Write the test
test "parse_int handles negatives" {
    assert_eq("-42".to_int(), -42)
}

// 2. Run - it passes because to_int() already handles this
// 3. Add edge cases
test "parse_int handles zero" {
    assert_eq("0".to_int(), 0)
}

Running Specific Tests

bash
wyn run tests/test_math.wyn        # Run one test file
wyn run tests/run_tests.wyn        # Run test suite

Benchmarking

bash
wyn bench file.wyn                  # 10 iterations, reports min/avg/p99
wyn bench file.wyn --iterations 100 # More iterations
wyn bench file.wyn --compare        # Show previous results

MIT License - v1.19.1