gpt4 book ai didi

Go:套件运行后打印文本?

转载 作者:IT王子 更新时间:2023-10-29 02:06:57 24 4
gpt4 key购买 nike

测试包捕获所有输出并且不会打印它,除非测试失败或详细 (-v) 已打开。有没有办法套件完成后输出文本而不需要打开详细信息?

例如:

func TestMain(m *testing.M) {
status := m.Run(m)
fmt.Println("important line to output")
os.Exit(status)
}

不会打印该行。


编辑:我发现如果您从包内部运行测试 (go test),fmt.Println 将起作用,但如果一个或多个包则无效指定 (go test ./...) 除非启用 -v 选项。

最佳答案

The testing package captures all output and will not print it unless a test fails or verbose (-v) is turned on

是什么让你说 stdout 被捕获了?如果我将此代码保存到 main_test.go 并运行 go test:

package main

import (
"fmt"
"os"
"testing"
)

func TestJoe(t *testing.T) {
fmt.Println("from joe")
}

func TestMain(m *testing.M) {
fmt.Println("before run")
s := m.Run()
fmt.Println("after run")
os.Exit(s)
}

我得到:

$ go test
before run
from joe
PASS
after run

关于 ./... 的输出,这里是 go help test 的摘录:

The second, called package list mode, occurs when go test is invoked with explicit package arguments (for example 'go test math', 'go test ./...', and even 'go test .'). In this mode, go test compiles and tests each of the packages listed on the command line. If a package test passes, go test prints only the final 'ok' summary line. If a package test fails, go test prints the full test output. If invoked with the -bench or -v flag, go test prints the full output even for passing package tests, in order to display the requested benchmark results or verbose logging.

关于Go:套件运行后打印文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55963084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com