testutil

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package testutil provides test utilities for packages that use the logger package.

This package offers a MockLogger that implements the logger.Logger interface, capturing all log calls in memory for later inspection. MockLogger is safe for concurrent use and supports the full Logger API including WithFields, WithContext, and WithLevel.

Assertion helpers are provided for common test patterns:

  • AssertLogCount verifies the number of captured log entries.
  • AssertLogContains checks that a log at a given level contains a substring.
  • AssertLogMessage checks for an exact message match at a given level.
  • AssertLogField verifies a structured field key-value pair exists.
  • AssertLogLevel verifies that at least one log at a given level exists.
  • AssertNoLogs verifies that no logs were captured.
  • AssertNoLogsAtLevel verifies no logs at a specific level were captured.

Usage:

mock := testutil.NewMockLogger()
logger.SetLogger(mock)

// Run the code under test...

testutil.AssertLogCount(t, mock, 1)
testutil.AssertLogContains(t, mock, logger.LevelInfo, "request received")

Package testutil implements MockLogger for capturing and asserting log output in tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertLogContains

func AssertLogContains(reporter TB, mock *MockLogger, level logger.Level, contains string)

AssertLogContains verifies that at least one log at the given level contains the expected substring.

func AssertLogCount

func AssertLogCount(reporter TB, mock *MockLogger, expected int)

AssertLogCount verifies the number of captured logs.

func AssertLogField

func AssertLogField(reporter TB, mock *MockLogger, level logger.Level, key string, value any)

AssertLogField verifies that at least one log at the given level has the specified field.

func AssertLogLevel

func AssertLogLevel(reporter TB, mock *MockLogger, level logger.Level)

AssertLogLevel verifies that at least one log at the given level exists.

func AssertLogMessage

func AssertLogMessage(reporter TB, mock *MockLogger, level logger.Level, message string)

AssertLogMessage verifies that at least one log has the exact message.

func AssertNoLogs

func AssertNoLogs(reporter TB, mock *MockLogger)

AssertNoLogs verifies that no logs were captured.

func AssertNoLogsAtLevel

func AssertNoLogsAtLevel(reporter TB, mock *MockLogger, level logger.Level)

AssertNoLogsAtLevel verifies that no logs were captured at the given level.

Types

type Entry

type Entry struct {
	Fields  map[string]any
	Message string
	Args    []any
	Level   logger.Level
}

Entry represents a captured log entry.

type MockLogger

type MockLogger struct {
	// contains filtered or unexported fields
}

MockLogger captures log calls for testing. It implements logger.Logger and is safe for concurrent use.

func NewMockLogger

func NewMockLogger() *MockLogger

NewMockLogger creates a new MockLogger.

func (*MockLogger) Context

func (m *MockLogger) Context() context.Context

Context returns the context set via WithContext, or nil if not set.

func (*MockLogger) Debug

func (m *MockLogger) Debug(msg string, args ...any)

Debug captures a debug-level log entry.

func (*MockLogger) Enabled

func (m *MockLogger) Enabled(level logger.Level) bool

Enabled returns true if logging at the given level would produce output.

func (*MockLogger) Entries

func (m *MockLogger) Entries() []Entry

Entries returns a copy of all captured log entries.

func (*MockLogger) Error

func (m *MockLogger) Error(msg string, args ...any)

Error captures an error-level log entry.

func (*MockLogger) Fatal

func (m *MockLogger) Fatal(msg string, args ...any)

Fatal captures a fatal-level log entry.

func (*MockLogger) Info

func (m *MockLogger) Info(msg string, args ...any)

Info captures an info-level log entry.

func (*MockLogger) Len

func (m *MockLogger) Len() int

Len returns the number of captured entries.

func (*MockLogger) Reset

func (m *MockLogger) Reset()

Reset clears all captured entries.

func (*MockLogger) Warn

func (m *MockLogger) Warn(msg string, args ...any)

Warn captures a warn-level log entry.

func (*MockLogger) WithContext

func (m *MockLogger) WithContext(ctx context.Context) logger.Logger

WithContext returns a new MockLogger with the given context.

func (*MockLogger) WithFields

func (m *MockLogger) WithFields(fields ...any) logger.Logger

WithFields returns a new MockLogger with additional fields.

func (*MockLogger) WithLevel

func (m *MockLogger) WithLevel(level logger.Level) logger.Logger

WithLevel returns a new MockLogger with the given minimum level.

type TB

type TB interface {
	Helper()
	Errorf(format string, args ...any)
}

TB is the interface common to T and B for test helper functions. This allows assertions to be used in both tests and benchmarks, and enables testing of the assertions themselves.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL