zaptest

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2023 License: MIT Imports: 4 Imported by: 4

README

Zaptest ⚡👩‍🔧

Test helpers to use a github.com/uber-go/zap logger in unit tests.


Package zaptest implements test helpers that facilitate using a zap.Logger in standard go unit tests.

This package is useful when running unit tests with components that use the https://github.com/uber-go/zap logging framework. In unit tests we usually want to suppress any logging output as long as the tests are not failing or they are started in a verbose mode.

Installation

$ go get github.com/fgrosse/zaptest

Usage with standard unit tests

You can use zaptest in standard unit tests. All log output will be sent via the testing.T so it will only be shown if the test fails or if the -v flag was set.

Note that you can also use zaptest in benchmarks because testing.B also implements the necessary logger interface.

func TestLogger(t *testing.T) {
	l := zaptest.Logger(t)
	l.Debug("This is a debug message, debug messages will be logged as well")
	l.Info("Logs will not be shown during normal test execution")
	l.Warn("You can see all log messages of a successful run by passing the -v flag")
	l.Error("Additionally the entire log output for a specific unit test will be visible when a test fails")
}

Usage with ginkgo

Package zaptest is also compatible with the https://github.com/onsi/ginkgo BDD testing framework. As with the standard unit tests, any log output for a specific test will only be printed if that test fails or if the test is running in verbose mode. With ginkgo use ginkgo -v to enable verbose output (go test -v does not seem to work).

// TestedType is an example of some type you want to test.
// Note that the TestedType uses a zap logger.
type TestedType struct {
	log *zap.Logger
}

// DoStuff is an example function of the TestedType which uses a logger.
func (tt *TestedType) DoStuff() error {
	tt.log.Debug("Doing stuff")
	return nil
}

// TestLoggerWriter shows how to use the LoggerWriter function for ginkgo tests.
// Run `ginkgo -v`to see the output of this test.
func TestLoggerWriter(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Ginkgo Example Suite")
}

// Describe the TestedType using ginkgo/gomega.
var _ = Describe("TestedType", func() {
	It("should do stuff", func() {
		tt := &TestedType{log: zaptest.LoggerWriter(GinkgoWriter)}
		Expect(tt.DoStuff()).To(Succeed())
	})
})

Usage as library

You can also use zaptest as library since it does not import the testing package.

Documentation

Overview

Package zaptest implements test helpers that facilitate using a zap.Logger in unit tests.

This package is useful when running unit tests with components that use the github.com/uber-go/zap logging framework. In unit tests we usually want to suppress any logging output as long as the tests are not failing or they are started in a verbose mode.

Package zaptest is also compatible with the github.com/onsi/ginkgo BDD testing framework. Have a look at the zaptest unit tests to see an usage examples.

Index

Constants

This section is empty.

Variables

View Source
var Config = func() zapcore.EncoderConfig {
	conf := zap.NewDevelopmentEncoderConfig()

	conf.TimeKey = ""

	return conf
}

Config is the default function used create the configuration for the zap logger. You can change this if you want to change the logger encoding options.

By default the zap development config will be used and timestamps will be omitted (just like normal t.Log does).

View Source
var Level = zap.DebugLevel

Level is the zap log level that will be printed in unit tests.

Functions

func Logger

func Logger(t logger) *zap.Logger

Logger creates a new zap.Logger that writes all messages via t.Log(…). Note that both testing.T and testing.B implement the logger interface.

func LoggerWriter

func LoggerWriter(w io.Writer) *zap.Logger

LoggerWriter creates a new zap.Logger that writes all messages to the given io.Writer.

Types

This section is empty.

Jump to

Keyboard shortcuts

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