Skip to content

proposal: go/doc,testing: add playable examples using a *testing.T context #74009

Closed as duplicate

Description

@dolmen

Proposal Details

Goal

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.

The Go Playground (code: golang.org/x/playground) supports running programs with a main, but also code that instead defines a Test* function with a *testing.T argument (example: https://go.dev/play/p/ECwmV7-1Ohx).

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.

EDIT: enriched rationale section

Activity

  1. added this to the Proposal milestone on Jun 5, 2025
  2. gopherbot commented on Jun 6, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/679655 mentions this issue: go/doc: expose playable examples with a *testing.T parameter

  3. 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
  4. 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
  5. dolmen commented on Jun 6, 2025

    @dolmen
    ContributorAuthor

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

  6. dolmen commented on Mar 16, 2026

    @dolmen
    ContributorAuthor

    @neild As you wrote that you like this proposal, what can be done to reopen it independently of #64993?

  7. dolmen commented on Mar 16, 2026

    @dolmen
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions