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

Comments

@mvdan
Copy link
Member

@mvdan 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

@ianlancetaylor ianlancetaylor commented Jun 1, 2021

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.

Loading

@mvdan
Copy link
Member Author

@mvdan mvdan commented Jun 1, 2021

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

Loading

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jun 1, 2021

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())
	})
}

Loading

@mvdan
Copy link
Member Author

@mvdan 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", ...).

Loading

@gopherbot
Copy link

@gopherbot gopherbot commented Jul 26, 2021

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

Loading

@gopherbot gopherbot closed this in 7ba8e79 Jul 27, 2021
steeve added a commit to znly/go that referenced this issue Aug 19, 2021
According to the discussion, it is clear that T.Name returns a
distinct name among all tests. However, there is no specification
of how sub-tests with the same specified test name are constructed.
This change only clarifies the uniqueness and the components of the
name without suggesting any explicit format of the returned name.

Fixes golang#46488

Change-Id: I6cebd419b69fb08d8646cb744a129548452042ef
Reviewed-on: https://go-review.googlesource.com/c/go/+/337392
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants