Unit Tests

Support for unit tests is built into the language.

Use the test keyword to define a unit test and functions in std.testing to check results:

const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;

test "operator + adds integers" {
    try expect(1 + 1 == 2);
    try expect(2 + 2 == 4);
    try expect(3 + 3 == 6);
}

test "operator - subtracts integers" {
    try expectEqual(1, 2 - 1);
    try expectEqual(2, 3 - 1);
    try expectEqual(3, 4 - 1);
}

The try keyword will be explained in Errors. For now, just remember to put it before each call to expect and friends.

zig test runs the tests in one or more source files:

$ zig test unit-tests.zig 
All 2 tests passed.

Next example: Basic Types.