Last active
August 29, 2015 14:02
-
-
Save aybabtme/f21f7d29d421e15134c0 to your computer and use it in GitHub Desktop.
test logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// io.Writer implementer | |
type writer func(p []byte) (int, error) | |
func (w writer) Write(p []byte) (int, error) { return w(p) } | |
// magic, a testing.T writer! | |
func testwriter(t *testing.T) io.Writer { | |
return writer(func(p []byte) (int, error) { | |
t.Log(string(p)) | |
return 0, nil | |
}) | |
} | |
// magic, a testing.T logger! | |
func testlogger(t *testing.T) *log.Logger { | |
return log.New(io.MultiWriter(testwriter(t), os.Stderr), "[test]", 0) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TestSomething(t *testing.T) { | |
log.SetOutput(testwriter(t)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment