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

testing: unclear if T.Name includes sub-tests as part of its string #46488

Closed
mvdan opened this issue Jun 1, 2021 · 5 comments
Closed

testing: unclear if T.Name includes sub-tests as part of its string #46488

mvdan opened this issue Jun 1, 2021 · 5 comments
Labels
Documentation FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@mvdan
Copy link
Member

mvdan commented Jun 1, 2021

The current docs say:

Name returns the name of the running test or benchmark.

For a func TestFoo(t *testing.T), I think it's reasonably clear that Name will be TestFoo.

However, for a sub-test like t.Run("Bar", ...), it's not clear from the documentation if Name will return TestFoo/Bar or just TestFoo.

Another open question is whether the name will be altered, just like it is when printing output. For example, t.Run("bar baz", ...) ends up printing TestFoo/bar_baz in the terminal, so if the subtest name is included in the Name method, I'm not sure which of the two I'd expect.

cc @mpvl as per the owners doc
cc @adg @bradfitz since they added the Name method

@ianlancetaylor
Copy link
Contributor

From the proposal in #17231 I think it's clear that the name of a subtest is intended to be distinct from the name of the enclosing test.

@ianlancetaylor ianlancetaylor added help wanted NeedsFix The path to resolution is known, but the work has not been done. labels Jun 1, 2021
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Jun 1, 2021
@mvdan
Copy link
Member Author

mvdan commented Jun 1, 2021

I'm not sure if that means that Name would always just return the root test's name :)

@ianlancetaylor
Copy link
Contributor

What I mean is that for code like the following the two logged names should be different (and fortunately that is true today):

package x_test

import "testing"

func TestTop(t *testing.T) {
	t.Log(t.Name())
	t.Run("Sub", func(t *testing.T) {
		t.Log(t.Name())
	})
}

@mvdan
Copy link
Member Author

mvdan commented Jun 1, 2021

Ah, gotcha. That's how I guessed it would work, so that's good to know. It seems to also obtain names just like they are printed, e.g. TestTop/Sub_with_spaces for t.Run("Sub with spaces", ...).

@gopherbot
Copy link

Change https://golang.org/cl/337392 mentions this issue: testing: clarify T.Name returns a distinct name of the running test

@golang golang locked and limited conversation to collaborators Jul 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants