You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
proposal: go/doc,testing: add playable examples using a *testing.T context #74009
Allow documentation/example writers to expose testable examples that run with a *testing.T context. That would be helpful to write richer examples as they would benefit from the well-known *testing.T API, which provides more flexible assertions infrastructure than just checking stdout result (for existing testable examples).
Proposed solution
Expose exported functions with prefix Example and signature func(*testing.T) as testable examples in a similar way to Example funcs. They would be displayed by x/pkgsite (like on https://pkg.go.dev) with a Test prefix replacing Example.
Such Example...(t *testing.T) would be executed by go test like standard Test...(*testing.T) tests.
Rationale
Testable examples are very useful for documenting the features of a package as they allow the reader to directly play with the API.
Connecting both would allow to provide richer examples, using existing infrastructure of the Go Playground, and in the spirit of the existing testable examples.
Why *testing.T?
Examples expressed as standard tests are much more flexible in how to express assertions. In standard testable examples the only assertion possible is matching of stdout output.
Here are a few case where strict text matching is not convenient because that usually makes the example code much more verbose (to filter/transform/truncate output) and so goes against the purpose of an example which has to be simple and clear:
examples that are system dependent (ex: use of package os or time.Now())
examples that produce binary content
examples that produce long content
*testing.T is a well known API to every Go developer so it fits the example scope for demonstrating features of a package. t.Fatal is a clean way to properly handle unexpected errors without verbosity.
changed the title [-]proposal: testing,x/pkgsite: add testable examples using a *testing.T context[/-][+]proposal: go/doc,testing: add testable examples using a *testing.T context[/+]on Jun 6, 2025
changed the title [-]proposal: go/doc,testing: add testable examples using a *testing.T context[/-][+]proposal: go/doc,testing: add playable examples using a *testing.T context[/+]on Jun 6, 2025
@seankhliao As this proposal is more scoped than #64993 and has a partial implementation in CL 679655, I suggest to detach it from #64993 for the review process.
I would like to go forward and continue to work on the implementation (the go test part) as I currently have some bandwidth but not if it has no chance to go in.
Also, I'm a maintainer of Testify so, like all authors of testing helpers, I definitely want this to improve both our docs and testsuite.
Activity
gopherbot commentedon Jun 6, 2025
Change https://go.dev/cl/679655 mentions this issue:
go/doc: expose playable examples with a *testing.T parameter[-]proposal: testing,x/pkgsite: add testable examples using a *testing.T context[/-][+]proposal: go/doc,testing: add testable examples using a *testing.T context[/+][-]proposal: go/doc,testing: add testable examples using a *testing.T context[/-][+]proposal: go/doc,testing: add playable examples using a *testing.T context[/+]dolmen commentedon Jun 6, 2025
@seankhliao As this proposal is more scoped than #64993 and has a partial implementation in CL 679655, I suggest to detach it from #64993 for the review process.
dolmen commentedon Mar 16, 2026
@neild As you wrote that you like this proposal, what can be done to reopen it independently of #64993?
dolmen commentedon Mar 16, 2026
I would like to go forward and continue to work on the implementation (the
go testpart) as I currently have some bandwidth but not if it has no chance to go in.Also, I'm a maintainer of Testify so, like all authors of
testinghelpers, I definitely want this to improve both our docs and testsuite.