test

package
v1.1.1002 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2025 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 added in v1.0.2003

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

func AssertErrorT

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

func AssertFalseT added in v1.0.2015

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

func AssertNoErrorT added in v1.0.5026

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

func AssertNotEmptyT added in v1.0.2022

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

func AssertNotEqualT added in v1.0.3009

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

func AssertTrueT added in v1.0.2023

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

func CallerInfo added in v1.0.5026

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 added in v1.0.5026

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

Condition uses a Comparison to assert a complex condition.

func Conditionf added in v1.0.5026

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

Conditionf uses a Comparison to assert a complex condition.

func Contains added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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

Equal asserts that two objects are equal.

func EqualError added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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

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

func ErrorAs added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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

Fail reports a failure through

func FailNow added in v1.0.5026

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

FailNow fails test

func FailNowf added in v1.0.5026

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

FailNowf fails test

func Failf added in v1.0.5026

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

Failf reports a failure through

func False added in v1.0.5026

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

False asserts that the specified value is false.

test.False(t, myBool)

func Falsef added in v1.0.5026

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 added in v1.0.5026

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

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

func IsTypef added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.4028

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

func Nil added in v1.0.5026

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

Nil asserts that the specified object is nil.

test.Nil(t, err)

func Nilf added in v1.0.5026

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 added in v1.0.5026

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

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

func NoErrorf added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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

NotNil asserts that the specified object is not nil.

NotNil(t, err)

func NotNilf added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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

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

func NotSamef added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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

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

func NotZerof added in v1.0.5026

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 added in v1.0.5026

func ObjectsAreEqual(expected, actual any) bool

ObjectsAreEqual determines if two objects are considered equal.

func ObjectsAreEqualValues added in v1.0.5026

func ObjectsAreEqualValues(expected, actual any) bool

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

func ObjectsExportedFieldsAreEqual added in v1.0.5026

func ObjectsExportedFieldsAreEqual(expected, actual any) bool

func Panics added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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

Same asserts that two pointers reference the same object.

func Samef added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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

True asserts that the specified value is true.

test.True(t, myBool)

func Truef added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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

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

func Zerof added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

type Comparison func() (success bool)

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

type ComparisonAssertionFunc added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

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 added in v1.0.5026

type PanicTestFunc func()

type TestingT added in v1.0.5026

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

TestingT is an interface wrapper around *testing.T

type ValueAssertionFunc added in v1.0.5026

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