Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zenizh/go-capturer
Capture stdout and/or stderr in Go.
https://github.com/zenizh/go-capturer
golang testing
Last synced: 2 days ago
JSON representation
Capture stdout and/or stderr in Go.
- Host: GitHub
- URL: https://github.com/zenizh/go-capturer
- Owner: zenizh
- License: mit
- Created: 2017-12-11T02:16:19.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-06-10T04:07:17.000Z (over 1 year ago)
- Last Synced: 2024-06-19T16:06:57.471Z (5 months ago)
- Topics: golang, testing
- Language: Go
- Homepage: https://godoc.org/github.com/kami-zh/go-capturer
- Size: 8.79 KB
- Stars: 42
- Watchers: 3
- Forks: 9
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-capturer
[![GoDoc](https://godoc.org/github.com/kami-zh/go-capturer?status.svg)](https://godoc.org/github.com/kami-zh/go-capturer)
[![Build Status](https://travis-ci.org/kami-zh/go-capturer.svg?branch=master)](https://travis-ci.org/kami-zh/go-capturer)
[![Go Report Card](https://goreportcard.com/badge/github.com/kami-zh/go-capturer)](https://goreportcard.com/report/github.com/kami-zh/go-capturer)Capture `os.Stdout` and/or `os.Stderr` in Go.
This package is useful for writing tests which print some outputs using `fmt` package.## Usage
This package provides `CaptureStdout()`, `CaptureStderr()` and `CaptureOutput()` functions to capture outputs.
```go
package mainimport (
"fmt"
"os""github.com/kami-zh/go-capturer"
)func ExampleCaptureStdout() {
out := capturer.CaptureStdout(func() {
fmt.Fprint(os.Stdout, "foo")
})fmt.Println(out)
// Output: foo
}func ExampleCaptureStderr() {
out := capturer.CaptureStderr(func() {
fmt.Fprint(os.Stderr, "bar")
})fmt.Println(out)
// Output: bar
}func ExampleCaptureOutput() {
out := capturer.CaptureOutput(func() {
fmt.Fprint(os.Stdout, "foo")
fmt.Fprint(os.Stderr, "bar")
})fmt.Println(out)
// Output: foobar
}
```## Installation
```
$ go get github.com/kami-zh/go-capturer
```## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/kami-zh/go-capturer.
## License
The package is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).