How can stdout be captured or suppressed for Go(la

2020-07-07 08:47发布

How can stdout be captured or suppressed for Go testing?

I am trying to teach myself go(lang) testing. In the code below, myshow.LoadPath prints lots of information to stdout (which is a normal side effect). It does however make for very noisy output when I run "go test" Is there a way to suppress or capture stdout?

For comparison, I'm thinking about something like this from the python world. http://pytest.org/latest/capture.html#captures

package slideshow_test

import (
    "os"
    "testing"

    "github.com/golliher/go-hpf/slideshow"
)

func setupTest() {
    myshow := slideshow.Slideshow{Name: "This is my show"}
    myshow.LoadPath("..")

}

func TestStub(t *testing.T) {
    if true == false {
        t.Fail()
    }
}

func TestMain(m *testing.M) {
    setupTest()
    os.Exit(m.Run())

}

标签: go
4条回答
做自己的国王
2楼-- · 2020-07-07 09:27

Not exactly what you are asking for but still might be helpful.

You can use t.Log (http://golang.org/pkg/testing/#T.Log) and t.Logf (http://golang.org/pkg/testing/#T.Logf) methods in the test method. The output will be printed only if the test fails or the -test.v flag is set.

I also would suggest to use log package to print to the console in myshow.LoadPath. Then you can disable (or capture) the output in the test by setting custom writer using log.SetOutput

查看更多
冷血范
3楼-- · 2020-07-07 09:40

os.Stdout which is used by the fmt.Printf and others is just a variable. So you can overwrite it at any time and restore it back when necessary. https://golang.org/pkg/os/#pkg-variables

查看更多
贼婆χ
4楼-- · 2020-07-07 09:41

To suppress the output during the test I use the following code. I fixes output as well as logging. After test is done it resets the output streams.

func TestStartStowWrongCommand(t *testing.T) {
 defer quiet()()   
 ...                      
}

func quiet() func() {
 null, _ := os.Open(os.DevNull)
 sout := os.Stdout
 serr := os.Stderr
 os.Stdout = null
 os.Stderr = null
 log.SetOutput(null)
 return func() {
  defer null.Close()
  os.Stdout = sout
  os.Stderr = serr
  log.SetOutput(os.Stderr)
 }
}
查看更多
再贱就再见
5楼-- · 2020-07-07 09:54

The output can be suppressed by running the tests with go test .:

$ go help test

Go test runs in two different modes: local directory mode when invoked with no package arguments (for example, 'go test'), and package list mode when invoked with package arguments (for example 'go test math', 'go test ./...', and even 'go test .').

In local directory mode, go test compiles and tests the package sources found in the current directory and then runs the resulting test binary. In this mode, caching (discussed below) is disabled. After the package test finishes, go test prints a summary line showing the test status ('ok' or 'FAIL'), package name, and elapsed time.

In package list 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.

查看更多
登录 后发表回答