在 zig 中运行测试时如何打印到标准输出?

问题描述 投票:0回答:1

当运行

zig test
时(例如来自这里

const std = @import("std");

fn failingFunction() error{Oops}!void {
    return error.Oops;
}

test "returning an error" {
    failingFunction() catch |err| {
        try std.testing.expect(err == error.Oops);
        std.debug.print("return from catched error: {}", .{err});
        return;
    };
}

控制台的输出很混乱,就像

Test [3/6] test.returning an error...All 6 tests passed.

作为 std.debug.print 和测试运行程序打印到 stderr。

我明白为什么你不想在库测试中打印输出,至少在你发送给其他人的东西中。然而,我的经验是,有时,当您开发库时,这可以很有用。根据 reddit 上的 this 帖子和 github 上的 this 问题,所描述的行为似乎......不知何故有意? docs只是告诉我“默认测试运行程序和Zig标准库的测试命名空间将消息输出到标准错误。”,而不是我必须在测试中避免

print
。那么,有没有办法在运行测试时“干净”地打印到标准输出?

testing stdout zig
1个回答
0
投票

一个选择可能是使用std.log,例如

test "returning an error" {
    failingFunction() catch |err| {
        try expect(err == error.Oops);
        std.log.warn("return from catched error: {}", .{err});
        return;
    };
}
Test [3/6] test.returning an error... [default] (warn): return from catched error: error.Oops
All 6 tests passed.
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.