猿问

如何在不使用详细选项的情况下从 go test 打印到控制台

我想打印一条信息性消息以从测试中进行控制台,但没有详细的测试输出。


我尝试使用以下方法从测试中打印:


fmt.Println("my message") // STDOUT

fmt.Fprintln(os.Stderr, "my message") // STDERR

t.Log("my message\n") // testing.T log

go test -v如果使用,它们都产生在控制台中显示的相同效果,但如果只是go test使用则不显示。


但是,使用go test -v,我还得到了所有详细的测试输出,例如:


=== RUN   My_Test

--- PASS: My_Test (0.07s)

go help testflag说:


-v

    Verbose output: log all tests as they are run. Also print all

    text from Log and Logf calls even if the test succeeds.

但我想要的是不在所有测试运行时记录它们,但 即使测试成功,仍然打印来自 Log 和 Logf 调用的所有文本


有没有办法从测试中打印可见消息,但看不到RUN和PASS消息?


手掌心
浏览 223回答 3
3回答

噜噜哒

出于显而易见的原因,测试框架“劫持”了标准输出和错误流。所以无论如何,对这些流的写入是否出现在输出中是由测试框架控制的,除了显示或隐藏所有使用标志之外,它没有提供“自定义”它的方法-v。你可以做的是使用-json测试标志:-json    Log verbose output and test results in JSON. This presents the    same information as the -v flag in a machine-readable format.因此,您获得了原本使用 获得的所有输出-v,但每一行都有一个单独的 JSON 对象。具有此测试功能:func TestMy_Test(t *testing.T) {    fmt.Println("[custom] my message from fmt.Println")}的输出go test -v .=== RUN   TestMy_Test[custom] my message from fmt.Println--- PASS: TestMy_Test (0.00s)PASSok      github.com/icza/play    0.002s的输出go test -json .{"Time":"2022-05-10T09:26:26.712800797+02:00","Action":"run","Package":"github.com/icza/play","Test":"TestMy_Test"}{"Time":"2022-05-10T09:26:26.71293072+02:00","Action":"output","Package":"github.com/icza/play","Test":"TestMy_Test","Output":"=== RUN   TestMy_Test\n"}{"Time":"2022-05-10T09:26:26.712946548+02:00","Action":"output","Package":"github.com/icza/play","Test":"TestMy_Test","Output":"[custom] my message from fmt.Println\n"}{"Time":"2022-05-10T09:26:26.712954637+02:00","Action":"output","Package":"github.com/icza/play","Test":"TestMy_Test","Output":"--- PASS: TestMy_Test (0.00s)\n"}{"Time":"2022-05-10T09:26:26.712958774+02:00","Action":"pass","Package":"github.com/icza/play","Test":"TestMy_Test","Elapsed":0}{"Time":"2022-05-10T09:26:26.712964812+02:00","Action":"output","Package":"github.com/icza/play","Output":"PASS\n"}{"Time":"2022-05-10T09:26:26.713170439+02:00","Action":"output","Package":"github.com/icza/play","Output":"ok  \tgithub.com/icza/play\t0.002s\n"}{"Time":"2022-05-10T09:26:26.713573313+02:00","Action":"pass","Package":"github.com/icza/play","Elapsed":0.003}您可以编写一个简单的应用程序来处理和过滤这些 JSON 对象。或者您可以像过滤任何其他输出一样过滤输出。的输出go test -json . |grep '\[custom\]'{"Time":"2022-05-10T09:28:24.197077681+02:00","Action":"output","Package":"github.com/icza/play","Test":"TestMy_Test","Output":"[custom] my message from fmt.Println\n"}如果您还想要“通过”或“失败”消息,请运行go test -json . |grep '"pass"\|"fail"\|\[custom\]'{"Time":"2022-05-10T09:29:26.069181336+02:00","Action":"output","Package":"github.com/icza/play","Test":"TestMy_Test","Output":"[custom] my message from fmt.Println\n"}{"Time":"2022-05-10T09:29:26.069189228+02:00","Action":"pass","Package":"github.com/icza/play","Test":"TestMy_Test","Elapsed":0}{"Time":"2022-05-10T09:29:26.069199239+02:00","Action":"pass","Package":"github.com/icza/play","Elapsed":0}

忽然笑

虽然这在测试期间不打印,但它会在测试后立即打印,这比根本不打印要好。在您的测试文件中定义一个os.File以将消息写入:var messagesFile *os.Filefunc messages() *os.File {&nbsp; &nbsp; if messagesFile == nil {&nbsp; &nbsp; &nbsp; &nbsp; messagesFile, _ = os.Create("tests.out")&nbsp; &nbsp; }&nbsp; &nbsp; return messagesFile}os.Create如果文件不存在则创建一个新文件,否则截断现有文件。在您的测试中,将消息写入该文件:messages().WriteString("my message")使用 运行测试go test,然后cat使用文件。就我而言,我使用make:test:&nbsp; &nbsp; go test .&nbsp; &nbsp; @cat tests.out输出看起来像:ok&nbsp; <path to tests>my message

慕运维8079593

您可以创建自己的日志功能并将其用于在屏幕上打印func Log(args ...interface{}) {&nbsp; &nbsp; fmt.Fprintln(os.Stdout, args...)}您还可以根据通过标志传递的条件打印日志var p = flag.Bool("p", false, "Enable Local Logging")func MyLog(args ...interface{}) {&nbsp; if *p {&nbsp; &nbsp; fmt.Fprintln(os.Stdout, args...)&nbsp; }}例子package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "testing"&nbsp; &nbsp; "os"&nbsp; &nbsp; "flag")var p = flag.Bool("p", false, "Enable Local Logging")func Log(args ...interface{}) {&nbsp; if *p {&nbsp; &nbsp; fmt.Fprintln(os.Stdout, args...)&nbsp; }}func IntMin(a, b int) int {&nbsp; &nbsp; if a < b {&nbsp; &nbsp; &nbsp; &nbsp; return a&nbsp; &nbsp; }&nbsp; &nbsp; return b}func TestIntMinBasic(t *testing.T) {&nbsp; &nbsp; ans := IntMin(2, -2)&nbsp; &nbsp; if ans != -2 {&nbsp; &nbsp; &nbsp; &nbsp; t.Errorf("IntMin(2, -2) = %d; want -2", ans)&nbsp; &nbsp; }}func TestIntMinTableDriven(t *testing.T) {&nbsp; &nbsp; var tests = []struct {&nbsp; &nbsp; &nbsp; &nbsp; a, b int&nbsp; &nbsp; &nbsp; &nbsp; want int&nbsp; &nbsp; }{&nbsp; &nbsp; &nbsp; &nbsp; {0, 1, 0},&nbsp; &nbsp; &nbsp; &nbsp; {1, 0, 0},&nbsp; &nbsp; &nbsp; &nbsp; {2, -2, -2},&nbsp; &nbsp; &nbsp; &nbsp; {0, -1, -1},&nbsp; &nbsp; &nbsp; &nbsp; {-1, 0, -1},&nbsp; &nbsp; }&nbsp; &nbsp; Log("Print to Screen")&nbsp; &nbsp; for _, tt := range tests {&nbsp; &nbsp; &nbsp; &nbsp; testname := fmt.Sprintf("%d,%d", tt.a, tt.b)&nbsp; &nbsp; &nbsp; &nbsp; t.Run(testname, func(t *testing.T) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ans := IntMin(tt.a, tt.b)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ans != tt.want {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.Errorf("got %d, want %d", ans, tt.want)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }}func BenchmarkIntMin(b *testing.B) {&nbsp; &nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; IntMin(1, 2)&nbsp; &nbsp; }}并传递您可以使用的标志-args-args将命令行的其余部分(-args 之后的所有内容)传递给测试二进制文件,不进行解释和更改。因为这个标志占用了命令行的其余部分,所以包列表(如果存在)必须出现在这个标志之前。命令示例:go test -args -p
随时随地看视频慕课网APP

相关分类

Go
我要回答