Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: testing: add TB.SetStdout #68810

Open
sivchari opened this issue Aug 9, 2024 · 2 comments
Open

proposal: testing: add TB.SetStdout #68810

sivchari opened this issue Aug 9, 2024 · 2 comments
Labels
Milestone

Comments

@sivchari
Copy link
Contributor

sivchari commented Aug 9, 2024

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.

@gopherbot gopherbot added this to the Proposal milestone Aug 9, 2024
@gabyhelp
Copy link

gabyhelp commented Aug 9, 2024

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@seankhliao
Copy link
Member

see also #59928 testing.TB.Output()

@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

4 participants