如何在Go(lang)测试中捕获或抑制stdout? [英] How can stdout be captured or suppressed for Go(lang) testing?
问题描述
如何在Go测试中捕获或抑制stdout?
我正在尝试自学go(lang)测试.在下面的代码中,myshow.LoadPath将大量信息输出到stdout(这是正常的副作用).但是,当我运行"go test"时,确实会产生非常嘈杂的输出.有没有办法抑制或捕获stdout?
为了进行比较,我正在考虑python世界中的类似问题. http://pytest.org/latest/capture.html#captures
程序包slideshow_test进口 ("os"测试""github.com/golliher/go-hpf/slideshow")func setupTest(){myshow:= slideshow.Slideshow {名称:这是我的节目"}myshow.LoadPath("..")}func TestStub(t * testing.T){如果true == false {t.Fail()}}func TestMain(m * testing.M){setupTest()os.Exit(m.Run())}
os.Stdout
(由 fmt.Printf
等使用)只是一个变量.因此,您可以随时覆盖它,并在必要时将其还原. https://golang.org/pkg/os/#pkg-variables >
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())
}
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
这篇关于如何在Go(lang)测试中捕获或抑制stdout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!