testlogrus

package
v0.0.0-...-6801039 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

The testlogrus package exposes two functions to use during testing.

When calling `Logs` function, the logs are reset, as such if multiple verification are to be done, you should first affect `Logs` result to a variable.

func TestSome(t *testing.T) {
	t.Run("error_test", func(t *testing.T) {
		// Arrange
		// mandatory call to setup logrus logging hook
		// logging hook is set to nil at the end of t.Run
		testlogrus.CatchLogs(t)

		// Act
		// call some functions that log with logrus

		// Assert
		logs := testlogrus.Logs()
		// assert some things around function expected logs
	})
}

Index

Constants

This section is empty.

Variables

View Source
var ErrNoHook = errors.New("testlogrus hook wasn't initialized with CatchLogs")

ErrNoHook is the panic error in case Logs is called without a previous call to CatchLogs.

Functions

func CatchLogs

func CatchLogs(t testing.TB)

CatchLogs creates a hook over logrus logs. Easy to use with Logs function to catch written logs in tests.

Useful to confirm logging behavior over application.

Note that at the end of t.Run a cleanup is called to reset the hook. As such CatchLogs must either be called at the beginning of every t.Run needing a log verification or at the beginning of every test function (TestXXX) needing logs verification.

func Logs

func Logs() string

Logs resets the logrus test logs hooks and returns all catched logs from when CatchLogs was executed.

Types

This section is empty.

Jump to

Keyboard shortcuts

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