-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Closed
Labels
Milestone
Description
When working on applications I often find myself copy pasting some boilerplate similar to the following into my tests:
func testLogger(t *testing.T) *log.Logger {
return log.New(testWriter{t}, "test", log.LstdFlags)
}
type testWriter struct {
t *testing.T
}
func (tw testWriter) Write(p []byte) (n int, err error) {
tw.t.Log(string(p))
return len(p), nil
}then when I test functions in my application I can pass in the testing logger and not end up with lots of logging unless the tests failed (in which case the logs show up in a more-or-less sane way and I only see logs generated by the specific tests that failed).
I would like to propose adding this to the testing package as a method on T:
// Logger returns a new logger that logs output using c.Print.
func (c *T) Logger() *log.Logger {}Alternatively, making *T a writer would require no new dependencies and be more generic but would require a bit more boiler plate for the test writer:
// Write satisfies the io.Writer interface for T that "writes" to t.Print.
// Write always returns len(p), nil.
func (c *T) Write(p []byte) (n int, err error) {}If accepted, this adds one method to type T which would need to be covered under the compatibility promise.
Reactions are currently unavailable