Search and commands

↑ ↓ to select · Enter to open · Esc to close

Type to search packages.

zaptest

v1.28.0
go.uber.org/zap/zaptest

Documentation

pkg.go.dev ↗

Table of contents

↓ to select · Enter to jump· Shift+Enter for source

19 matches

Top ↑

Package zaptest provides a variety of helpers for testing log output.

Functions

NewLogger

func NewLogger(t TestingT, opts ...LoggerOption) *zap.Logger

NewLogger builds a new Logger that logs all messages to the given testing.TB.

logger := zaptest.NewLogger(t)

Use this with a *testing.T or *testing.B to get logs which get printed only if a test fails or if you ran go test -v.

The returned logger defaults to logging debug level messages and above. This may be changed by passing a zaptest.Level during construction.

logger := zaptest.NewLogger(t, zaptest.Level(zap.WarnLevel))

You may also pass zap.Option's to customize test logger.

logger := zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller()))

Sleep

func Sleep(base time.Duration)

Sleep scales the sleep duration by $TEST_TIMEOUT_SCALE.

Deprecated: This function is intended for internal testing and shouldn't be used outside zap itself. It was introduced before Go supported internal packages.

Timeout

func Timeout(base time.Duration) time.Duration

Timeout scales the provided duration by $TEST_TIMEOUT_SCALE.

Deprecated: This function is intended for internal testing and shouldn't be used outside zap itself. It was introduced before Go supported internal packages.

Types

Buffer

type Buffer = ztest.Buffer

Buffer is an implementation of zapcore.WriteSyncer that sends all writes to a bytes.Buffer. It has convenience methods to split the accumulated buffer on newlines.

Discarder

type Discarder = ztest.Discarder

A Discarder sends all writes to io.Discard.

FailWriter

type FailWriter = ztest.FailWriter

FailWriter is a WriteSyncer that always returns an error on writes.

LoggerOption

type LoggerOption interface {
	// contains filtered or unexported methods
}

LoggerOption configures the test logger built by NewLogger.

Level

func Level(enab zapcore.LevelEnabler) LoggerOption

Level controls which messages are logged by a test Logger built by NewLogger.

WrapOptions

func WrapOptions(zapOpts ...zap.Option) LoggerOption

WrapOptions adds zap.Option's to a test Logger built by NewLogger.

ShortWriter

type ShortWriter = ztest.ShortWriter

ShortWriter is a WriteSyncer whose write method never returns an error, but always reports that it wrote one byte less than the input slice's length (thus, a "short write").

Syncer

type Syncer = ztest.Syncer

A Syncer is a spy for the Sync portion of zapcore.WriteSyncer.

TestingT

type TestingT interface {
	// Logs the given message without failing the test.
	Logf(string, ...interface{})

	// Logs the given message and marks the test as failed.
	Errorf(string, ...interface{})

	// Marks the test as failed.
	Fail()

	// Returns true if the test has been marked as failed.
	Failed() bool

	// Returns the name of the test.
	Name() string

	// Marks the test as failed and stops execution of that test.
	FailNow()
}

TestingT is a subset of the API provided by all *testing.T and *testing.B objects.

TestingWriter

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

TestingWriter is a WriteSyncer that writes to the given testing.TB.

NewTestingWriter

func NewTestingWriter(t TestingT) TestingWriter

NewTestingWriter builds a new TestingWriter that writes to the given testing.TB.

Use this if you need more flexibility when creating *zap.Logger than zaptest.NewLogger() provides.

E.g., if you want to use custom core with zaptest.TestingWriter:

encoder := newCustomEncoder()
writer := zaptest.NewTestingWriter(t)
level := zap.NewAtomicLevelAt(zapcore.DebugLevel)

core := newCustomCore(encoder, writer, level)

logger := zap.New(core, zap.AddCaller())

TestingWriter.Sync

func (w TestingWriter) Sync() error

Sync commits the current contents (a no-op for TestingWriter).

TestingWriter.WithMarkFailed

func (w TestingWriter) WithMarkFailed(v bool) TestingWriter

WithMarkFailed returns a copy of this TestingWriter with markFailed set to the provided value.

TestingWriter.Write

func (w TestingWriter) Write(p []byte) (n int, err error)

Write writes bytes from p to the underlying testing.TB.

Dependency graph