Skip to content

testing: examples do not always capture stdout #12348

Description

@alexcesaro

Examples do not capture stdout when os.Stdout is assigned to a variable outside the example body:

// foo_test.go
package foo

import "os"

var f = os.Stdout

func Example() {
    f.WriteString("a")
    // Output:
    // a
}
$ go test
a--- FAIL: Example (0.00s)
got:

want:
a
FAIL
exit status 1

Activity

  1. minux commented on Aug 26, 2015

    @minux
    Member
  2. gopherbot commented on Aug 26, 2015

    @gopherbot
    Contributor

    CL https://golang.org/cl/13927 mentions this issue.

  3. ianlancetaylor commented on Aug 27, 2015

    @ianlancetaylor
    Contributor

    It doesn't work, so, don't do that. Examples are a convenience feature for documentation, not something that has to work no matter what the program does.

  4. locked and limited conversation to collaborators on Sep 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions