test

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertEmptyT

func AssertEmptyT(t *testing.T) func(any, string)

func AssertEqualT

func AssertEqualT(t *testing.T) func(any, any, string)

func AssertErrorT

func AssertErrorT(t *testing.T) func(error, string)

func AssertFalseT

func AssertFalseT(t *testing.T) func(bool, string)

func AssertNoErrorT

func AssertNoErrorT(t *testing.T) func(error, string)

func AssertNotEmptyT

func AssertNotEmptyT(t *testing.T) func(any, string)

func AssertNotEqualT

func AssertNotEqualT(t *testing.T) func(any, any, string)

func AssertTrueT

func AssertTrueT(t *testing.T) func(bool, string)

func CallerInfo

func CallerInfo() []string

CallerInfo returns an array of strings containing the file and line number of each stack frame leading from the current test to the assert call that failed.

func Condition

func Condition(t TestingT, comp Comparison, msgAndArgs ...interface{}) bool

Condition uses a Comparison to assert a complex condition.

func Conditionf

func Conditionf(t TestingT, comp Comparison, msg string, args ...any) bool

Conditionf uses a Comparison to assert a complex condition.

func Contains

func Contains(t TestingT, s, contains any, msgAndArgs ...any) bool

Contains asserts that the specified string, list(array, slice...) or map contains the specified substring or element.

test.Contains(t, "Hello World", "World")
test.Contains(t, ["Hello", "World"], "World")
test.Contains(t, {"Hello": "World"}, "Hello")

func Containsf

func Containsf(t TestingT, s any, contains any, msg string, args ...any) bool

Containsf asserts that the specified string, list(array, slice...) or map contains the specified substring or element.

test.Containsf(t, "Hello World", "World", "error message %s", "formatted")
test.Containsf(t, ["Hello", "World"], "World", "error message %s", "formatted")
test.Containsf(t, {"Hello": "World"}, "Hello", "error message %s", "formatted")

func Empty

func Empty(t TestingT, object any, msgAndArgs ...any) bool

Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either a slice or a channel with len == 0.

test.Empty(t, obj)

func Emptyf

func Emptyf(t TestingT, object any, msg string, args ...any) bool

Emptyf asserts that the specified object is empty. I.e. nil, "", false, 0 or either a slice or a channel with len == 0.

test.Emptyf(t, obj, "error message %s", "formatted")

func Equal

func Equal(t TestingT, expected, actual any, msgAndArgs ...any) bool

Equal asserts that two objects are equal.

func EqualError

func EqualError(t TestingT, theError error, errString string, msgAndArgs ...any) bool

EqualError asserts that a function returned an error (i.e. not `nil`) and that it is equal to the provided error.

func EqualErrorf

func EqualErrorf(t TestingT, theError error, errString string, msg string, args ...any) bool

EqualErrorf asserts that a function returned an error (i.e. not `nil`) and that it is equal to the provided error.

actualObj, err := SomeFunction()
test.EqualErrorf(t, err,  expectedErrorString, "error message %s", "formatted")

func EqualExportedValues

func EqualExportedValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool

EqualExportedValues asserts that the types of two objects are equal and their public fields are also equal. This is useful for comparing structs that have private fields that could potentially differ.

 type S struct {
	Exported     	int
	notExported   	int
 }
 test.EqualExportedValues(t, S{1, 2}, S{1, 3}) => true
 test.EqualExportedValues(t, S{1, 2}, S{2, 3}) => false

func EqualExportedValuesf

func EqualExportedValuesf(t TestingT, expected any, actual any, msg string, args ...any) bool

EqualExportedValuesf asserts that the types of two objects are equal and their public fields are also equal. This is useful for comparing structs that have private fields that could potentially differ.

 type S struct {
	Exported     	int
	notExported   	int
 }
 test.EqualExportedValuesf(t, S{1, 2}, S{1, 3}, "error message %s", "formatted") => true
 test.EqualExportedValuesf(t, S{1, 2}, S{2, 3}, "error message %s", "formatted") => false

func EqualValues

func EqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool

EqualValues asserts that two objects are equal or convertible to the larger type and equal.

test.EqualValues(t, uint32(123), int32(123))

func EqualValuesf

func EqualValuesf(t TestingT, expected any, actual any, msg string, args ...any) bool

EqualValuesf asserts that two objects are equal or convertible to the larger type and equal.

test.EqualValuesf(t, uint32(123), int32(123), "error message %s", "formatted")

func Equalf

func Equalf(t TestingT, expected any, actual any, msg string, args ...any) bool

Equalf asserts that two objects are equal.

test.Equalf(t, 123, 123, "error message %s", "formatted")

Pointer variable equality is determined based on the equality of the referenced values (as opposed to the memory addresses). Function equality cannot be determined and will always fail.

func Error

func Error(t TestingT, err error, msgAndArgs ...any) bool

Error asserts that a function returned an error (i.e. not `nil`).

func ErrorAs

func ErrorAs(t TestingT, err error, target interface{}, msgAndArgs ...interface{}) bool

ErrorAs asserts that at least one of the errors in err's chain matches target, and if so, sets target to that error value. This is a wrapper for errors.As.

func ErrorAsf

func ErrorAsf(t TestingT, err error, target any, msg string, args ...any) bool

ErrorAsf asserts that at least one of the errors in err's chain matches target, and if so, sets target to that error value. This is a wrapper for errors.As.

func ErrorContains

func ErrorContains(t TestingT, theError error, contains string, msgAndArgs ...any) bool

ErrorContains asserts that a function returned an error (i.e. not `nil`) and that the error contains the specified substring.

func ErrorContainsf

func ErrorContainsf(t TestingT, theError error, contains string, msg string, args ...any) bool

ErrorContainsf asserts that a function returned an error (i.e. not `nil`) and that the error contains the specified substring.

actualObj, err := SomeFunction()
test.ErrorContainsf(t, err,  expectedErrorSubString, "error message %s", "formatted")

func ErrorIs

func ErrorIs(t TestingT, err, target error, msgAndArgs ...interface{}) bool

ErrorIs asserts that at least one of the errors in err's chain matches target. This is a wrapper for errors.Is.

func ErrorIsf

func ErrorIsf(t TestingT, err error, target error, msg string, args ...any) bool

ErrorIsf asserts that at least one of the errors in err's chain matches target. This is a wrapper for errors.Is.

func Errorf

func Errorf(t TestingT, err error, msg string, args ...any) bool

Errorf asserts that a function returned an error (i.e. not `nil`).

  actualObj, err := SomeFunction()
  if test.Errorf(t, err, "error message %s", "formatted") {
	   test.Equal(t, expectedErrorf, err)
  }

func Exactly

