Skip to content

Commit

Permalink
Ensure we have a README section for output capture (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Jan 18, 2024
1 parent 933aa62 commit bcb75eb
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,33 @@ func TestTableThings(t *testing.T) {
}
```

### Capturing Stdout and Stderr

We've all been there, trying to test a function that prints but doesn't accept an `io.Writer` as a destination 🙄.

That's where `test.CaptureOutput` comes in!

```go
func TestOutput(t *testing.T) {
// Function that prints to stdout and stderr, but imagine this is defined somewhere else
// maybe a 3rd party library that you don't control, it just prints and you can't tell it where
fn := func() error {
fmt.Fprintln(os.Stdout, "hello stdout")
fmt.Fprintln(os.Stderr, "hello stderr")

return nil
}

// CaptureOutput to the rescue!
stdout, stderr := test.CaptureOutput(t, fn)

test.Equal(t, stdout, "hello stdout\n")
test.Equal(t, stderr, "hello stderr\n")
}
```

Under the hood `CaptureOutput` temporarily captures both streams, copies the data to a buffer and returns the output back to you, before cleaning everything back up again.

### Credits

This package was created with [copier] and the [FollowTheProcess/go_copier] project template.
Expand Down

0 comments on commit bcb75eb

Please sign in to comment.