Skip to content
The Chat Archive
gpt-4.1-mini-2025-04-14· OpenAI WildChat-4.8M (AI2)

Below is a conversation between the user and the assistant. Give a concise name for this…

  1. User

    Below is a conversation between the user and the assistant. Give a concise name for this chat. Output ONLY the name of the chat (WITHOUT quotes) and NOTHING else.

    User:

    go test 中 ,t.Logf 打印的内容运行时不显示 Assistant: 在 go test 运行时,确实默认情况下 t.Log 和 t.Logf 的输出只有在测试失败时才会显示。如果你想让这些日志在测试通过时也显示出来,有几种方法:

    1. 使用 -v 参数运行测试

    go test -v
    

    加上 -v (verbose) 参数后,t.Log 和 t.Logf 打印的日志会被显示出来。

    2. 代码示例

    func TestExample(t *testing.T) {
        t.Logf("这是一个调试信息")
        // 其他测试逻辑
    }
    

    然后用下面命令运行:

    go test -v
    

    你会看到打印的日志。


    补充说明:

    • 如果不加 -v,只有当测试失败时,日志才会显示出来。
    • fmt.Println 也是直接打印到标准输出,不受以上限制,但 t.Logf 打印的内容会跟测试结果绑定,更适合调试测试用例。

    总结: 想让 t.Logf 打印内容显示,运行测试时一定要加 -v 参数。 User: 我用的 zed 编辑器,如何配置会加上 -v Assistant:...

  2. Assistant

    go test 日志显示及 zed 编辑器配置