Documentation ¶
Overview ¶
Package ldlogtest contains test helpers for use with the ldlog package.
This package provides the MockLog type, which allows you to capture output that is sent to the ldlog.Loggers API. This can be useful in test code for verifying that some component produces the log output you expect it to. It is separate from the ldlog package because production code normally will not use this tool.
Index ¶
- type MockLog
- func (ml *MockLog) AssertMessageMatch(t *testing.T, shouldMatch bool, level ldlog.LogLevel, pattern string)
- func (ml *MockLog) Dump(w io.Writer)
- func (ml *MockLog) DumpIfTestFailed(t *testing.T)
- func (ml *MockLog) GetAllOutput() []MockLogItem
- func (ml *MockLog) GetOutput(level ldlog.LogLevel) []string
- func (ml *MockLog) HasMessageMatch(level ldlog.LogLevel, pattern string) bool
- type MockLogItem
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MockLog ¶
type MockLog struct { // Loggers is the ldlog.Loggers instance to be used for tests. Loggers ldlog.Loggers // contains filtered or unexported fields }
MockLog provides the ability to capture log output.
It contains a Loggers instance which can be used like any other Loggers, but all of the output is captured by the enclosing MockLog and can be retrieved with the MockLog methods.
mockLog := ldlogtest.NewMockLog() mockLog.Loggers.Warn("message") mockLog.Loggers.Warnf("also: %t", true) warnMessages := mockLog.GetOutput(ldlog.Warn) // returns {"message", "also: true"}
func (*MockLog) AssertMessageMatch ¶
func (ml *MockLog) AssertMessageMatch(t *testing.T, shouldMatch bool, level ldlog.LogLevel, pattern string)
AssertMessageMatch asserts whether there is a log message of this level that matches this regex. This is equivalent to using assert.True or assert.False with HasMessageMatch, except that if the test fails, it includes the actual log messages in the failure message.
func (*MockLog) DumpIfTestFailed ¶
DumpIfTestFailed is a shortcut for writing all captured log lines to standard output only if t.Failed() is true.
This is useful in tests where you normally don't want to see the log output, but you do want to see it if there was a failure. The simplest way to do this is to use defer:
func TestSomething(t *testing.T) { ml := ldlogtest.NewMockLog() defer ml.DumpIfTestFailed(t) // ... do some test things that may generate log output and/or cause a failure }
func (*MockLog) GetAllOutput ¶
func (ml *MockLog) GetAllOutput() []MockLogItem
GetAllOutput returns the captured output for all log levels.
type MockLogItem ¶
MockLogItem represents a log message captured by MockLog.