테스팅 네임스페이스

테스팅 네임스페이스 (The Testing Namespace)

Zig 표준 라이브러리의 testing 네임스페이스에는 테스트를 만들 때 유용한 함수들이 모여 있어요. 이 문서에서 우리는 앞서 살펴본 expect 함수 말고도 두어 가지 함수를 더 사용하게 되는데, 지금부터 그 함수들을 어떻게 쓰는지 코드로 확인해 볼게요.

출처: Zig Documentation

본문

Zig 표준 라이브러리의 testing 네임스페이스는 테스트를 작성하는 데 도움이 되는 함수들을 담고 있습니다. expect 함수에 더해, 이 문서에서는 다음과 같은 함수들을 사용합니다:

const std = @import("std");

test "expectEqual demo" {
    const expected: i32 = 42;
    const actual = 42;

    // The first argument to `expectEqual` is the known, expected, result.
    // The second argument is the result of some expression.
    // The actual's type is casted to the type of expected.
    try std.testing.expectEqual(expected, actual);
}

test "expectError demo" {
    const expected_error = error.DemoError;
    const actual_error_union: anyerror!void = error.DemoError;

    // `expectError` will fail when the actual error is different than
    // the expected error.
    try std.testing.expectError(expected_error, actual_error_union);
}
$ zig test testing_namespace.zig
1/2 testing_namespace.test.expectEqual demo...OK
2/2 testing_namespace.test.expectError demo...OK
All 2 tests passed.

Zig 표준 라이브러리에는 이 밖에도 Slices나 문자열, 그 외 여러 값을 비교하는 함수들이 들어 있고요, 그렇게 쓸 수 있는 함수들과 함께 std.testing 네임스페이스 전체를 Zig Standard Library에서 확인해 보실 수 있어요.

더 알아보기