iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🙆‍♀️

Running Tests in Zig

に公開

Prepare the test code

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

test {
    try expect(true);
}

Run the test runner

zig test test.zig
All 1 tests passed.

Next, let's try making a test fail

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

test "success" {
    try expect(true);
}

test "failure" {
    try expect(false);
}

When you run the test runner, the following error message is displayed:

$ zig test test.zig
Test [2/2] test.failure... FAIL (TestUnexpectedResult)
/home/masakielastic/zig-linux-x86_64-0.11.0/lib/std/testing.zig:515:14: 0x22423f in expect (test)
    if (!ok) return error.TestUnexpectedResult;
             ^
/home/masakielastic/test.zig:9:5: 0x224395 in test.failure (test)
    try expect(false);
    ^
1 passed; 0 skipped; 1 failed.
error: the following test command failed with exit code 1:
/home/masakielastic/.cache/zig/o/456c8ea4c3250a1e97723674ad8235b2/test

Discussion