func Exactly(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool

Exactly asserts that two objects are equal in value and type.

test.Exactly(t, int32(123), int64(123))

func Exactlyf

func Exactlyf(t TestingT, expected any, actual any, msg string, args ...any) bool

Exactlyf asserts that two objects are equal in value and type.

test.Exactlyf(t, int32(123), int64(123), "error message %s", "formatted")

func Fail

func Fail(t TestingT, failureMessage string, msgAndArgs ...any) bool

Fail reports a failure through

func FailNow

func FailNow(t TestingT, failureMessage string, msgAndArgs ...any) bool

FailNow fails test

func FailNowf

func FailNowf(t TestingT, failureMessage string, msg string, args ...any) bool

FailNowf fails test

func Failf

func Failf(t TestingT, failureMessage string, msg string, args ...any) bool

Failf reports a failure through

func False

func False(t TestingT, value bool, msgAndArgs ...any) bool

False asserts that the specified value is false.

test.False(t, myBool)

func Falsef

func Falsef(t TestingT, value bool, msg string, args ...any) bool

Falsef asserts that the specified value is false.

test.Falsef(t, myBool, "error message %s", "formatted")

func IsType

func IsType(t TestingT, expectedType any, object any, msgAndArgs ...any) bool

IsType asserts that the specified objects are of the same type.

func IsTypef

func IsTypef(t TestingT, expectedType any, object any, msg string, args ...any) bool

IsTypef asserts that the specified objects are of the same type.

func Len

func Len(t TestingT, object interface{}, length int, msgAndArgs ...interface{}) bool

Len asserts that the specified object has specific length. Len also fails if the object has a type that len() not accept.

test.Len(t, mySlice, 3)

func MustPanic

func MustPanic(t *testing.T, msg string, f func())

func Nil

func Nil(t TestingT, object any, msgAndArgs ...any) bool

Nil asserts that the specified object is nil.

test.Nil(t, err)

func Nilf

func Nilf(t TestingT, object any, msg string, args ...any) bool

Nilf asserts that the specified object is nil.

test.Nilf(t, err, "error message %s", "formatted")

func NoError

func NoError(t TestingT, err error, msgAndArgs ...any) bool

NoError asserts that a function returned no error (i.e. `nil`).

func NoErrorf

func NoErrorf(t TestingT, err error, msg string, args ...any) bool

NoErrorf asserts that a function returned no error (i.e. `nil`).

  actualObj, err := SomeFunction()
  if test.NoErrorf(t, err, "error message %s", "formatted") {
	   test.Equal(t, expectedObj, actualObj)
  }

func NotContains

func NotContains(t TestingT, s, contains any, msgAndArgs ...any) bool

NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the specified substring or element.

test.NotContains(t, "Hello World", "Earth")
test.NotContains(t, ["Hello", "World"], "Earth")
test.NotContains(t, {"Hello": "World"}, "Earth")

func NotContainsf

func NotContainsf(t TestingT, s any, contains any, msg string, args ...any) bool

NotContainsf asserts that the specified string, list(array, slice...) or map does NOT contain the specified substring or element.

test.NotContainsf(t, "Hello World", "Earth", "error message %s", "formatted")
test.NotContainsf(t, ["Hello", "World"], "Earth", "error message %s", "formatted")
test.NotContainsf(t, {"Hello": "World"}, "Earth", "error message %s", "formatted")

func NotEmpty

func NotEmpty(t TestingT, object any, msgAndArgs ...any) bool

NotEmpty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either a slice or a channel with len == 0.

if test.NotEmpty(t, obj) {
  test.Equal(t, "two", obj[1])
}

func NotEmptyf

func NotEmptyf(t TestingT, object any, msg string, args ...any) bool

NotEmptyf asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either a slice or a channel with len == 0.

if test.NotEmptyf(t, obj, "error message %s", "formatted") {
  test.Equal(t, "two", obj[1])
}

func NotEqual

func NotEqual(t TestingT, expected, actual any, msgAndArgs ...any) bool

NotEqual asserts that the specified values are NOT equal.

test.NotEqual(t, obj1, obj2)

func NotEqualValues

func NotEqualValues(t TestingT, expected, actual any, msgAndArgs ...interface{}) bool

NotEqualValues asserts that two objects are not equal even when converted to the same type

test.NotEqualValues(t, obj1, obj2)

func NotEqualValuesf

func NotEqualValuesf(t TestingT, expected any, actual any, msg string, args ...any) bool

NotEqualValuesf asserts that two objects are not equal even when converted to the same type

test.NotEqualValuesf(t, obj1, obj2, "error message %s", "formatted")

func NotEqualf

func NotEqualf(t TestingT, expected any, actual any, msg string, args ...any) bool

NotEqualf asserts that the specified values are NOT equal.

test.NotEqualf(t, obj1, obj2, "error message %s", "formatted")

Pointer variable equality is determined based on the equality of the referenced values (as opposed to the memory addresses).

func NotErrorAs

func NotErrorAs(t TestingT, err error, target interface{}, msgAndArgs ...interface{}) bool

NotErrorAs asserts that none of the errors in err's chain matches target, but if so, sets target to that error value.

func NotErrorAsf

func NotErrorAsf(t TestingT, err error, target any, msg string, args ...any) bool

NotErrorAsf asserts that none of the errors in err's chain matches target, but if so, sets target to that error value.

func NotErrorIs

func NotErrorIs(t TestingT, err, target error, msgAndArgs ...interface{}) bool

NotErrorIs asserts that none of the errors in err's chain matches target. This is a wrapper for errors.Is.

func NotErrorIsf

func NotErrorIsf(t TestingT, err error, target error, msg string, args ...any) bool

NotErrorIsf asserts that none of the errors in err's chain matches target. This is a wrapper for errors.Is.

func NotNil

func NotNil(t TestingT, object any, msgAndArgs ...any) bool

NotNil asserts that the specified object is not nil.

NotNil(t, err)

func NotNilf

func NotNilf(t TestingT, object any, msg string, args ...any) bool

NotNilf asserts that the specified object is not nil.

test.NotNilf(t, err, "error message %s", "formatted")

func NotPanics

func NotPanics(t TestingT, f PanicTestFunc, msgAndArgs ...any) bool

NotPanics asserts that the code inside the specified PanicTestFunc does NOT panic.

test.NotPanics(t, func(){ RemainCalm() })

func NotPanicsf

func NotPanicsf(t TestingT, f PanicTestFunc, msg string, args ...any) bool

NotPanicsf asserts that the code inside the specified PanicTestFunc does NOT panic.

test.NotPanicsf(t, func(){ RemainCalm() }, "error message %s", "formatted")

func NotSame

func NotSame(t TestingT, expected, actual any, msgAndArgs ...any) bool

NotSame asserts that two pointers do not reference the same object.

func NotSamef

func NotSamef(t TestingT, expected any, actual any, msg string, args ...any) bool

NotSamef asserts that two pointers do not reference the same object.

test.NotSamef(t, ptr1, ptr2, "error message %s", "formatted")

Both arguments must be pointer variables. Pointer variable sameness is determined based on the equality of both type and value.

func NotSubset

func NotSubset(t TestingT, list, subset any, msgAndArgs ...any) (ok bool)

NotSubset asserts that the specified list(array, slice...) or map does NOT contain all elements given in the specified subset list(array, slice...) or map.

test.NotSubset(t, [1, 3, 4], [1, 2])
test.NotSubset(t, {"x": 1, "y": 2}, {"z": 3})

func NotSubsetf

func NotSubsetf(t TestingT, list any, subset any, msg string, args ...any) bool

NotSubsetf asserts that the specified list(array, slice...) or map does NOT contain all elements given in the specified subset list(array, slice...) or map.

test.NotSubsetf(t, [1, 3, 4], [1, 2], "error message %s", "formatted")
test.NotSubsetf(t, {"x": 1, "y": 2}, {"z": 3}, "error message %s", "formatted")

func NotZero

func NotZero(t TestingT, i any, msgAndArgs ...any) bool

NotZero asserts that i is not the zero value for its type.

func NotZerof

func NotZerof(t TestingT, i any, msg string, args ...any) bool

NotZerof asserts that i is not the zero value for its type.

func ObjectsAreEqual

func ObjectsAreEqual(expected, actual any) bool

ObjectsAreEqual determines if two objects are considered equal.

func ObjectsAreEqualValues

func ObjectsAreEqualValues(expected, actual any) bool

ObjectsAreEqualValues gets whether two objects are equal, or if their values are equal.

func ObjectsExportedFieldsAreEqual

func ObjectsExportedFieldsAreEqual(expected, actual any) bool

func Panics

func Panics(t TestingT, f PanicTestFunc, msgAndArgs ...any) bool

Panics asserts that the code inside the specified PanicTestFunc panics.

test.Panics(t, func(){ GoCrazy() })

func PanicsWithError

func PanicsWithError(t TestingT, errString string, f PanicTestFunc, msgAndArgs ...any) bool

PanicsWithError asserts that the code inside the specified PanicTestFunc panics, and that the recovered panic value is an error that satisfies the EqualError comparison.

test.PanicsWithError(t, "crazy error", func(){ GoCrazy() })

func PanicsWithErrorf

func PanicsWithErrorf(t TestingT, errString string, f PanicTestFunc, msg string, args ...any) bool

PanicsWithErrorf asserts that the code inside the specified PanicTestFunc panics, and that the recovered panic value is an error that satisfies the EqualError comparison.

test.PanicsWithErrorf(t, "crazy error", func(){ GoCrazy() }, "error message %s", "formatted")

func PanicsWithValue

func PanicsWithValue(t TestingT, expected any, f PanicTestFunc, msgAndArgs ...any) bool

PanicsWithValue asserts that the code inside the specified PanicTestFunc panics, and that the recovered panic value equals the expected panic value.

test.PanicsWithValue(t, "crazy error", func(){ GoCrazy() })

func PanicsWithValuef

func PanicsWithValuef(t TestingT, expected any, f PanicTestFunc, msg string, args ...any) bool

PanicsWithValuef asserts that the code inside the specified PanicTestFunc panics, and that the recovered panic value equals the expected panic value.

test.PanicsWithValuef(t, "crazy error", func(){ GoCrazy() }, "error message %s", "formatted")

func Panicsf

func Panicsf(t TestingT, f PanicTestFunc, msg string, args ...any) bool

Panicsf asserts that the code inside the specified PanicTestFunc panics.

test.Panicsf(t, func(){ GoCrazy() }, "error message %s", "formatted")

func Same

func Same(t TestingT, expected, actual any, msgAndArgs ...any) bool

Same asserts that two pointers reference the same object.

func Samef

func Samef(t TestingT, expected any, actual any, msg string, args ...any) bool

Samef asserts that two pointers reference the same object.

test.Samef(t, ptr1, ptr2, "error message %s", "formatted")

Both arguments must be pointer variables. Pointer variable sameness is determined based on the equality of both type and value.

func Subset

func Subset(t TestingT, list, subset any, msgAndArgs ...any) (ok bool)

Subset asserts that the specified list(array, slice...) or map contains all elements given in the specified subset list(array, slice...) or map.

test.Subset(t, [1, 2, 3], [1, 2])
test.Subset(t, {"x": 1, "y": 2}, {"x": 1})

func Subsetf

func Subsetf(t TestingT, list any, subset any, msg string, args ...any) bool

Subsetf asserts that the specified list(array, slice...) or map contains all elements given in the specified subset list(array, slice...) or map.

test.Subsetf(t, [1, 2, 3], [1, 2], "error message %s", "formatted")
test.Subsetf(t, {"x": 1, "y": 2}, {"x": 1}, "error message %s", "formatted")

func True

func True(t TestingT, value bool, msgAndArgs ...any) bool

True asserts that the specified value is true.

test.True(t, myBool)

func Truef

func Truef(t TestingT, value bool, msg string, args ...any) bool

Truef asserts that the specified value is true.

test.Truef(t, myBool, "error message %s", "formatted")

func WithinDuration

func WithinDuration(t TestingT, expected, actual time.Time, delta time.Duration, msgAndArgs ...interface{}) bool

WithinDuration asserts that the two times are within duration delta of each other.

test.WithinDuration(t, time.Now(), time.Now(), 10*time.Second)

func WithinDurationf

func WithinDurationf(t TestingT, expected time.Time, actual time.Time, delta time.Duration, msg string, args ...any) bool

WithinDurationf asserts that the two times are within duration delta of each other.

test.WithinDurationf(t, time.Now(), time.Now(), 10*time.Second, "error message %s", "formatted")

func WithinRange

func WithinRange(t TestingT, actual, start, end time.Time, msgAndArgs ...interface{}) bool

WithinRange asserts that a time is within a time range (inclusive).

test.WithinRange(t, time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second))

func WithinRangef

func WithinRangef(t TestingT, actual time.Time, start time.Time, end time.Time, msg string, args ...any) bool

WithinRangef asserts that a time is within a time range (inclusive).

test.WithinRangef(t, time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second), "error message %s", "formatted")

func Zero

func Zero(t TestingT, i any, msgAndArgs ...any) bool

Zero asserts that i is the zero value for its type.

func Zerof

func Zerof(t TestingT, i any, msg string, args ...any) bool

Zerof asserts that i is the zero value for its type.

Types

type BoolAssertionFunc

type BoolAssertionFunc func(TestingT, bool, ...any) bool

BoolAssertionFunc is a common function prototype when validating a bool value. Can be useful for table driven tests.

type Comparison

type Comparison func() (success bool)

Comparison is a custom function that returns true on success and false on failure

type ComparisonAssertionFunc

type ComparisonAssertionFunc func(TestingT, any, any, ...any) bool

ComparisonAssertionFunc is a common function prototype when comparing two values. Can be useful for table driven tests.

type ErrorAssertionFunc

type ErrorAssertionFunc func(TestingT, error, ...any) bool

ErrorAssertionFunc is a common function prototype when validating an error value. Can be useful for table driven tests.

type PanicAssertionFunc

type PanicAssertionFunc = func(t TestingT, f PanicTestFunc, msgAndArgs ...any) bool

PanicAssertionFunc is a common function prototype when validating a panic value. Can be useful for table driven tests.

type PanicTestFunc

type PanicTestFunc func()

type TestingT

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

TestingT is an interface wrapper around *testing.T

type ValueAssertionFunc

type ValueAssertionFunc func(TestingT, any, ...any) bool

ValueAssertionFunc is a common function prototype when validating a single value. Can be useful for table driven tests.

Jump to

Keyboard shortcuts

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