Proposal Details
I propose to add SetStdout to testing package.
If we want to test about Stdout, we must write like below.
func TestStdout(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
defer r.Close()
defer w.Close()
oldStdout := os.Stdout
t.Cleanup(func() {
os.Stdout = oldStdout
})
os.Stdout = w
// test
}
The reason I propose this API is similar with this.
Stdout is used globally and if we forget to restore the Stdout, many tests would be effected by this. So I think it's better approach to prevent it by to provide TB.SetStdout(). And there are a few codes that use this case in Go core. So I think it's common approach when we want to check stdout by go test.
As another idea, it might be good to check by static analysis.
Thanks.
Proposal Details
I propose to add SetStdout to testing package.
If we want to test about Stdout, we must write like below.
The reason I propose this API is similar with this.
Stdout is used globally and if we forget to restore the Stdout, many tests would be effected by this. So I think it's better approach to prevent it by to provide TB.SetStdout(). And there are a few codes that use this case in Go core. So I think it's common approach when we want to check stdout by go test.
As another idea, it might be good to check by static analysis.
Thanks.