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: document how to export unexported identifiers in *_test.go #56995

Open
changkun opened this issue Nov 30, 2022 · 9 comments
Open

testing: document how to export unexported identifiers in *_test.go #56995

changkun opened this issue Nov 30, 2022 · 9 comments
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@changkun
Copy link
Member

While I am trying to introduce the usage of export_test.go to someone new to Go, I failed to find a document regarding this feature neither in testing nor in go command, not even in the wiki.

I am not sure if I overlooked somewhere, but I would propose documenting this somewhere more visible, such as the testing package's document.

@ianlancetaylor
Copy link
Contributor

To me this sounds like something more for a blog post or a wiki page than the testing package documentation. The export_test.go approach follows from the way that cmd/go works.

@seankhliao
Copy link
Member

From the cmd/go docs there is

Test files that declare a package with the suffix "_test" will be compiled as a separate package, and then linked and run with the main test binary.

@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 30, 2022
@changkun
Copy link
Member Author

The actual format may be different, I think the problem here is there is no official introduction about this, and if I search google export_test.go the source that introduces it is from a third-person blog.

I don't really remember how I learned that it is possible to use export_test.go with package pkg instead of package pkg_test to export internal objects of a package to test. Maybe the cmd/go docs mean in this way, but it is too obscure for a person to be aware of the special existence of export_test.go other than external_test.go.

@seankhliao
Copy link
Member

I just realized, dup of #25223
it's on tip: https://pkg.go.dev/testing@master

@changkun
Copy link
Member Author

changkun commented Nov 30, 2022

It might have some overlaps with #25223, but I think my original intention for opening this issue is about the existence of export_test as a special case. The general *_test.go is something different.

@seankhliao
Copy link
Member

It adds an entire section with examples on using an package abs_test.
Isn't that what this issue is about?

@changkun
Copy link
Member Author

Oh, that's interesting. I thought for exporting purpose, internal members must use the special name export_test, but it seems can be any name:

$ cat a.go
package xxx

var a = 1

$ cat b_test.go
package xxx

var A = a

$ cat c_test.go
package xxx_test

import (
        "testing"

        "./xxx"
)

func TestXXX(t *testing.T) {
        t.Log(xxx.A)
}

The overall usage is a bit tricky because from the current doc f3a1f92

// ... and only its exported identifiers may be used.
// This is known as "black box" testing.

It is a bit hard to realize the mixture of two kinds of *_test files, where one is package xxx and the other is package xxx_test

@ianlancetaylor
Copy link
Contributor

@seankhliao Thanks. @changkun Can we improve the comments there?

There is nothing special about "export_test.go". The way it behaves just follows from the rules about _test.go files and about the package name.

@changkun changkun changed the title testing: document the usage of export_test.go testing: document how to export unexported objects for *_test.go Nov 30, 2022
@changkun changkun changed the title testing: document how to export unexported objects for *_test.go testing: document how to export unexported identifiers in *_test.go Nov 30, 2022
@gopherbot
Copy link

Change https://go.dev/cl/454335 mentions this issue: testing: document how to export unexported identifiers in *_test.go

@seankhliao seankhliao added this to the Backlog milestone Jan 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants