testutil

package
v1.0.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2022 License: Apache-2.0 Imports: 10 Imported by: 11

Documentation

Overview

Package testutil is a minimal testing utility with only few functions like `Assert`, `Ok`, `NotOk` for errors and `Equals`. It also provides TB ("TestOrBench") utility for union of testing and benchmarks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert

func Assert(tb testing.TB, condition bool, v ...interface{})

Assert fails the test if the condition is false.

func ContainsStringSlice

func ContainsStringSlice(tb testing.TB, haystack, needle []string)

ContainsStringSlice fails the test if needle is not contained within haystack, if haystack or needle is an empty slice, or if needle is longer than haystack.

func Equals

func Equals(tb testing.TB, exp, act interface{}, v ...interface{})

Equals fails the test if exp is not equal to act.

func FaultOrPanicToErr

func FaultOrPanicToErr(f func()) (err error)

FaultOrPanicToErr returns error if panic of fault was triggered during execution of function.

func NotOk

func NotOk(tb testing.TB, err error, v ...interface{})

NotOk fails the test if an err is nil.

func Ok

func Ok(tb testing.TB, err error, v ...interface{})

Ok fails the test if an err is not nil.

func WithGoCmp

func WithGoCmp(opts ...cmp.Option) goCmp

WithGoCmp allows specifying options and using https://github.com/google/go-cmp for equality comparisons. The compatibility guarantee of this function's arguments are the same as go-cmp (no guarantee due to v0.x).

Types

type TB

type TB interface {
	testing.TB
	IsBenchmark() bool
	Run(name string, f func(t TB)) bool

	SetBytes(n int64)
	N() int
	ResetTimer()
}

TB represents union of test and benchmark. This allows the same test suite to be run by both benchmark and test, helping to reuse more code. The reason is that usually benchmarks are not being run on CI, especially for short tests, so you need to recreate usually similar tests for `Test<Name>(t *testing.T)` methods. Example of usage is presented here:

 func TestTestOrBench(t *testing.T) {
	tb := NewTB(t)
	tb.Run("1", func(tb TB) { testorbenchComplexTest(tb) })
	tb.Run("2", func(tb TB) { testorbenchComplexTest(tb) })
}

func BenchmarkTestOrBench(b *testing.B) {
	tb := NewTB(t)
	tb.Run("1", func(tb TB) { testorbenchComplexTest(tb) })
	tb.Run("2", func(tb TB) { testorbenchComplexTest(tb) })
}

func NewTB

func NewTB(t testing.TB) TB

NewTB creates tb from testing.TB.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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