How can stdout be captured or suppressed for Go(lang) testing?

后端 未结 4 2262
梦如初夏
梦如初夏 2021-02-20 04:32

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

4条回答
  •  [愿得一人]
    2021-02-20 05:26

    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

提交回复
热议问题