Documentation
¶
Overview ¶
Package assert provides a set of testing tools for use with the standard Go testing system.
Note ¶
All functions in this package return a bool value indicating whether the assertion has passed.
Example usage ¶
The following is a complete example using assert in a standard test function:
import (
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func TestSomething(t *testing.T) {
var a string = "Hello"
var b string = "Hello"
assert.Equal(t, a, b, "The two words should be the same.")
}
if you assert many times, you might find the format below more concise:
import (
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func TestSomething(t *testing.T) {
assert := assert.New(t)
var a string = "Hello"
var b string = "Hello"
assert.Equal(a, b, "The two words should be the same.")
}
Assertions ¶
Assertions allow you to easily write test code using go's standard testing library.
All assertion functions take as the first argument, the *testing.T object provided by the standard testing framework.
This allows the assertion functions to write their failings and other details to the correct place.
Every assertion function takes an optional string message as the final argument, allowing custom error messages to be appended to the message the assertion method outputs.
See our doc site at https://go-openapi.github.io/testify/ for usage, examples and searchable reference.
See https://pkg.go/dev/go-openapi/testify/ for complete reference.
Index ¶
- Constants
- func CallerInfo() []string
- func Condition(t T, comp func() bool, msgAndArgs ...any) bool
- func Conditionf(t T, comp func() bool, msg string, args ...any) bool
- func Consistently[C Conditioner](t T, condition C, timeout time.Duration, tick time.Duration, msgAndArgs ...any) bool
- func Consistentlyf[C Conditioner](t T, condition C, timeout time.Duration, tick time.Duration, msg string, ...) bool
- func Contains(t T, s any, contains any, msgAndArgs ...any) bool
- func Containsf(t T, s any, contains any, msg string, args ...any) bool
- func DirExists(t T, path string, msgAndArgs ...any) bool
- func DirExistsf(t T, path string, msg string, args ...any) bool
- func DirNotExists(t T, path string, msgAndArgs ...any) bool
- func DirNotExistsf(t T, path string, msg string, args ...any) bool
- func ElementsMatch(t T, listA any, listB any, msgAndArgs ...any) (ok bool)
- func ElementsMatchT[E comparable](t T, listA []E, listB []E, msgAndArgs ...any) bool
- func ElementsMatchTf[E comparable](t T, listA []E, listB []E, msg string, args ...any) bool
- func ElementsMatchf(t T, listA any, listB any, msg string, args ...any) (ok bool)
- func Empty(t T, object any, msgAndArgs ...any) bool
- func Emptyf(t T, object any, msg string, args ...any) bool
- func Equal(t T, expected any, actual any, msgAndArgs ...any) bool
- func EqualError(t T, err error, errString string, msgAndArgs ...any) bool
- func EqualErrorf(t T, err error, errString string, msg string, args ...any) bool
- func EqualExportedValues(t T, expected any, actual any, msgAndArgs ...any) bool
- func EqualExportedValuesf(t T, expected any, actual any, msg string, args ...any) bool
- func EqualT[V comparable](t T, expected V, actual V, msgAndArgs ...any) bool
- func EqualTf[V comparable](t T, expected V, actual V, msg string, args ...any) bool
- func EqualValues(t T, expected any, actual any, msgAndArgs ...any) bool
- func EqualValuesf(t T, expected any, actual any, msg string, args ...any) bool
- func Equalf(t T, expected any, actual any, msg string, args ...any) bool
- func Error(t T, err error, msgAndArgs ...any) bool
- func ErrorAs(t T, err error, target any, msgAndArgs ...any) bool
- func ErrorAsf(t T, err error, target any, msg string, args ...any) bool
- func ErrorContains(t T, err error, contains string, msgAndArgs ...any) bool
- func ErrorContainsf(t T, err error, contains string, msg string, args ...any) bool
- func ErrorIs(t T, err error, target error, msgAndArgs ...any) bool
- func ErrorIsf(t T, err error, target error, msg string, args ...any) bool
- func Errorf(t T, err error, msg string, args ...any) bool
- func Eventually[C Conditioner](t T, condition C, timeout time.Duration, tick time.Duration, msgAndArgs ...any) bool
- func EventuallyWith[C CollectibleConditioner](t T, condition C, timeout time.Duration, tick time.Duration, msgAndArgs ...any) bool
- func EventuallyWithf[C CollectibleConditioner](t T, condition C, timeout time.Duration, tick time.Duration, msg string, ...) bool
- func Eventuallyf[C Conditioner](t T, condition C, timeout time.Duration, tick time.Duration, msg string, ...) bool
- func Exactly(t T, expected any, actual any, msgAndArgs ...any) bool
- func Exactlyf(t T, expected any, actual any, msg string, args ...any) bool
- func Fail(t T, failureMessage string, msgAndArgs ...any) bool
- func FailNow(t T, failureMessage string, msgAndArgs ...any) bool
- func FailNowf(t T, failureMessage string, msg string, args ...any) bool
- func Failf(t T, failureMessage string, msg string, args ...any) bool
- func False(t T, value bool, msgAndArgs ...any) bool
- func FalseT[B Boolean](t T, value B, msgAndArgs ...any) bool
- func FalseTf[B Boolean](t T, value B, msg string, args ...any) bool
- func Falsef(t T, value bool, msg string, args ...any) bool
- func FileEmpty(t T, path string, msgAndArgs ...any) bool
- func FileEmptyf(t T, path string, msg string, args ...any) bool
- func FileExists(t T, path string, msgAndArgs ...any) bool
- func FileExistsf(t T, path string, msg string, args ...any) bool
- func FileNotEmpty(t T, path string, msgAndArgs ...any) bool
- func FileNotEmptyf(t T, path string, msg string, args ...any) bool
- func FileNotExists(t T, path string, msgAndArgs ...any) bool
- func FileNotExistsf(t T, path string, msg string, args ...any) bool
- func Greater(t T, e1 any, e2 any, msgAndArgs ...any) bool
- func GreaterOrEqual(t T, e1 any, e2 any, msgAndArgs ...any) bool
- func GreaterOrEqualT[Orderable Ordered](t T, e1 Orderable, e2 Orderable, msgAndArgs ...any) bool
- func GreaterOrEqualTf[Orderable Ordered](t T, e1 Orderable, e2 Orderable, msg string, args ...any) bool
- func GreaterOrEqualf(t T, e1 any, e2 any, msg string, args ...any) bool
- func GreaterT[Orderable Ordered](t T, e1 Orderable, e2 Orderable, msgAndArgs ...any) bool
- func GreaterTf[Orderable Ordered](t T, e1 Orderable, e2 Orderable, msg string, args ...any) bool
- func Greaterf(t T, e1 any, e2 any, msg string, args ...any) bool
- func HTTPBody(handler http.HandlerFunc, method string, url string, values url.Values) string
- func HTTPBodyContains(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func HTTPBodyContainsf(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func HTTPBodyNotContains(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func HTTPBodyNotContainsf(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func HTTPError(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func HTTPErrorf(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func HTTPRedirect(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func HTTPRedirectf(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func HTTPStatusCode(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func HTTPStatusCodef(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func HTTPSuccess(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func HTTPSuccessf(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func Implements(t T, interfaceObject any, object any, msgAndArgs ...any) bool
- func Implementsf(t T, interfaceObject any, object any, msg string, args ...any) bool
- func InDelta(t T, expected any, actual any, delta float64, msgAndArgs ...any) bool
- func InDeltaMapValues(t T, expected any, actual any, delta float64, msgAndArgs ...any) bool
- func InDeltaMapValuesf(t T, expected any, actual any, delta float64, msg string, args ...any) bool
- func InDeltaSlice(t T, expected any, actual any, delta float64, msgAndArgs ...any) bool
- func InDeltaSlicef(t T, expected any, actual any, delta float64, msg string, args ...any) bool
- func InDeltaT[Number Measurable](t T, expected Number, actual Number, delta Number, msgAndArgs ...any) bool
- func InDeltaTf[Number Measurable](t T, expected Number, actual Number, delta Number, msg string, args ...any) bool
- func InDeltaf(t T, expected any, actual any, delta float64, msg string, args ...any) bool
- func InEpsilon(t T, expected any, actual any, epsilon float64, msgAndArgs ...any) bool
- func InEpsilonSlice(t T, expected any, actual any, epsilon float64, msgAndArgs ...any) bool
- func InEpsilonSlicef(t T, expected any, actual any, epsilon float64, msg string, args ...any) bool
- func InEpsilonT[Number Measurable](t T, expected Number, actual Number, epsilon float64, msgAndArgs ...any) bool
- func InEpsilonTf[Number Measurable](t T, expected Number, actual Number, epsilon float64, msg string, args ...any) bool
- func InEpsilonf(t T, expected any, actual any, epsilon float64, msg string, args ...any) bool
- func IsDecreasing(t T, collection any, msgAndArgs ...any) bool
- func IsDecreasingT[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msgAndArgs ...any) bool
- func IsDecreasingTf[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msg string, args ...any) bool
- func IsDecreasingf(t T, collection any, msg string, args ...any) bool
- func IsIncreasing(t T, collection any, msgAndArgs ...any) bool
- func IsIncreasingT[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msgAndArgs ...any) bool
- func IsIncreasingTf[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msg string, args ...any) bool
- func IsIncreasingf(t T, collection any, msg string, args ...any) bool
- func IsNonDecreasing(t T, collection any, msgAndArgs ...any) bool
- func IsNonDecreasingT[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msgAndArgs ...any) bool
- func IsNonDecreasingTf[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msg string, args ...any) bool
- func IsNonDecreasingf(t T, collection any, msg string, args ...any) bool
- func IsNonIncreasing(t T, collection any, msgAndArgs ...any) bool
- func IsNonIncreasingT[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msgAndArgs ...any) bool
- func IsNonIncreasingTf[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msg string, args ...any) bool
- func IsNonIncreasingf(t T, collection any, msg string, args ...any) bool
- func IsNotOfTypeT[EType any](t T, object any, msgAndArgs ...any) bool
- func IsNotOfTypeTf[EType any](t T, object any, msg string, args ...any) bool
- func IsNotType(t T, theType any, object any, msgAndArgs ...any) bool
- func IsNotTypef(t T, theType any, object any, msg string, args ...any) bool
- func IsOfTypeT[EType any](t T, object any, msgAndArgs ...any) bool
- func IsOfTypeTf[EType any](t T, object any, msg string, args ...any) bool
- func IsType(t T, expectedType any, object any, msgAndArgs ...any) bool
- func IsTypef(t T, expectedType any, object any, msg string, args ...any) bool
- func JSONEq(t T, expected string, actual string, msgAndArgs ...any) bool
- func JSONEqBytes(t T, expected []byte, actual []byte, msgAndArgs ...any) bool
- func JSONEqBytesf(t T, expected []byte, actual []byte, msg string, args ...any) bool
- func JSONEqT[EDoc, ADoc Text](t T, expected EDoc, actual ADoc, msgAndArgs ...any) bool
- func JSONEqTf[EDoc, ADoc Text](t T, expected EDoc, actual ADoc, msg string, args ...any) bool
- func JSONEqf(t T, expected string, actual string, msg string, args ...any) bool
- func JSONMarshalAsT[EDoc Text](t T, expected EDoc, object any, msgAndArgs ...any) bool
- func JSONMarshalAsTf[EDoc Text](t T, expected EDoc, object any, msg string, args ...any) bool
- func JSONUnmarshalAsT[Object any, ADoc Text](t T, expected Object, jazon ADoc, msgAndArgs ...any) bool
- func JSONUnmarshalAsTf[Object any, ADoc Text](t T, expected Object, jazon ADoc, msg string, args ...any) bool
- func Kind(t T, expectedKind reflect.Kind, object any, msgAndArgs ...any) bool
- func Kindf(t T, expectedKind reflect.Kind, object any, msg string, args ...any) bool
- func Len(t T, object any, length int, msgAndArgs ...any) bool
- func Lenf(t T, object any, length int, msg string, args ...any) bool
- func Less(t T, e1 any, e2 any, msgAndArgs ...any) bool
- func LessOrEqual(t T, e1 any, e2 any, msgAndArgs ...any) bool
- func LessOrEqualT[Orderable Ordered](t T, e1 Orderable, e2 Orderable, msgAndArgs ...any) bool
- func LessOrEqualTf[Orderable Ordered](t T, e1 Orderable, e2 Orderable, msg string, args ...any) bool
- func LessOrEqualf(t T, e1 any, e2 any, msg string, args ...any) bool
- func LessT[Orderable Ordered](t T, e1 Orderable, e2 Orderable, msgAndArgs ...any) bool
- func LessTf[Orderable Ordered](t T, e1 Orderable, e2 Orderable, msg string, args ...any) bool
- func Lessf(t T, e1 any, e2 any, msg string, args ...any) bool
- func MapContainsT[Map ~map[K]V, K comparable, V any](t T, m Map, key K, msgAndArgs ...any) bool
- func MapContainsTf[Map ~map[K]V, K comparable, V any](t T, m Map, key K, msg string, args ...any) bool
- func MapNotContainsT[Map ~map[K]V, K comparable, V any](t T, m Map, key K, msgAndArgs ...any) bool
- func MapNotContainsTf[Map ~map[K]V, K comparable, V any](t T, m Map, key K, msg string, args ...any) bool
- func Negative(t T, e any, msgAndArgs ...any) bool
- func NegativeT[SignedNumber SignedNumeric](t T, e SignedNumber, msgAndArgs ...any) bool
- func NegativeTf[SignedNumber SignedNumeric](t T, e SignedNumber, msg string, args ...any) bool
- func Negativef(t T, e any, msg string, args ...any) bool
- func Never(t T, condition func() bool, timeout time.Duration, tick time.Duration, ...) bool
- func Neverf(t T, condition func() bool, timeout time.Duration, tick time.Duration, ...) bool
- func Nil(t T, object any, msgAndArgs ...any) bool
- func Nilf(t T, object any, msg string, args ...any) bool
- func NoError(t T, err error, msgAndArgs ...any) bool
- func NoErrorf(t T, err error, msg string, args ...any) bool
- func NoFileDescriptorLeak(t T, tested func(), msgAndArgs ...any) bool
- func NoFileDescriptorLeakf(t T, tested func(), msg string, args ...any) bool
- func NoGoRoutineLeak(t T, tested func(), msgAndArgs ...any) bool
- func NoGoRoutineLeakf(t T, tested func(), msg string, args ...any) bool
- func NotContains(t T, s any, contains any, msgAndArgs ...any) bool
- func NotContainsf(t T, s any, contains any, msg string, args ...any) bool
- func NotElementsMatch(t T, listA any, listB any, msgAndArgs ...any) (ok bool)
- func NotElementsMatchT[E comparable](t T, listA []E, listB []E, msgAndArgs ...any) (ok bool)
- func NotElementsMatchTf[E comparable](t T, listA []E, listB []E, msg string, args ...any) (ok bool)
- func NotElementsMatchf(t T, listA any, listB any, msg string, args ...any) (ok bool)
- func NotEmpty(t T, object any, msgAndArgs ...any) bool
- func NotEmptyf(t T, object any, msg string, args ...any) bool
- func NotEqual(t T, expected any, actual any, msgAndArgs ...any) bool
- func NotEqualT[V comparable](t T, expected V, actual V, msgAndArgs ...any) bool
- func NotEqualTf[V comparable](t T, expected V, actual V, msg string, args ...any) bool
- func NotEqualValues(t T, expected any, actual any, msgAndArgs ...any) bool
- func NotEqualValuesf(t T, expected any, actual any, msg string, args ...any) bool
- func NotEqualf(t T, expected any, actual any, msg string, args ...any) bool
- func NotErrorAs(t T, err error, target any, msgAndArgs ...any) bool
- func NotErrorAsf(t T, err error, target any, msg string, args ...any) bool
- func NotErrorIs(t T, err error, target error, msgAndArgs ...any) bool
- func NotErrorIsf(t T, err error, target error, msg string, args ...any) bool
- func NotImplements(t T, interfaceObject any, object any, msgAndArgs ...any) bool
- func NotImplementsf(t T, interfaceObject any, object any, msg string, args ...any) bool
- func NotKind(t T, expectedKind reflect.Kind, object any, msgAndArgs ...any) bool
- func NotKindf(t T, expectedKind reflect.Kind, object any, msg string, args ...any) bool
- func NotNil(t T, object any, msgAndArgs ...any) bool
- func NotNilf(t T, object any, msg string, args ...any) bool
- func NotPanics(t T, f func(), msgAndArgs ...any) bool
- func NotPanicsf(t T, f func(), msg string, args ...any) bool
- func NotRegexp(t T, rx any, actual any, msgAndArgs ...any) bool
- func NotRegexpT[Rex RegExp, ADoc Text](t T, rx Rex, actual ADoc, msgAndArgs ...any) bool
- func NotRegexpTf[Rex RegExp, ADoc Text](t T, rx Rex, actual ADoc, msg string, args ...any) bool
- func NotRegexpf(t T, rx any, actual any, msg string, args ...any) bool
- func NotSame(t T, expected any, actual any, msgAndArgs ...any) bool
- func NotSameT[P any](t T, expected *P, actual *P, msgAndArgs ...any) bool
- func NotSameTf[P any](t T, expected *P, actual *P, msg string, args ...any) bool
- func NotSamef(t T, expected any, actual any, msg string, args ...any) bool
- func NotSortedT[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msgAndArgs ...any) bool
- func NotSortedTf[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msg string, args ...any) bool
- func NotSubset(t T, list any, subset any, msgAndArgs ...any) (ok bool)
- func NotSubsetf(t T, list any, subset any, msg string, args ...any) (ok bool)
- func NotZero(t T, i any, msgAndArgs ...any) bool
- func NotZerof(t T, i any, msg string, args ...any) bool
- func ObjectsAreEqual(expected any, actual any) bool
- func ObjectsAreEqualValues(expected any, actual any) bool
- func Panics(t T, f func(), msgAndArgs ...any) bool
- func PanicsWithError(t T, errString string, f func(), msgAndArgs ...any) bool
- func PanicsWithErrorf(t T, errString string, f func(), msg string, args ...any) bool
- func PanicsWithValue(t T, expected any, f func(), msgAndArgs ...any) bool
- func PanicsWithValuef(t T, expected any, f func(), msg string, args ...any) bool
- func Panicsf(t T, f func(), msg string, args ...any) bool
- func Positive(t T, e any, msgAndArgs ...any) bool
- func PositiveT[SignedNumber SignedNumeric](t T, e SignedNumber, msgAndArgs ...any) bool
- func PositiveTf[SignedNumber SignedNumeric](t T, e SignedNumber, msg string, args ...any) bool
- func Positivef(t T, e any, msg string, args ...any) bool
- func Regexp(t T, rx any, actual any, msgAndArgs ...any) bool
- func RegexpT[Rex RegExp, ADoc Text](t T, rx Rex, actual ADoc, msgAndArgs ...any) bool
- func RegexpTf[Rex RegExp, ADoc Text](t T, rx Rex, actual ADoc, msg string, args ...any) bool
- func Regexpf(t T, rx any, actual any, msg string, args ...any) bool
- func Same(t T, expected any, actual any, msgAndArgs ...any) bool
- func SameT[P any](t T, expected *P, actual *P, msgAndArgs ...any) bool
- func SameTf[P any](t T, expected *P, actual *P, msg string, args ...any) bool
- func Samef(t T, expected any, actual any, msg string, args ...any) bool
- func SeqContainsT[E comparable](t T, iter iter.Seq[E], element E, msgAndArgs ...any) bool
- func SeqContainsTf[E comparable](t T, iter iter.Seq[E], element E, msg string, args ...any) bool
- func SeqNotContainsT[E comparable](t T, iter iter.Seq[E], element E, msgAndArgs ...any) bool
- func SeqNotContainsTf[E comparable](t T, iter iter.Seq[E], element E, msg string, args ...any) bool
- func SliceContainsT[Slice ~[]E, E comparable](t T, s Slice, element E, msgAndArgs ...any) bool
- func SliceContainsTf[Slice ~[]E, E comparable](t T, s Slice, element E, msg string, args ...any) bool
- func SliceNotContainsT[Slice ~[]E, E comparable](t T, s Slice, element E, msgAndArgs ...any) bool
- func SliceNotContainsTf[Slice ~[]E, E comparable](t T, s Slice, element E, msg string, args ...any) bool
- func SliceNotSubsetT[Slice ~[]E, E comparable](t T, list Slice, subset Slice, msgAndArgs ...any) (ok bool)
- func SliceNotSubsetTf[Slice ~[]E, E comparable](t T, list Slice, subset Slice, msg string, args ...any) (ok bool)
- func SliceSubsetT[Slice ~[]E, E comparable](t T, list Slice, subset Slice, msgAndArgs ...any) (ok bool)
- func SliceSubsetTf[Slice ~[]E, E comparable](t T, list Slice, subset Slice, msg string, args ...any) (ok bool)
- func SortedT[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msgAndArgs ...any) bool
- func SortedTf[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msg string, args ...any) bool
- func StringContainsT[ADoc, EDoc Text](t T, str ADoc, substring EDoc, msgAndArgs ...any) bool
- func StringContainsTf[ADoc, EDoc Text](t T, str ADoc, substring EDoc, msg string, args ...any) bool
- func StringNotContainsT[ADoc, EDoc Text](t T, str ADoc, substring EDoc, msgAndArgs ...any) bool
- func StringNotContainsTf[ADoc, EDoc Text](t T, str ADoc, substring EDoc, msg string, args ...any) bool
- func Subset(t T, list any, subset any, msgAndArgs ...any) (ok bool)
- func Subsetf(t T, list any, subset any, msg string, args ...any) (ok bool)
- func True(t T, value bool, msgAndArgs ...any) bool
- func TrueT[B Boolean](t T, value B, msgAndArgs ...any) bool
- func TrueTf[B Boolean](t T, value B, msg string, args ...any) bool
- func Truef(t T, value bool, msg string, args ...any) bool
- func WithinDuration(t T, expected time.Time, actual time.Time, delta time.Duration, ...) bool
- func WithinDurationf(t T, expected time.Time, actual time.Time, delta time.Duration, msg string, ...) bool
- func WithinRange(t T, actual time.Time, start time.Time, end time.Time, msgAndArgs ...any) bool
- func WithinRangef(t T, actual time.Time, start time.Time, end time.Time, msg string, args ...any) bool
- func YAMLEq(t T, expected string, actual string, msgAndArgs ...any) bool
- func YAMLEqBytes(t T, expected []byte, actual []byte, msgAndArgs ...any) bool
- func YAMLEqBytesf(t T, expected []byte, actual []byte, msg string, args ...any) bool
- func YAMLEqT[EDoc, ADoc Text](t T, expected EDoc, actual ADoc, msgAndArgs ...any) bool
- func YAMLEqTf[EDoc, ADoc Text](t T, expected EDoc, actual ADoc, msg string, args ...any) bool
- func YAMLEqf(t T, expected string, actual string, msg string, args ...any) bool
- func YAMLMarshalAsT[EDoc Text](t T, expected EDoc, object any, msgAndArgs ...any) bool
- func YAMLMarshalAsTf[EDoc Text](t T, expected EDoc, object any, msg string, args ...any) bool
- func YAMLUnmarshalAsT[Object any, ADoc Text](t T, expected Object, jazon ADoc, msgAndArgs ...any) bool
- func YAMLUnmarshalAsTf[Object any, ADoc Text](t T, expected Object, jazon ADoc, msg string, args ...any) bool
- func Zero(t T, i any, msgAndArgs ...any) bool
- func Zerof(t T, i any, msg string, args ...any) bool
- type Assertions
- func (a *Assertions) Condition(comp func() bool, msgAndArgs ...any) bool
- func (a *Assertions) Conditionf(comp func() bool, msg string, args ...any) bool
- func (a *Assertions) Contains(s any, contains any, msgAndArgs ...any) bool
- func (a *Assertions) Containsf(s any, contains any, msg string, args ...any) bool
- func (a *Assertions) DirExists(path string, msgAndArgs ...any) bool
- func (a *Assertions) DirExistsf(path string, msg string, args ...any) bool
- func (a *Assertions) DirNotExists(path string, msgAndArgs ...any) bool
- func (a *Assertions) DirNotExistsf(path string, msg string, args ...any) bool
- func (a *Assertions) ElementsMatch(listA any, listB any, msgAndArgs ...any) (ok bool)
- func (a *Assertions) ElementsMatchf(listA any, listB any, msg string, args ...any) (ok bool)
- func (a *Assertions) Empty(object any, msgAndArgs ...any) bool
- func (a *Assertions) Emptyf(object any, msg string, args ...any) bool
- func (a *Assertions) Equal(expected any, actual any, msgAndArgs ...any) bool
- func (a *Assertions) EqualError(err error, errString string, msgAndArgs ...any) bool
- func (a *Assertions) EqualErrorf(err error, errString string, msg string, args ...any) bool
- func (a *Assertions) EqualExportedValues(expected any, actual any, msgAndArgs ...any) bool
- func (a *Assertions) EqualExportedValuesf(expected any, actual any, msg string, args ...any) bool
- func (a *Assertions) EqualValues(expected any, actual any, msgAndArgs ...any) bool
- func (a *Assertions) EqualValuesf(expected any, actual any, msg string, args ...any) bool
- func (a *Assertions) Equalf(expected any, actual any, msg string, args ...any) bool
- func (a *Assertions) Error(err error, msgAndArgs ...any) bool
- func (a *Assertions) ErrorAs(err error, target any, msgAndArgs ...any) bool
- func (a *Assertions) ErrorAsf(err error, target any, msg string, args ...any) bool
- func (a *Assertions) ErrorContains(err error, contains string, msgAndArgs ...any) bool
- func (a *Assertions) ErrorContainsf(err error, contains string, msg string, args ...any) bool
- func (a *Assertions) ErrorIs(err error, target error, msgAndArgs ...any) bool
- func (a *Assertions) ErrorIsf(err error, target error, msg string, args ...any) bool
- func (a *Assertions) Errorf(err error, msg string, args ...any) bool
- func (a *Assertions) Exactly(expected any, actual any, msgAndArgs ...any) bool
- func (a *Assertions) Exactlyf(expected any, actual any, msg string, args ...any) bool
- func (a *Assertions) Fail(failureMessage string, msgAndArgs ...any) bool
- func (a *Assertions) FailNow(failureMessage string, msgAndArgs ...any) bool
- func (a *Assertions) FailNowf(failureMessage string, msg string, args ...any) bool
- func (a *Assertions) Failf(failureMessage string, msg string, args ...any) bool
- func (a *Assertions) False(value bool, msgAndArgs ...any) bool
- func (a *Assertions) Falsef(value bool, msg string, args ...any) bool
- func (a *Assertions) FileEmpty(path string, msgAndArgs ...any) bool
- func (a *Assertions) FileEmptyf(path string, msg string, args ...any) bool
- func (a *Assertions) FileExists(path string, msgAndArgs ...any) bool
- func (a *Assertions) FileExistsf(path string, msg string, args ...any) bool
- func (a *Assertions) FileNotEmpty(path string, msgAndArgs ...any) bool
- func (a *Assertions) FileNotEmptyf(path string, msg string, args ...any) bool
- func (a *Assertions) FileNotExists(path string, msgAndArgs ...any) bool
- func (a *Assertions) FileNotExistsf(path string, msg string, args ...any) bool
- func (a *Assertions) Greater(e1 any, e2 any, msgAndArgs ...any) bool
- func (a *Assertions) GreaterOrEqual(e1 any, e2 any, msgAndArgs ...any) bool
- func (a *Assertions) GreaterOrEqualf(e1 any, e2 any, msg string, args ...any) bool
- func (a *Assertions) Greaterf(e1 any, e2 any, msg string, args ...any) bool
- func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func (a *Assertions) HTTPBodyContainsf(handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func (a *Assertions) HTTPBodyNotContains(handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func (a *Assertions) HTTPBodyNotContainsf(handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func (a *Assertions) HTTPError(handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func (a *Assertions) HTTPErrorf(handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func (a *Assertions) HTTPRedirect(handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func (a *Assertions) HTTPRedirectf(handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func (a *Assertions) HTTPStatusCode(handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func (a *Assertions) HTTPStatusCodef(handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func (a *Assertions) HTTPSuccess(handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func (a *Assertions) HTTPSuccessf(handler http.HandlerFunc, method string, url string, values url.Values, ...) bool
- func (a *Assertions) Implements(interfaceObject any, object any, msgAndArgs ...any) bool
- func (a *Assertions) Implementsf(interfaceObject any, object any, msg string, args ...any) bool
- func (a *Assertions) InDelta(expected any, actual any, delta float64, msgAndArgs ...any) bool
- func (a *Assertions) InDeltaMapValues(expected any, actual any, delta float64, msgAndArgs ...any) bool
- func (a *Assertions) InDeltaMapValuesf(expected any, actual any, delta float64, msg string, args ...any) bool
- func (a *Assertions) InDeltaSlice(expected any, actual any, delta float64, msgAndArgs ...any) bool
- func (a *Assertions) InDeltaSlicef(expected any, actual any, delta float64, msg string, args ...any) bool
- func (a *Assertions) InDeltaf(expected any, actual any, delta float64, msg string, args ...any) bool
- func (a *Assertions) InEpsilon(expected any, actual any, epsilon float64, msgAndArgs ...any) bool
- func (a *Assertions) InEpsilonSlice(expected any, actual any, epsilon float64, msgAndArgs ...any) bool
- func (a *Assertions) InEpsilonSlicef(expected any, actual any, epsilon float64, msg string, args ...any) bool
- func (a *Assertions) InEpsilonf(expected any, actual any, epsilon float64, msg string, args ...any) bool
- func (a *Assertions) IsDecreasing(collection any, msgAndArgs ...any) bool
- func (a *Assertions) IsDecreasingf(collection any, msg string, args ...any) bool
- func (a *Assertions) IsIncreasing(collection any, msgAndArgs ...any) bool
- func (a *Assertions) IsIncreasingf(collection any, msg string, args ...any) bool
- func (a *Assertions) IsNonDecreasing(collection any, msgAndArgs ...any) bool
- func (a *Assertions) IsNonDecreasingf(collection any, msg string, args ...any) bool
- func (a *Assertions) IsNonIncreasing(collection any, msgAndArgs ...any) bool
- func (a *Assertions) IsNonIncreasingf(collection any, msg string, args ...any) bool
- func (a *Assertions) IsNotType(theType any, object any, msgAndArgs ...any) bool
- func (a *Assertions) IsNotTypef(theType any, object any, msg string, args ...any) bool
- func (a *Assertions) IsType(expectedType any, object any, msgAndArgs ...any) bool
- func (a *Assertions) IsTypef(expectedType any, object any, msg string, args ...any) bool
- func (a *Assertions) JSONEq(expected string, actual string, msgAndArgs ...any) bool
- func (a *Assertions) JSONEqBytes(expected []byte, actual []byte, msgAndArgs ...any) bool
- func (a *Assertions) JSONEqBytesf(expected []byte, actual []byte, msg string, args ...any) bool
- func (a *Assertions) JSONEqf(expected string, actual string, msg string, args ...any) bool
- func (a *Assertions) Kind(expectedKind reflect.Kind, object any, msgAndArgs ...any) bool
- func (a *Assertions) Kindf(expectedKind reflect.Kind, object any, msg string, args ...any) bool
- func (a *Assertions) Len(object any, length int, msgAndArgs ...any) bool
- func (a *Assertions) Lenf(object any, length int, msg string, args ...any) bool
- func (a *Assertions) Less(e1 any, e2 any, msgAndArgs ...any) bool
- func (a *Assertions) LessOrEqual(e1 any, e2 any, msgAndArgs ...any) bool
- func (a *Assertions) LessOrEqualf(e1 any, e2 any, msg string, args ...any) bool
- func (a *Assertions) Lessf(e1 any, e2 any, msg string, args ...any) bool
- func (a *Assertions) Negative(e any, msgAndArgs ...any) bool
- func (a *Assertions) Negativef(e any, msg string, args ...any) bool
- func (a *Assertions) Never(condition func() bool, timeout time.Duration, tick time.Duration, ...) bool
- func (a *Assertions) Neverf(condition func() bool, timeout time.Duration, tick time.Duration, msg string, ...) bool
- func (a *Assertions) Nil(object any, msgAndArgs ...any) bool
- func (a *Assertions) Nilf(object any, msg string, args ...any) bool
- func (a *Assertions) NoError(err error, msgAndArgs ...any) bool
- func (a *Assertions) NoErrorf(err error, msg string, args ...any) bool
- func (a *Assertions) NoFileDescriptorLeak(tested func(), msgAndArgs ...any) bool
- func (a *Assertions) NoFileDescriptorLeakf(tested func(), msg string, args ...any) bool
- func (a *Assertions) NoGoRoutineLeak(tested func(), msgAndArgs ...any) bool
- func (a *Assertions) NoGoRoutineLeakf(tested func(), msg string, args ...any) bool
- func (a *Assertions) NotContains(s any, contains any, msgAndArgs ...any) bool
- func (a *Assertions) NotContainsf(s any, contains any, msg string, args ...any) bool
- func (a *Assertions) NotElementsMatch(listA any, listB any, msgAndArgs ...any) (ok bool)
- func (a *Assertions) NotElementsMatchf(listA any, listB any, msg string, args ...any) (ok bool)
- func (a *Assertions) NotEmpty(object any, msgAndArgs ...any) bool
- func (a *Assertions) NotEmptyf(object any, msg string, args ...any) bool
- func (a *Assertions) NotEqual(expected any, actual any, msgAndArgs ...any) bool
- func (a *Assertions) NotEqualValues(expected any, actual any, msgAndArgs ...any) bool
- func (a *Assertions) NotEqualValuesf(expected any, actual any, msg string, args ...any) bool
- func (a *Assertions) NotEqualf(expected any, actual any, msg string, args ...any) bool
- func (a *Assertions) NotErrorAs(err error, target any, msgAndArgs ...any) bool
- func (a *Assertions) NotErrorAsf(err error, target any, msg string, args ...any) bool
- func (a *Assertions) NotErrorIs(err error, target error, msgAndArgs ...any) bool
- func (a *Assertions) NotErrorIsf(err error, target error, msg string, args ...any) bool
- func (a *Assertions) NotImplements(interfaceObject any, object any, msgAndArgs ...any) bool
- func (a *Assertions) NotImplementsf(interfaceObject any, object any, msg string, args ...any) bool
- func (a *Assertions) NotKind(expectedKind reflect.Kind, object any, msgAndArgs ...any) bool
- func (a *Assertions) NotKindf(expectedKind reflect.Kind, object any, msg string, args ...any) bool
- func (a *Assertions) NotNil(object any, msgAndArgs ...any) bool
- func (a *Assertions) NotNilf(object any, msg string, args ...any) bool
- func (a *Assertions) NotPanics(f func(), msgAndArgs ...any) bool
- func (a *Assertions) NotPanicsf(f func(), msg string, args ...any) bool
- func (a *Assertions) NotRegexp(rx any, actual any, msgAndArgs ...any) bool
- func (a *Assertions) NotRegexpf(rx any, actual any, msg string, args ...any) bool
- func (a *Assertions) NotSame(expected any, actual any, msgAndArgs ...any) bool
- func (a *Assertions) NotSamef(expected any, actual any, msg string, args ...any) bool
- func (a *Assertions) NotSubset(list any, subset any, msgAndArgs ...any) (ok bool)
- func (a *Assertions) NotSubsetf(list any, subset any, msg string, args ...any) (ok bool)
- func (a *Assertions) NotZero(i any, msgAndArgs ...any) bool
- func (a *Assertions) NotZerof(i any, msg string, args ...any) bool
- func (a *Assertions) Panics(f func(), msgAndArgs ...any) bool
- func (a *Assertions) PanicsWithError(errString string, f func(), msgAndArgs ...any) bool
- func (a *Assertions) PanicsWithErrorf(errString string, f func(), msg string, args ...any) bool
- func (a *Assertions) PanicsWithValue(expected any, f func(), msgAndArgs ...any) bool
- func (a *Assertions) PanicsWithValuef(expected any, f func(), msg string, args ...any) bool
- func (a *Assertions) Panicsf(f func(), msg string, args ...any) bool
- func (a *Assertions) Positive(e any, msgAndArgs ...any) bool
- func (a *Assertions) Positivef(e any, msg string, args ...any) bool
- func (a *Assertions) Regexp(rx any, actual any, msgAndArgs ...any) bool
- func (a *Assertions) Regexpf(rx any, actual any, msg string, args ...any) bool
- func (a *Assertions) Same(expected any, actual any, msgAndArgs ...any) bool
- func (a *Assertions) Samef(expected any, actual any, msg string, args ...any) bool
- func (a *Assertions) Subset(list any, subset any, msgAndArgs ...any) (ok bool)
- func (a *Assertions) Subsetf(list any, subset any, msg string, args ...any) (ok bool)
- func (a *Assertions) True(value bool, msgAndArgs ...any) bool
- func (a *Assertions) Truef(value bool, msg string, args ...any) bool
- func (a *Assertions) WithinDuration(expected time.Time, actual time.Time, delta time.Duration, msgAndArgs ...any) bool
- func (a *Assertions) WithinDurationf(expected time.Time, actual time.Time, delta time.Duration, msg string, ...) bool
- func (a *Assertions) WithinRange(actual time.Time, start time.Time, end time.Time, msgAndArgs ...any) bool
- func (a *Assertions) WithinRangef(actual time.Time, start time.Time, end time.Time, msg string, args ...any) bool
- func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...any) bool
- func (a *Assertions) YAMLEqBytes(expected []byte, actual []byte, msgAndArgs ...any) bool
- func (a *Assertions) YAMLEqBytesf(expected []byte, actual []byte, msg string, args ...any) bool
- func (a *Assertions) YAMLEqf(expected string, actual string, msg string, args ...any) bool
- func (a *Assertions) Zero(i any, msgAndArgs ...any) bool
- func (a *Assertions) Zerof(i any, msg string, args ...any) bool
- type BoolAssertionFunc
- type Boolean
- type CollectT
- type CollectibleConditioner
- type ComparisonAssertionFunc
- type Conditioner
- type ErrorAssertionFunc
- type H
- type Measurable
- type Ordered
- type PanicAssertionFunc
- type RegExp
- type SignedNumeric
- type T
- type TestExampleError
- type TestingTdeprecated
- type Text
- type UnsignedNumeric
- type ValueAssertionFunc
Examples ¶
- Assertions (With_generics)
- BoolAssertionFunc
- ComparisonAssertionFunc
- Condition
- Consistently
- Consistently (AlwaysHealthy)
- Consistently (Invariant)
- Contains
- DirExists
- DirNotExists
- ElementsMatch
- ElementsMatchT
- Empty
- Equal
- EqualError
- EqualExportedValues
- EqualT
- EqualValues
- Error
- ErrorAs
- ErrorAssertionFunc
- ErrorContains
- ErrorIs
- Eventually
- Eventually (AsyncReady)
- Eventually (HealthCheck)
- EventuallyWith
- Exactly
- False
- FalseT
- FileEmpty
- FileExists
- FileNotEmpty
- FileNotExists
- Greater
- GreaterOrEqual
- GreaterOrEqualT
- GreaterT
- HTTPBodyContains
- HTTPBodyNotContains
- HTTPError
- HTTPRedirect
- HTTPStatusCode
- HTTPSuccess
- Implements
- InDelta
- InDeltaMapValues
- InDeltaSlice
- InDeltaT
- InEpsilon
- InEpsilonSlice
- InEpsilonT
- IsDecreasing
- IsDecreasingT
- IsIncreasing
- IsIncreasingT
- IsNonDecreasing
- IsNonDecreasingT
- IsNonIncreasing
- IsNonIncreasingT
- IsNotOfTypeT
- IsNotType
- IsOfTypeT
- IsType
- JSONEq
- JSONEqBytes
- JSONEqT
- JSONMarshalAsT
- JSONUnmarshalAsT
- Kind
- Len
- Less
- LessOrEqual
- LessOrEqualT
- LessT
- MapContainsT
- MapNotContainsT
- Negative
- NegativeT
- Never
- Never (NoSpuriousEvents)
- Nil
- NoError
- NoFileDescriptorLeak
- NoGoRoutineLeak
- NoGoRoutineLeak (Fail)
- NotContains
- NotElementsMatch
- NotElementsMatchT
- NotEmpty
- NotEqual
- NotEqualT
- NotEqualValues
- NotErrorAs
- NotErrorIs
- NotImplements
- NotKind
- NotNil
- NotPanics
- NotRegexp
- NotRegexpT
- NotSame
- NotSameT
- NotSortedT
- NotSubset
- NotZero
- PanicAssertionFunc
- Panics
- PanicsWithError
- PanicsWithValue
- Positive
- PositiveT
- Regexp
- RegexpT
- Same
- SameT
- SeqContainsT
- SeqNotContainsT
- SliceContainsT
- SliceNotContainsT
- SliceNotSubsetT
- SliceSubsetT
- SortedT
- StringContainsT
- StringNotContainsT
- Subset
- True
- TrueT
- ValueAssertionFunc
- WithinDuration
- WithinRange
- Zero
Constants ¶
const ( // ErrTest is an error instance useful for testing. // // If the code does not care about error specifics, and only needs // to return the error as an example, this error may be used to make // the test code more readable. ErrTest = assertions.ErrTest )
Variables ¶
This section is empty.
Functions ¶
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 ¶
Condition uses a comparison function to assert a complex condition.
Usage ¶
assertions.Condition(t, func() bool { return myCondition })
Examples ¶
success: func() bool { return true }
failure: func() bool { return false }
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestCondition(t *testing.T)
success := assert.Condition(t, func() bool {
return true
})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func Conditionf ¶
Conditionf is the same as Condition, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Consistently ¶ added in v2.4.0
func Consistently[C Conditioner](t T, condition C, timeout time.Duration, tick time.Duration, msgAndArgs ...any) bool
Consistently asserts that the given condition is always satisfied until timeout, periodically checking the target function at each tick.
Consistently ("always") imposes a stronger constraint than Eventually ("at least once"): it checks at every tick that every occurrence of the condition is satisfied, whereas Eventually succeeds on the first occurrence of a successful condition.
Usage ¶
assertions.Consistently(t, func() bool { return true }, time.Second, 10*time.Millisecond)
See also Eventually for details about using context and concurrency.
Alternative condition signature ¶
The simplest form of condition is:
func() bool
The semantics of the assertion are "always returns true".
To build more complex cases, a condition may also be defined as:
func(context.Context) error
It fails as soon as an error is returned before timeout expressing "always returns no error (nil)"
This is consistent with Eventually expressing "eventually returns no error (nil)".
It will be executed with the context of the assertion, which inherits the testing.T.Context and is cancelled on timeout.
Concurrency ¶
See Eventually.
Attention point ¶
See Eventually.
Examples ¶
success: func() bool { return true }, 100*time.Millisecond, 20*time.Millisecond
failure: func() bool { return false }, 100*time.Millisecond, 20*time.Millisecond
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"time"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestConsistently(t *testing.T)
success := assert.Consistently(t, func() bool {
return true
}, 100*time.Millisecond, 20*time.Millisecond)
fmt.Printf("success: %t\n", success)
}
Output: success: true
Example (AlwaysHealthy) ¶
ExampleConsistently_alwaysHealthy demonstrates Consistently with a func(context.Context) error condition, asserting that the operation always succeeds (returns nil) throughout the observation period.
package main
import (
"context"
"fmt"
"testing"
"time"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // normally provided by test
// Simulate a service that stays healthy.
healthCheck := func(_ context.Context) error {
return nil // always healthy
}
result := assert.Consistently(t, healthCheck, 100*time.Millisecond, 10*time.Millisecond)
fmt.Printf("consistently healthy: %t", result)
}
Output: consistently healthy: true
Example (Invariant) ¶
ExampleConsistently_invariant demonstrates asserting that a condition remains true throughout the entire observation period.
package main
import (
"fmt"
"sync/atomic"
"testing"
"time"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // normally provided by test
// A counter that stays within bounds during the test.
var counter atomic.Int32
counter.Store(5)
result := assert.Consistently(t, func() bool {
return counter.Load() < 10
}, 100*time.Millisecond, 10*time.Millisecond)
fmt.Printf("consistently under limit: %t", result)
}
Output: consistently under limit: true
func Consistentlyf ¶ added in v2.4.0
func Consistentlyf[C Conditioner](t T, condition C, timeout time.Duration, tick time.Duration, msg string, args ...any) bool
Consistentlyf is the same as Consistently, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Contains ¶
Contains asserts that the specified string, list(array, slice...) or map contains the specified substring or element.
Usage ¶
assertions.Contains(t, "Hello World", "World")
assertions.Contains(t, []string{"Hello", "World"}, "World")
assertions.Contains(t, map[string]string{"Hello": "World"}, "Hello")
Examples ¶
success: []string{"A","B"}, "A"
failure: []string{"A","B"}, "C"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestContains(t *testing.T)
success := assert.Contains(t, []string{"A", "B"}, "A")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func Containsf ¶
Containsf is the same as Contains, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func DirExists ¶
DirExists checks whether a directory exists in the given path. It also fails if the path is a file rather a directory or there is an error checking whether it exists.
Usage ¶
assertions.DirExists(t, "path/to/directory")
Examples ¶
success: filepath.Join(testDataPath(),"existing_dir") failure: filepath.Join(testDataPath(),"non_existing_dir")
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"path/filepath"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestDirExists(t *testing.T)
success := assert.DirExists(t, filepath.Join(testDataPath(), "existing_dir"))
fmt.Printf("success: %t\n", success)
}
func testDataPath() string {
return filepath.Join("..", "internal", "assertions", "testdata")
}
Output: success: true
func DirExistsf ¶
DirExistsf is the same as DirExists, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func DirNotExists ¶ added in v2.2.0
DirNotExists checks whether a directory does not exist in the given path. It fails if the path points to an existing _directory_ only.
Usage ¶
assertions.DirNotExists(t, "path/to/directory")
Examples ¶
success: filepath.Join(testDataPath(),"non_existing_dir") failure: filepath.Join(testDataPath(),"existing_dir")
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"path/filepath"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestDirNotExists(t *testing.T)
success := assert.DirNotExists(t, filepath.Join(testDataPath(), "non_existing_dir"))
fmt.Printf("success: %t\n", success)
}
func testDataPath() string {
return filepath.Join("..", "internal", "assertions", "testdata")
}
Output: success: true
func DirNotExistsf ¶ added in v2.2.0
DirNotExistsf is the same as DirNotExists, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func ElementsMatch ¶
ElementsMatch asserts that the specified listA(array, slice...) is equal to specified listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, the number of appearances of each of them in both lists should match.
Usage ¶
assertions.ElementsMatch(t, []int{1, 3, 2, 3}, []int{1, 3, 3, 2})
Examples ¶
success: []int{1, 3, 2, 3}, []int{1, 3, 3, 2}
failure: []int{1, 2, 3}, []int{1, 2, 4}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestElementsMatch(t *testing.T)
success := assert.ElementsMatch(t, []int{1, 3, 2, 3}, []int{1, 3, 3, 2})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func ElementsMatchT ¶ added in v2.2.0
func ElementsMatchT[E comparable](t T, listA []E, listB []E, msgAndArgs ...any) bool
ElementsMatchT asserts that the specified listA(array, slice...) is equal to specified listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, the number of appearances of each of them in both lists should match.
Usage ¶
assertions.ElementsMatchT(t, []int{1, 3, 2, 3}, []int{1, 3, 3, 2})
Examples ¶
success: []int{1, 3, 2, 3}, []int{1, 3, 3, 2}
failure: []int{1, 2, 3}, []int{1, 2, 4}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestElementsMatchT(t *testing.T)
success := assert.ElementsMatchT(t, []int{1, 3, 2, 3}, []int{1, 3, 3, 2})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func ElementsMatchTf ¶ added in v2.2.0
func ElementsMatchTf[E comparable](t T, listA []E, listB []E, msg string, args ...any) bool
ElementsMatchTf is the same as ElementsMatchT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func ElementsMatchf ¶
ElementsMatchf is the same as ElementsMatch, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Empty ¶
Empty asserts that the given value is "empty".
Zero values are "empty".
Arrays are "empty" if every element is the zero value of the type (stricter than "empty").
Slices, maps and channels with zero length are "empty".
Pointer values are "empty" if the pointer is nil or if the pointed value is "empty".
Usage ¶
assertions.Empty(t, obj)
Examples ¶
success: "" failure: "not empty"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestEmpty(t *testing.T)
success := assert.Empty(t, "")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func Emptyf ¶
Emptyf is the same as Empty, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Equal ¶
Equal asserts that two objects are equal.
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.
Usage ¶
assertions.Equal(t, 123, 123)
Examples ¶
success: 123, 123 failure: 123, 456
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestEqual(t *testing.T)
success := assert.Equal(t, 123, 123)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func EqualError ¶
EqualError asserts that a function returned a non-nil error (i.e. an error) and that it is equal to the provided error.
Usage ¶
actualObj, err := SomeFunction() assertions.EqualError(t, err, expectedErrorString)
Examples ¶
success: ErrTest, "assert.ErrTest general error for testing" failure: ErrTest, "wrong error message"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestEqualError(t *testing.T)
success := assert.EqualError(t, assert.ErrTest, "assert.ErrTest general error for testing")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func EqualErrorf ¶
EqualErrorf is the same as EqualError, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func EqualExportedValues ¶
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.
Function equality cannot be determined and will always fail.
Usage ¶
type S struct {
Exported int
notExported int
}
assertions.EqualExportedValues(t, S{1, 2}, S{1, 3}) => true
assertions.EqualExportedValues(t, S{1, 2}, S{2, 3}) => false
Examples ¶
success: &dummyStruct{A: "a", b: 1}, &dummyStruct{A: "a", b: 2}
failure: &dummyStruct{A: "a", b: 1}, &dummyStruct{A: "b", b: 1}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestEqualExportedValues(t *testing.T)
success := assert.EqualExportedValues(t, &dummyStruct{A: "a", b: 1}, &dummyStruct{A: "a", b: 2})
fmt.Printf("success: %t\n", success)
}
type dummyStruct struct {
A string
b int
}
Output: success: true
func EqualExportedValuesf ¶
EqualExportedValuesf is the same as EqualExportedValues, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func EqualT ¶ added in v2.2.0
func EqualT[V comparable](t T, expected V, actual V, msgAndArgs ...any) bool
EqualT asserts that two objects of the same comparable type are equal.
Pointer variable equality is determined based on the equality of the memory addresses (unlike Equal, but like Same).
Functions, slices and maps are not comparable. See also ComparisonOperators.
If you need to compare values of non-comparable types, or compare pointers by the value they point to, use Equal instead.
Usage ¶
assertions.EqualT(t, 123, 123)
Examples ¶
success: 123, 123 failure: 123, 456
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestEqualT(t *testing.T)
success := assert.EqualT(t, 123, 123)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func EqualTf ¶ added in v2.2.0
func EqualTf[V comparable](t T, expected V, actual V, msg string, args ...any) bool
EqualTf is the same as EqualT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func EqualValues ¶
EqualValues asserts that two objects are equal or convertible to the larger type and equal.
Function equality cannot be determined and will always fail.
Usage ¶
assertions.EqualValues(t, uint32(123), int32(123))
Examples ¶
success: uint32(123), int32(123) failure: uint32(123), int32(456)
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestEqualValues(t *testing.T)
success := assert.EqualValues(t, uint32(123), int32(123))
fmt.Printf("success: %t\n", success)
}
Output: success: true
func EqualValuesf ¶
EqualValuesf is the same as EqualValues, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Equalf ¶
Equalf is the same as Equal, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Error ¶
Error asserts that a function returned a non-nil error (i.e. an error).
Usage ¶
actualObj, err := SomeFunction() assertions.Error(t, err)
Examples ¶
success: ErrTest failure: nil
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestError(t *testing.T)
success := assert.Error(t, assert.ErrTest)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func ErrorAs ¶
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.
Usage ¶
assertions.ErrorAs(t, err, &target)
Examples ¶
success: fmt.Errorf("wrap: %w", &dummyError{}), new(*dummyError)
failure: ErrTest, new(*dummyError)
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestErrorAs(t *testing.T)
success := assert.ErrorAs(t, fmt.Errorf("wrap: %w", &dummyError{}), new(*dummyError))
fmt.Printf("success: %t\n", success)
}
type dummyError struct {
}
func (d *dummyError) Error() string {
return "dummy error"
}
Output: success: true
func ErrorAsf ¶
ErrorAsf is the same as ErrorAs, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func ErrorContains ¶
ErrorContains asserts that a function returned a non-nil error (i.e. an error) and that the error contains the specified substring.
Usage ¶
actualObj, err := SomeFunction() assertions.ErrorContains(t, err, expectedErrorSubString)
Examples ¶
success: ErrTest, "general error" failure: ErrTest, "not in message"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestErrorContains(t *testing.T)
success := assert.ErrorContains(t, assert.ErrTest, "general error")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func ErrorContainsf ¶
ErrorContainsf is the same as ErrorContains, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func ErrorIs ¶
ErrorIs asserts that at least one of the errors in err's chain matches target.
This is a wrapper for errors.Is.
Usage ¶
assertions.ErrorIs(t, err, io.EOF)
Examples ¶
success: fmt.Errorf("wrap: %w", io.EOF), io.EOF
failure: ErrTest, io.EOF
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"io"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestErrorIs(t *testing.T)
success := assert.ErrorIs(t, fmt.Errorf("wrap: %w", io.EOF), io.EOF)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func ErrorIsf ¶
ErrorIsf is the same as ErrorIs, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Errorf ¶
Errorf is the same as Error, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Eventually ¶
func Eventually[C Conditioner](t T, condition C, timeout time.Duration, tick time.Duration, msgAndArgs ...any) bool
Eventually asserts that the given condition will be met before timeout, periodically checking the target function on each tick.
Eventually waits until the condition returns true, at most until timeout, or until the parent context of the test is cancelled.
If the condition takes longer than the timeout to complete, Eventually fails but waits for the current condition execution to finish before returning.
For long-running conditions to be interrupted early, check testing.T.Context which is cancelled on test failure.
Usage ¶
assertions.Eventually(t, func() bool { return true }, time.Second, 10*time.Millisecond)
Alternative condition signature ¶
The simplest form of condition is:
func() bool
To build more complex cases, a condition may also be defined as:
func(context.Context) error
It fails when an error has always been returned up to timeout (equivalent semantics to func() bool returns false), expressing "eventually returns no error (nil)".
It will be executed with the context of the assertion, which inherits the testing.T.Context and is cancelled on timeout.
The semantics of the three available async assertions read as follows.
Eventually (func() bool) : "eventually returns true"
Never (func() bool) : "never returns true"
Consistently (func() bool): "always returns true"
Eventually (func(ctx) error) : "eventually returns nil"
Never (func(ctx) error) : not supported, use Consistently instead (avoids confusion with double negation)
Consistently (func(ctx) error): "always returns nil"
Concurrency ¶
The condition function is always executed serially by a single goroutine. It is always executed at least once.
It may thus write to variables outside its scope without triggering race conditions.
A blocking condition will cause Eventually to hang until it returns.
Notice that time ticks may be skipped if the condition takes longer than the tick interval.
Attention point ¶
Time-based tests may be flaky in a resource-constrained environment such as a CI runner and may produce counter-intuitive results, such as ticks or timeouts not firing in time as expected.
To avoid flaky tests, always make sure that ticks and timeouts differ by at least an order of magnitude (tick << timeout).
Examples ¶
success: func() bool { return true }, 100*time.Millisecond, 20*time.Millisecond
failure: func() bool { return false }, 100*time.Millisecond, 20*time.Millisecond
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"time"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestEventually(t *testing.T)
success := assert.Eventually(t, func() bool {
return true
}, 100*time.Millisecond, 20*time.Millisecond)
fmt.Printf("success: %t\n", success)
}
Output: success: true
Example (AsyncReady) ¶
ExampleEventually_asyncReady demonstrates polling a condition that becomes true after a few attempts, simulating an asynchronous operation completing.
package main
import (
"fmt"
"sync/atomic"
"testing"
"time"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // normally provided by test
// Simulate an async operation that completes after a short delay.
var ready atomic.Bool
go func() {
time.Sleep(30 * time.Millisecond)
ready.Store(true)
}()
result := assert.Eventually(t, ready.Load, 200*time.Millisecond, 10*time.Millisecond)
fmt.Printf("eventually ready: %t", result)
}
Output: eventually ready: true
Example (HealthCheck) ¶
ExampleEventually_healthCheck demonstrates Eventually with a func(context.Context) error condition, polling until the operation succeeds (returns nil).
package main
import (
"context"
"errors"
"fmt"
"sync/atomic"
"testing"
"time"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // normally provided by test
// Simulate a service that becomes healthy after a few attempts.
var attempts atomic.Int32
healthCheck := func(_ context.Context) error {
if attempts.Add(1) < 3 {
return errors.New("service not ready")
}
return nil
}
result := assert.Eventually(t, healthCheck, 200*time.Millisecond, 10*time.Millisecond)
fmt.Printf("eventually healthy: %t", result)
}
Output: eventually healthy: true
func EventuallyWith ¶ added in v2.3.0
func EventuallyWith[C CollectibleConditioner](t T, condition C, timeout time.Duration, tick time.Duration, msgAndArgs ...any) bool
EventuallyWith asserts that the given condition will be met before the timeout, periodically checking the target function at each tick.
In contrast to Eventually, the condition function is supplied with a CollectT to accumulate errors from calling other assertions.
The condition is considered "met" if no errors are raised in a tick. The supplied CollectT collects all errors from one tick.
If the condition is not met before the timeout, the collected errors from the last tick are copied to t.
Calling [CollectT.FailNow] cancels the condition immediately and causes the assertion to fail.
Usage ¶
externalValue := false
go func() {
time.Sleep(8*time.Second)
externalValue = true
}()
assertions.EventuallyWith(t, func(c *assertions.CollectT) {
// add assertions as needed; any assertion failure will fail the current tick
assertions.True(c, externalValue, "expected 'externalValue' to be true")
},
10*time.Second,
1*time.Second,
"external state has not changed to 'true'; still false",
)
Concurrency ¶
The condition function is never executed in parallel: only one goroutine executes it. It may write to variables outside its scope without triggering race conditions.
Examples ¶
success: func(c *CollectT) { True(c,true) }, 100*time.Millisecond, 20*time.Millisecond
failure: func(c *CollectT) { False(c,true) }, 100*time.Millisecond, 20*time.Millisecond
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"time"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestEventuallyWith(t *testing.T)
success := assert.EventuallyWith(t, func(c *assert.CollectT) {
assert.True(c, true)
}, 100*time.Millisecond, 20*time.Millisecond)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func EventuallyWithf ¶ added in v2.3.0
func EventuallyWithf[C CollectibleConditioner](t T, condition C, timeout time.Duration, tick time.Duration, msg string, args ...any) bool
EventuallyWithf is the same as EventuallyWith, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Eventuallyf ¶
func Eventuallyf[C Conditioner](t T, condition C, timeout time.Duration, tick time.Duration, msg string, args ...any) bool
Eventuallyf is the same as Eventually, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Exactly ¶
Exactly asserts that two objects are equal in value and type.
Usage ¶
assertions.Exactly(t, int32(123), int64(123))
Examples ¶
success: int32(123), int32(123) failure: int32(123), int64(123)
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestExactly(t *testing.T)
success := assert.Exactly(t, int32(123), int32(123))
fmt.Printf("success: %t\n", success)
}
Output: success: true
func Exactlyf ¶
Exactlyf is the same as Exactly, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Fail ¶
Fail reports a failure through.
Usage ¶
assertions.Fail(t, "failed")
Examples ¶
failure: "failed"
Upon failure, the test T is marked as failed and continues execution.
func FailNow ¶
FailNow fails test.
Usage ¶
assertions.FailNow(t, "failed")
Examples ¶
failure: "failed"
Upon failure, the test T is marked as failed and continues execution.
func FailNowf ¶
FailNowf is the same as FailNow, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Failf ¶
Failf is the same as Fail, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func False ¶
False asserts that the specified value is false.
Usage ¶
assertions.False(t, myBool)
Examples ¶
success: 1 == 0 failure: 1 == 1
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestFalse(t *testing.T)
success := assert.False(t, 1 == 0)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func FalseT ¶ added in v2.2.0
FalseT asserts that the specified value is false.
The type constraint Boolean accepts any type which underlying type is bool.
Usage ¶
type B bool var b B = true assertions.FalseT(t, b)
Examples ¶
success: 1 == 0 failure: 1 == 1
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestFalseT(t *testing.T)
success := assert.FalseT(t, 1 == 0)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func FalseTf ¶ added in v2.2.0
FalseTf is the same as FalseT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Falsef ¶
Falsef is the same as False, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func FileEmpty ¶ added in v2.1.0
FileEmpty checks whether a file exists in the given path and is empty. It fails if the file is not empty, if the path points to a directory or there is an error when trying to check the file.
Usage ¶
assertions.FileEmpty(t, "path/to/file")
Examples ¶
success: filepath.Join(testDataPath(),"empty_file") failure: filepath.Join(testDataPath(),"existing_file")
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"path/filepath"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestFileEmpty(t *testing.T)
success := assert.FileEmpty(t, filepath.Join(testDataPath(), "empty_file"))
fmt.Printf("success: %t\n", success)
}
func testDataPath() string {
return filepath.Join("..", "internal", "assertions", "testdata")
}
Output: success: true
func FileEmptyf ¶ added in v2.1.0
FileEmptyf is the same as FileEmpty, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func FileExists ¶
FileExists checks whether a file exists in the given path. It also fails if the path points to a directory or there is an error when trying to check the file.
Usage ¶
assertions.FileExists(t, "path/to/file")
Examples ¶
success: filepath.Join(testDataPath(),"existing_file") failure: filepath.Join(testDataPath(),"non_existing_file")
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"path/filepath"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestFileExists(t *testing.T)
success := assert.FileExists(t, filepath.Join(testDataPath(), "existing_file"))
fmt.Printf("success: %t\n", success)
}
func testDataPath() string {
return filepath.Join("..", "internal", "assertions", "testdata")
}
Output: success: true
func FileExistsf ¶
FileExistsf is the same as FileExists, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func FileNotEmpty ¶ added in v2.1.0
FileNotEmpty checks whether a file exists in the given path and is not empty. It fails if the file is empty, if the path points to a directory or there is an error when trying to check the file.
Usage ¶
assertions.FileNotEmpty(t, "path/to/file")
Examples ¶
success: filepath.Join(testDataPath(),"existing_file") failure: filepath.Join(testDataPath(),"empty_file")
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"path/filepath"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestFileNotEmpty(t *testing.T)
success := assert.FileNotEmpty(t, filepath.Join(testDataPath(), "existing_file"))
fmt.Printf("success: %t\n", success)
}
func testDataPath() string {
return filepath.Join("..", "internal", "assertions", "testdata")
}
Output: success: true
func FileNotEmptyf ¶ added in v2.1.0
FileNotEmptyf is the same as FileNotEmpty, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func FileNotExists ¶ added in v2.2.0
FileNotExists checks whether a file does not exist in a given path. It fails if the path points to an existing _file_ only.
Usage ¶
assertions.FileNotExists(t, "path/to/file")
Examples ¶
success: filepath.Join(testDataPath(),"non_existing_file") failure: filepath.Join(testDataPath(),"existing_file")
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"path/filepath"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestFileNotExists(t *testing.T)
success := assert.FileNotExists(t, filepath.Join(testDataPath(), "non_existing_file"))
fmt.Printf("success: %t\n", success)
}
func testDataPath() string {
return filepath.Join("..", "internal", "assertions", "testdata")
}
Output: success: true
func FileNotExistsf ¶ added in v2.2.0
FileNotExistsf is the same as FileNotExists, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Greater ¶
Greater asserts that the first element is strictly greater than the second.
Both elements must be of the same type in the reflect.Kind sense. To compare values that need a type conversion (e.g. float32 against float64), you need to convert types beforehand.
Usage ¶
assertions.Greater(t, 2, 1) assertions.Greater(t, float64(2), float64(1)) assertions.Greater(t, "b", "a")
Examples ¶
success: 2, 1 failure: 1, 2
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestGreater(t *testing.T)
success := assert.Greater(t, 2, 1)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func GreaterOrEqual ¶
GreaterOrEqual asserts that the first element is greater than or equal to the second.
See also Greater.
Usage ¶
assertions.GreaterOrEqual(t, 2, 1) assertions.GreaterOrEqual(t, 2, 2) assertions.GreaterOrEqual(t, "b", "a") assertions.GreaterOrEqual(t, "b", "b")
Examples ¶
success: 2, 1 failure: 1, 2
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestGreaterOrEqual(t *testing.T)
success := assert.GreaterOrEqual(t, 2, 1)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func GreaterOrEqualT ¶ added in v2.2.0
GreaterOrEqualT asserts that for two elements of the same type, the first element is greater than or equal to the second.
The Ordered type can be any of Go's cmp.Ordered (strings, numeric types), []byte (uses bytes.Compare) and time.Time (uses time.Time.Compare.
Notice that pointers are not Ordered, but uintptr are. So you can't call GreaterOrEqualT with *time.Time.
GreaterOrEqualT ensures type safety at build time. If you need to compare values with a dynamically assigned type, use GreaterOrEqual instead.
To compare values that need a type conversion (e.g. float32 against float64), you need to convert types beforehand.
Usage ¶
assertions.GreaterOrEqualT(t, 2, 1) assertions.GreaterOrEqualT(t, 2, 2) assertions.GreaterOrEqualT(t, "b", "a") assertions.GreaterOrEqualT(t, "b", "b")
Examples ¶
success: 2, 1 failure: 1, 2
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestGreaterOrEqualT(t *testing.T)
success := assert.GreaterOrEqualT(t, 2, 1)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func GreaterOrEqualTf ¶ added in v2.2.0
func GreaterOrEqualTf[Orderable Ordered](t T, e1 Orderable, e2 Orderable, msg string, args ...any) bool
GreaterOrEqualTf is the same as GreaterOrEqualT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func GreaterOrEqualf ¶
GreaterOrEqualf is the same as GreaterOrEqual, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func GreaterT ¶ added in v2.2.0
GreaterT asserts that for two elements of the same type, the first element is strictly greater than the second.
The Ordered type can be any of Go's cmp.Ordered (strings, numeric types), []byte (uses bytes.Compare) and time.Time (uses time.Time.Compare.
Notice that pointers are not Ordered, but uintptr are. So you can't call GreaterT with *time.Time.
GreaterT ensures type safety at build time. If you need to compare values with a dynamically assigned type, use Greater instead.
To compare values that need a type conversion (e.g. float32 against float64), you need to convert types beforehand.
Usage ¶
assertions.GreaterT(t, 2, 1) assertions.GreaterT(t, float64(2), float64(1)) assertions.GreaterT(t, "b", "a") assertions.GreaterT(t, time.Date(2026,1,1,0,0,0,0,nil), time.Now())
Examples ¶
success: 2, 1 failure: 1, 2
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestGreaterT(t *testing.T)
success := assert.GreaterT(t, 2, 1)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func GreaterTf ¶ added in v2.2.0
GreaterTf is the same as GreaterT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Greaterf ¶
Greaterf is the same as Greater, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func HTTPBody ¶
HTTPBody is a helper that returns the HTTP body of the response. It returns the empty string if building a new request fails.
func HTTPBodyContains ¶
func HTTPBodyContains(t T, handler http.HandlerFunc, method string, url string, values url.Values, str any, msgAndArgs ...any) bool
HTTPBodyContains asserts that a specified handler returns a body that contains a string.
Returns whether the assertion was successful (true) or not (false).
Usage ¶
assertions.HTTPBodyContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
Examples ¶
success: httpBody, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"
failure: httpBody, "GET", "/", url.Values{"name": []string{"Bob"}}, "Hello, World!"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"net/http"
"net/url"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestHTTPBodyContains(t *testing.T)
success := assert.HTTPBodyContains(t, httpBody, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!")
fmt.Printf("success: %t\n", success)
}
func httpBody(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
_, _ = fmt.Fprintf(w, "Hello, %s!", name)
}
Output: success: true
func HTTPBodyContainsf ¶
func HTTPBodyContainsf(t T, handler http.HandlerFunc, method string, url string, values url.Values, str any, msg string, args ...any) bool
HTTPBodyContainsf is the same as HTTPBodyContains, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func HTTPBodyNotContains ¶
func HTTPBodyNotContains(t T, handler http.HandlerFunc, method string, url string, values url.Values, str any, msgAndArgs ...any) bool
HTTPBodyNotContains asserts that a specified handler returns a body that does not contain a string.
Returns whether the assertion was successful (true) or not (false).
Usage ¶
assertions.HTTPBodyNotContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
Examples ¶
success: httpBody, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, Bob!"
failure: httpBody, "GET", "/", url.Values{"name": []string{"Bob"}}, "Hello, Bob!"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"net/http"
"net/url"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestHTTPBodyNotContains(t *testing.T)
success := assert.HTTPBodyNotContains(t, httpBody, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, Bob!")
fmt.Printf("success: %t\n", success)
}
func httpBody(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
_, _ = fmt.Fprintf(w, "Hello, %s!", name)
}
Output: success: true
func HTTPBodyNotContainsf ¶
func HTTPBodyNotContainsf(t T, handler http.HandlerFunc, method string, url string, values url.Values, str any, msg string, args ...any) bool
HTTPBodyNotContainsf is the same as HTTPBodyNotContains, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func HTTPError ¶
func HTTPError(t T, handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...any) bool
HTTPError asserts that a specified handler returns an error status code.
Returns whether the assertion was successful (true) or not (false).
Usage ¶
assertions.HTTPError(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}
Examples ¶
success: httpError, "GET", "/", nil failure: httpOK, "GET", "/", nil
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"net/http"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestHTTPError(t *testing.T)
success := assert.HTTPError(t, httpError, "GET", "/", nil)
fmt.Printf("success: %t\n", success)
}
func httpError(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}
Output: success: true
func HTTPErrorf ¶
func HTTPErrorf(t T, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...any) bool
HTTPErrorf is the same as HTTPError, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func HTTPRedirect ¶
func HTTPRedirect(t T, handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...any) bool
HTTPRedirect asserts that a specified handler returns a redirect status code.
Returns whether the assertion was successful (true) or not (false).
Usage ¶
assertions.HTTPRedirect(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}
Examples ¶
success: httpRedirect, "GET", "/", nil failure: httpError, "GET", "/", nil
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"net/http"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestHTTPRedirect(t *testing.T)
success := assert.HTTPRedirect(t, httpRedirect, "GET", "/", nil)
fmt.Printf("success: %t\n", success)
}
func httpRedirect(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusTemporaryRedirect)
}
Output: success: true
func HTTPRedirectf ¶
func HTTPRedirectf(t T, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...any) bool
HTTPRedirectf is the same as HTTPRedirect, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func HTTPStatusCode ¶
func HTTPStatusCode(t T, handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msgAndArgs ...any) bool
HTTPStatusCode asserts that a specified handler returns a specified status code.
Returns whether the assertion was successful (true) or not (false).
Usage ¶
assertions.HTTPStatusCode(t, myHandler, "GET", "/notImplemented", nil, 501)
Examples ¶
success: httpOK, "GET", "/", nil, http.StatusOK failure: httpError, "GET", "/", nil, http.StatusOK
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"net/http"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestHTTPStatusCode(t *testing.T)
success := assert.HTTPStatusCode(t, httpOK, "GET", "/", nil, http.StatusOK)
fmt.Printf("success: %t\n", success)
}
func httpOK(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}
Output: success: true
func HTTPStatusCodef ¶
func HTTPStatusCodef(t T, handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msg string, args ...any) bool
HTTPStatusCodef is the same as HTTPStatusCode, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func HTTPSuccess ¶
func HTTPSuccess(t T, handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...any) bool
HTTPSuccess asserts that a specified handler returns a success status code.
Returns whether the assertion was successful (true) or not (false).
Usage ¶
assertions.HTTPSuccess(t, myHandler, "POST", "http://www.google.com", nil)
Examples ¶
success: httpOK, "GET", "/", nil failure: httpError, "GET", "/", nil
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"net/http"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestHTTPSuccess(t *testing.T)
success := assert.HTTPSuccess(t, httpOK, "GET", "/", nil)
fmt.Printf("success: %t\n", success)
}
func httpOK(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}
Output: success: true
func HTTPSuccessf ¶
func HTTPSuccessf(t T, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...any) bool
HTTPSuccessf is the same as HTTPSuccess, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Implements ¶
Implements asserts that an object is implemented by the specified interface.
Usage ¶
assertions.Implements(t, (*MyInterface)(nil), new(MyObject))
Examples ¶
success: ptr(dummyInterface), new(testing.T) failure: (*error)(nil), new(testing.T)
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestImplements(t *testing.T)
success := assert.Implements(t, ptr(dummyInterface), new(testing.T))
fmt.Printf("success: %t\n", success)
}
//nolint:gochecknoglobals // this is on purpose to share a common pointer when testing
var (
staticVar = "static string"
dummyInterface assert.T
)
func ptr[T any](value T) *T {
p := value
return &p
}
Output: success: true
func Implementsf ¶
Implementsf is the same as Implements, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func InDelta ¶
InDelta asserts that the two numerals are within delta of each other.
Delta must be greater than or equal to zero.
Expected and actual values should convert to float64. To compare large integers that can't be represented accurately as float64 (e.g. uint64), prefer InDeltaT to preserve the original type.
Behavior with IEEE floating point arithmetic ¶
- expected NaN is matched only by a NaN, e.g. this works: InDeltaT(math.Sqrt(-1), math.Sqrt(-1), 0.0)
- expected +Inf is matched only by a +Inf
- expected -Inf is matched only by a -Inf
Usage ¶
assertions.InDelta(t, math.Pi, 22/7.0, 0.01)
Examples ¶
success: 1.0, 1.01, 0.02 failure: 1.0, 1.1, 0.05
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestInDelta(t *testing.T)
success := assert.InDelta(t, 1.0, 1.01, 0.02)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func InDeltaMapValues ¶
InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys.
See InDelta.
Usage ¶
assertions.InDeltaMapValues(t, map[string]float64{"a": 1.0}, map[string]float64{"a": 1.01}, 0.02)
Examples ¶
success: map[string]float64{"a": 1.0}, map[string]float64{"a": 1.01}, 0.02
failure: map[string]float64{"a": 1.0}, map[string]float64{"a": 1.1}, 0.05
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestInDeltaMapValues(t *testing.T)
success := assert.InDeltaMapValues(t, map[string]float64{"a": 1.0}, map[string]float64{"a": 1.01}, 0.02)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func InDeltaMapValuesf ¶
InDeltaMapValuesf is the same as InDeltaMapValues, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func InDeltaSlice ¶
InDeltaSlice is the same as InDelta, except it compares two slices.
See InDelta.
Usage ¶
assertions.InDeltaSlice(t, []float64{1.0, 2.0}, []float64{1.01, 2.01}, 0.02)
Examples ¶
success: []float64{1.0, 2.0}, []float64{1.01, 2.01}, 0.02
failure: []float64{1.0, 2.0}, []float64{1.1, 2.1}, 0.05
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestInDeltaSlice(t *testing.T)
success := assert.InDeltaSlice(t, []float64{1.0, 2.0}, []float64{1.01, 2.01}, 0.02)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func InDeltaSlicef ¶
InDeltaSlicef is the same as InDeltaSlice, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func InDeltaT ¶ added in v2.2.0
func InDeltaT[Number Measurable](t T, expected Number, actual Number, delta Number, msgAndArgs ...any) bool
InDeltaT asserts that the two numerals of the same type numerical type are within delta of each other.
InDeltaT accepts any go numeric type, including integer types.
The main difference with InDelta is that the delta is expressed with the same type as the values, not necessarily a float64.
Delta must be greater than or equal to zero.
Behavior with IEEE floating point arithmetic ¶
- expected NaN is matched only by a NaN, e.g. this works: InDeltaT(math.NaN(), math.Sqrt(-1), 0.0)
- expected +Inf is matched only by a +Inf
- expected -Inf is matched only by a -Inf
Usage ¶
assertions.InDeltaT(t, math.Pi, 22/7.0, 0.01)
Examples ¶
success: 1.0, 1.01, 0.02 failure: 1.0, 1.1, 0.05
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestInDeltaT(t *testing.T)
success := assert.InDeltaT(t, 1.0, 1.01, 0.02)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func InDeltaTf ¶ added in v2.2.0
func InDeltaTf[Number Measurable](t T, expected Number, actual Number, delta Number, msg string, args ...any) bool
InDeltaTf is the same as InDeltaT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func InDeltaf ¶
InDeltaf is the same as InDelta, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func InEpsilon ¶
InEpsilon asserts that expected and actual have a relative error less than epsilon.
Behavior with IEEE floating point arithmetic ¶
- expected NaN is matched only by a NaN, e.g. this works: InDeltaT(math.NaN(), math.Sqrt(-1), 0.0)
- expected +Inf is matched only by a +Inf
- expected -Inf is matched only by a -Inf
Edge case: for very large integers that do not convert accurately to a float64 (e.g. uint64), prefer InDeltaT.
Formula:
- If expected == 0: fail if |actual - expected| > epsilon
- If expected != 0: fail if |actual - expected| > epsilon * |expected|
This allows InEpsilonT to work naturally across the full numeric range including zero.
Usage ¶
assertions.InEpsilon(t, 100.0, 101.0, 0.02)
Examples ¶
success: 100.0, 101.0, 0.02 failure: 100.0, 110.0, 0.05
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestInEpsilon(t *testing.T)
success := assert.InEpsilon(t, 100.0, 101.0, 0.02)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func InEpsilonSlice ¶
InEpsilonSlice is the same as InEpsilon, except it compares each value from two slices.
See InEpsilon.
Usage ¶
assertions.InEpsilonSlice(t, []float64{100.0, 200.0}, []float64{101.0, 202.0}, 0.02)
Examples ¶
success: []float64{100.0, 200.0}, []float64{101.0, 202.0}, 0.02
failure: []float64{100.0, 200.0}, []float64{110.0, 220.0}, 0.05
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestInEpsilonSlice(t *testing.T)
success := assert.InEpsilonSlice(t, []float64{100.0, 200.0}, []float64{101.0, 202.0}, 0.02)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func InEpsilonSlicef ¶
InEpsilonSlicef is the same as InEpsilonSlice, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func InEpsilonT ¶ added in v2.2.0
func InEpsilonT[Number Measurable](t T, expected Number, actual Number, epsilon float64, msgAndArgs ...any) bool
InEpsilonT asserts that expected and actual have a relative error less than epsilon.
When expected is zero, epsilon is interpreted as an absolute error threshold, since relative error is mathematically undefined for zero values.
Unlike InDeltaT, which preserves the original type, InEpsilonT converts the expected and actual numbers to float64, since the relative error doesn't make sense as an integer.
Behavior with IEEE floating point arithmetic ¶
- expected NaN is matched only by a NaN, e.g. this works: InDeltaT(math.NaN(), math.Sqrt(-1), 0.0)
- expected +Inf is matched only by a +Inf
- expected -Inf is matched only by a -Inf
Edge case: for very large integers that do not convert accurately to a float64 (e.g. uint64), prefer InDeltaT.
Formula:
- If expected == 0: fail if |actual - expected| > epsilon
- If expected != 0: fail if |actual - expected| > epsilon * |expected|
This allows InEpsilonT to work naturally across the full numeric range including zero.
Usage ¶
assertions.InEpsilon(t, 100.0, 101.0, 0.02)
Examples ¶
success: 100.0, 101.0, 0.02 failure: 100.0, 110.0, 0.05
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestInEpsilonT(t *testing.T)
success := assert.InEpsilonT(t, 100.0, 101.0, 0.02)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func InEpsilonTf ¶ added in v2.2.0
func InEpsilonTf[Number Measurable](t T, expected Number, actual Number, epsilon float64, msg string, args ...any) bool
InEpsilonTf is the same as InEpsilonT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func InEpsilonf ¶
InEpsilonf is the same as InEpsilon, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func IsDecreasing ¶
IsDecreasing asserts that the collection is strictly decreasing.
Usage ¶
assertions.IsDecreasing(t, []int{2, 1, 0})
assertions.IsDecreasing(t, []float{2, 1})
assertions.IsDecreasing(t, []string{"b", "a"})
Examples ¶
success: []int{3, 2, 1}
failure: []int{1, 2, 3}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestIsDecreasing(t *testing.T)
success := assert.IsDecreasing(t, []int{3, 2, 1})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func IsDecreasingT ¶ added in v2.2.0
func IsDecreasingT[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msgAndArgs ...any) bool
IsDecreasingT asserts that a slice of Ordered is strictly decreasing.
Usage ¶
assertions.IsDecreasingT(t, []int{2, 1, 0})
assertions.IsDecreasingT(t, []float{2, 1})
assertions.IsDecreasingT(t, []string{"b", "a"})
Examples ¶
success: []int{3, 2, 1}
failure: []int{1, 2, 3}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestIsDecreasingT(t *testing.T)
success := assert.IsDecreasingT(t, []int{3, 2, 1})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func IsDecreasingTf ¶ added in v2.2.0
func IsDecreasingTf[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msg string, args ...any) bool
IsDecreasingTf is the same as IsDecreasingT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func IsDecreasingf ¶
IsDecreasingf is the same as IsDecreasing, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func IsIncreasing ¶
IsIncreasing asserts that the collection is strictly increasing.
Usage ¶
assertions.IsIncreasing(t, []int{1, 2, 3})
assertions.IsIncreasing(t, []float{1, 2})
assertions.IsIncreasing(t, []string{"a", "b"})
Examples ¶
success: []int{1, 2, 3}
failure: []int{1, 1, 2}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestIsIncreasing(t *testing.T)
success := assert.IsIncreasing(t, []int{1, 2, 3})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func IsIncreasingT ¶ added in v2.2.0
func IsIncreasingT[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msgAndArgs ...any) bool
IsIncreasingT asserts that a slice of Ordered is strictly increasing.
Usage ¶
assertions.IsIncreasingT(t, []int{1, 2, 3})
assertions.IsIncreasingT(t, []float{1, 2})
assertions.IsIncreasingT(t, []string{"a", "b"})
Examples ¶
success: []int{1, 2, 3}
failure: []int{1, 1, 2}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestIsIncreasingT(t *testing.T)
success := assert.IsIncreasingT(t, []int{1, 2, 3})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func IsIncreasingTf ¶ added in v2.2.0
func IsIncreasingTf[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msg string, args ...any) bool
IsIncreasingTf is the same as IsIncreasingT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func IsIncreasingf ¶
IsIncreasingf is the same as IsIncreasing, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func IsNonDecreasing ¶
IsNonDecreasing asserts that the collection is not strictly decreasing.
Usage ¶
assertions.IsNonDecreasing(t, []int{1, 1, 2})
assertions.IsNonDecreasing(t, []float{1, 2})
assertions.IsNonDecreasing(t, []string{"a", "b"})
Examples ¶
success: []int{1, 1, 2}
failure: []int{2, 1, 0}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestIsNonDecreasing(t *testing.T)
success := assert.IsNonDecreasing(t, []int{1, 1, 2})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func IsNonDecreasingT ¶ added in v2.2.0
func IsNonDecreasingT[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msgAndArgs ...any) bool
IsNonDecreasingT asserts that a slice of Ordered is not decreasing.
Usage ¶
assertions.IsNonDecreasingT(t, []int{1, 1, 2})
assertions.IsNonDecreasingT(t, []float{1, 2})
assertions.IsNonDecreasingT(t, []string{"a", "b"})
Examples ¶
success: []int{1, 1, 2}
failure: []int{2, 1, 0}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestIsNonDecreasingT(t *testing.T)
success := assert.IsNonDecreasingT(t, []int{1, 1, 2})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func IsNonDecreasingTf ¶ added in v2.2.0
func IsNonDecreasingTf[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msg string, args ...any) bool
IsNonDecreasingTf is the same as IsNonDecreasingT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func IsNonDecreasingf ¶
IsNonDecreasingf is the same as IsNonDecreasing, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func IsNonIncreasing ¶
IsNonIncreasing asserts that the collection is not increasing.
Usage ¶
assertions.IsNonIncreasing(t, []int{2, 1, 1})
assertions.IsNonIncreasing(t, []float{2, 1})
assertions.IsNonIncreasing(t, []string{"b", "a"})
Examples ¶
success: []int{2, 1, 1}
failure: []int{1, 2, 3}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestIsNonIncreasing(t *testing.T)
success := assert.IsNonIncreasing(t, []int{2, 1, 1})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func IsNonIncreasingT ¶ added in v2.2.0
func IsNonIncreasingT[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msgAndArgs ...any) bool
IsNonIncreasingT asserts that a slice of Ordered is NOT strictly increasing.
Usage ¶
assertions.IsNonIncreasing(t, []int{2, 1, 1})
assertions.IsNonIncreasing(t, []float{2, 1})
assertions.IsNonIncreasing(t, []string{"b", "a"})
Examples ¶
success: []int{2, 1, 1}
failure: []int{1, 2, 3}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestIsNonIncreasingT(t *testing.T)
success := assert.IsNonIncreasingT(t, []int{2, 1, 1})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func IsNonIncreasingTf ¶ added in v2.2.0
func IsNonIncreasingTf[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msg string, args ...any) bool
IsNonIncreasingTf is the same as IsNonIncreasingT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func IsNonIncreasingf ¶
IsNonIncreasingf is the same as IsNonIncreasing, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func IsNotOfTypeT ¶ added in v2.2.0
IsNotOfTypeT asserts that an object is not of a given type.
Usage ¶
assertions.IsOfType[MyType](t,myVar)
Examples ¶
success: 123.123 failure: myType(123.123)
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestIsNotOfTypeT(t *testing.T)
success := assert.IsNotOfTypeT[myType](t, 123.123)
fmt.Printf("success: %t\n", success)
}
type myType float64
Output: success: true
func IsNotOfTypeTf ¶ added in v2.2.0
IsNotOfTypeTf is the same as IsNotOfTypeT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func IsNotType ¶
IsNotType asserts that the specified objects are not of the same type.
Usage ¶
assertions.IsNotType(t, &NotMyStruct{}, &MyStruct{})
Examples ¶
success: int32(123), int64(456) failure: 123, 456
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestIsNotType(t *testing.T)
success := assert.IsNotType(t, int32(123), int64(456))
fmt.Printf("success: %t\n", success)
}
Output: success: true
func IsNotTypef ¶
IsNotTypef is the same as IsNotType, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func IsOfTypeT ¶ added in v2.2.0
IsOfTypeT asserts that an object is of a given type.
Usage ¶
assertions.IsOfTypeT[MyType](t,myVar)
Examples ¶
success: myType(123.123) failure: 123.123
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestIsOfTypeT(t *testing.T)
success := assert.IsOfTypeT[myType](t, myType(123.123))
fmt.Printf("success: %t\n", success)
}
type myType float64
Output: success: true
func IsOfTypeTf ¶ added in v2.2.0
IsOfTypeTf is the same as IsOfTypeT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func IsType ¶
IsType asserts that the specified objects are of the same type.
Usage ¶
assertions.IsType(t, &MyStruct{}, &MyStruct{})
Examples ¶
success: 123, 456 failure: int32(123), int64(456)
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestIsType(t *testing.T)
success := assert.IsType(t, 123, 456)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func IsTypef ¶
IsTypef is the same as IsType, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func JSONEq ¶
JSONEq asserts that two JSON strings are semantically equivalent.
Expected and actual must be valid JSON.
Usage ¶
assertions.JSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
Examples ¶
success: `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`
failure: `{"hello": "world", "foo": "bar"}`, `[{"foo": "bar"}, {"hello": "world"}]`
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestJSONEq(t *testing.T)
success := assert.JSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func JSONEqBytes ¶ added in v2.0.2
JSONEqBytes asserts that two JSON slices of bytes are semantically equivalent.
Expected and actual must be valid JSON.
Usage ¶
assertions.JSONEqBytes(t, []byte(`{"hello": "world", "foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`))
Examples ¶
success: []byte(`{"hello": "world", "foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`)
failure: []byte(`{"hello": "world", "foo": "bar"}`), []byte(`[{"foo": "bar"}, {"hello": "world"}]`)
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestJSONEqBytes(t *testing.T)
success := assert.JSONEqBytes(t, []byte(`{"hello": "world", "foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`))
fmt.Printf("success: %t\n", success)
}
Output: success: true
func JSONEqBytesf ¶ added in v2.0.2
JSONEqBytesf is the same as JSONEqBytes, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func JSONEqT ¶ added in v2.2.0
JSONEqT asserts that two JSON documents are semantically equivalent.
The expected and actual arguments may be string or []byte. They do not need to be of the same type.
Expected and actual must be valid JSON.
Usage ¶
assertions.JSONEqT(t, `{"hello": "world", "foo": "bar"}`, []byte(`{"foo": "bar", "hello": "world"}`))
Examples ¶
success: `{"hello": "world", "foo": "bar"}`, []byte(`{"foo": "bar", "hello": "world"}`)
failure: `{"hello": "world", "foo": "bar"}`, `[{"foo": "bar"}, {"hello": "world"}]`
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestJSONEqT(t *testing.T)
success := assert.JSONEqT(t, `{"hello": "world", "foo": "bar"}`, []byte(`{"foo": "bar", "hello": "world"}`))
fmt.Printf("success: %t\n", success)
}
Output: success: true
func JSONEqTf ¶ added in v2.2.0
JSONEqTf is the same as JSONEqT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func JSONEqf ¶
JSONEqf is the same as JSONEq, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func JSONMarshalAsT ¶ added in v2.3.0
JSONMarshalAsT wraps JSONEq after [json.Marshal].
The input JSON may be a string or []byte.
It fails if the marshaling returns an error or if the expected JSON bytes differ semantically from the expected ones.
Usage ¶
actual := struct {
A int `json:"a"`
}{
A: 10,
}
assertions.JSONUnmarshalAsT(t,expected, `{"a": 10}`)
Examples ¶
success: []byte(`{"A": "a"}`), dummyStruct{A: "a"}
failure: `[{"foo": "bar"}, {"hello": "world"}]`, 1
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestJSONMarshalAsT(t *testing.T)
success := assert.JSONMarshalAsT(t, []byte(`{"A": "a"}`), dummyStruct{A: "a"})
fmt.Printf("success: %t\n", success)
}
type dummyStruct struct {
A string
b int
}
Output: success: true
func JSONMarshalAsTf ¶ added in v2.3.0
JSONMarshalAsTf is the same as JSONMarshalAsT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func JSONUnmarshalAsT ¶ added in v2.3.0
func JSONUnmarshalAsT[Object any, ADoc Text](t T, expected Object, jazon ADoc, msgAndArgs ...any) bool
JSONUnmarshalAsT wraps Equal after [json.Unmarshal].
The input JSON may be a string or []byte.
It fails if the unmarshaling returns an error or if the resulting object is not equal to the expected one.
Be careful not to wrap the expected object into an "any" interface if this is not what you expected: the unmarshaling would take this type to unmarshal as a map[string]any.
Usage ¶
expected := struct {
A int `json:"a"`
}{
A: 10,
}
assertions.JSONUnmarshalAsT(t,expected, `{"a": 10}`)
Examples ¶
success: dummyStruct{A: "a"} , []byte(`{"A": "a"}`)
failure: 1, `[{"foo": "bar"}, {"hello": "world"}]`
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestJSONUnmarshalAsT(t *testing.T)
success := assert.JSONUnmarshalAsT(t, dummyStruct{A: "a"}, []byte(`{"A": "a"}`))
fmt.Printf("success: %t\n", success)
}
type dummyStruct struct {
A string
b int
}
Output: success: true
func JSONUnmarshalAsTf ¶ added in v2.3.0
func JSONUnmarshalAsTf[Object any, ADoc Text](t T, expected Object, jazon ADoc, msg string, args ...any) bool
JSONUnmarshalAsTf is the same as JSONUnmarshalAsT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Kind ¶ added in v2.2.0
Kind asserts that the reflect.Kind of a given object matches the expected reflect.Kind.
Kind reflects the concrete value stored in the object. The nil value (or interface with nil value) are comparable to reflect.Invalid. See also reflect.Value.Kind.
Usage ¶
assertions.Kind(t, reflect.String, "Hello World")
Examples ¶
success: reflect.String, "hello" failure: reflect.String, 0
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"reflect"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestKind(t *testing.T)
success := assert.Kind(t, reflect.String, "hello")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func Kindf ¶ added in v2.2.0
Kindf is the same as Kind, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Len ¶
Len asserts that the specified object has specific length.
Len also fails if the object has a type that len() does not accept.
The asserted object can be a string, a slice, a map, an array, pointer to array or a channel.
Usage ¶
assertions.Len(t, mySlice, 3) assertions.Len(t, myString, 4) assertions.Len(t, myMap, 5)
Examples ¶
success: []string{"A","B"}, 2
failure: []string{"A","B"}, 1
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestLen(t *testing.T)
success := assert.Len(t, []string{"A", "B"}, 2)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func Lenf ¶
Lenf is the same as Len, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Less ¶
Less asserts that the first element is strictly less than the second.
Both elements must be of the same type in the reflect.Kind sense. To compare values that need a type conversion (e.g. float32 against float64), you need to convert types beforehand.
Usage ¶
assertions.Less(t, 1, 2) assertions.Less(t, float64(1), float64(2)) assertions.Less(t, "a", "b")
Examples ¶
success: 1, 2 failure: 2, 1
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestLess(t *testing.T)
success := assert.Less(t, 1, 2)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func LessOrEqual ¶
LessOrEqual asserts that the first element is less than or equal to the second.
Usage ¶
assertions.LessOrEqual(t, 1, 2) assertions.LessOrEqual(t, 2, 2) assertions.LessOrEqual(t, "a", "b") assertions.LessOrEqual(t, "b", "b")
Examples ¶
success: 1, 2 failure: 2, 1
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestLessOrEqual(t *testing.T)
success := assert.LessOrEqual(t, 1, 2)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func LessOrEqualT ¶ added in v2.2.0
LessOrEqualT asserts that for two elements of the same type, the first element is less than or equal to the second.
The Ordered type can be any of Go's cmp.Ordered (strings, numeric types), []byte (uses bytes.Compare) and time.Time (uses time.Time.Compare.
Notice that pointers are not Ordered, but uintptr are. So you can't call LessOrEqualT with *time.Time.
LessOrEqualT ensures type safety at build time. If you need to compare values with a dynamically assigned type, use LessOrEqual instead.
To compare values that need a type conversion (e.g. float32 against float64), you should use LessOrEqual instead.
Usage ¶
assertions.LessOrEqualT(t, 1, 2) assertions.LessOrEqualT(t, 2, 2) assertions.LessOrEqualT(t, "a", "b") assertions.LessOrEqualT(t, "b", "b")
Examples ¶
success: 1, 2 failure: 2, 1
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestLessOrEqualT(t *testing.T)
success := assert.LessOrEqualT(t, 1, 2)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func LessOrEqualTf ¶ added in v2.2.0
func LessOrEqualTf[Orderable Ordered](t T, e1 Orderable, e2 Orderable, msg string, args ...any) bool
LessOrEqualTf is the same as LessOrEqualT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func LessOrEqualf ¶
LessOrEqualf is the same as LessOrEqual, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func LessT ¶ added in v2.2.0
LessT asserts that for two elements of the same type, the first element is strictly less than the second.
The Ordered type can be any of Go's cmp.Ordered (strings, numeric types), []byte (uses bytes.Compare) and time.Time (uses time.Time.Compare.
Notice that pointers are not Ordered, but uintptr are. So you can't call LessT with *time.Time.
LessT ensures type safety at build time. If you need to compare values with a dynamically assigned type, use Less instead.
To compare values that need a type conversion (e.g. float32 against float64), you need to convert types beforehand.
Usage ¶
assertions.LessT(t, 1, 2) assertions.LessT(t, float64(1), float64(2)) assertions.LessT(t, "a", "b")
Examples ¶
success: 1, 2 failure: 2, 1
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestLessT(t *testing.T)
success := assert.LessT(t, 1, 2)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func LessTf ¶ added in v2.2.0
LessTf is the same as LessT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Lessf ¶
Lessf is the same as Less, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func MapContainsT ¶ added in v2.2.0
func MapContainsT[Map ~map[K]V, K comparable, V any](t T, m Map, key K, msgAndArgs ...any) bool
MapContainsT asserts that the specified map contains a key.
Go native comparable types are explained there: comparable-types.
Usage ¶
assertions.MapContainsT(t, map[string]string{"Hello": "x","World": "y"}, "World")
Examples ¶
success: map[string]string{"A": "B"}, "A"
failure: map[string]string{"A": "B"}, "C"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestMapContainsT(t *testing.T)
success := assert.MapContainsT(t, map[string]string{"A": "B"}, "A")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func MapContainsTf ¶ added in v2.2.0
func MapContainsTf[Map ~map[K]V, K comparable, V any](t T, m Map, key K, msg string, args ...any) bool
MapContainsTf is the same as MapContainsT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func MapNotContainsT ¶ added in v2.2.0
func MapNotContainsT[Map ~map[K]V, K comparable, V any](t T, m Map, key K, msgAndArgs ...any) bool
MapNotContainsT asserts that the specified map does not contain a key.
Usage ¶
assertions.MapNotContainsT(t, map[string]string{"Hello": "x","World": "y"}, "hi")
Examples ¶
success: map[string]string{"A": "B"}, "C"
failure: map[string]string{"A": "B"}, "A"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestMapNotContainsT(t *testing.T)
success := assert.MapNotContainsT(t, map[string]string{"A": "B"}, "C")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func MapNotContainsTf ¶ added in v2.2.0
func MapNotContainsTf[Map ~map[K]V, K comparable, V any](t T, m Map, key K, msg string, args ...any) bool
MapNotContainsTf is the same as MapNotContainsT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Negative ¶
Negative asserts that the specified element is strictly negative.
Usage ¶
assertions.Negative(t, -1) assertions.Negative(t, -1.23)
Examples ¶
success: -1 failure: 1
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNegative(t *testing.T)
success := assert.Negative(t, -1)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NegativeT ¶ added in v2.2.0
func NegativeT[SignedNumber SignedNumeric](t T, e SignedNumber, msgAndArgs ...any) bool
NegativeT asserts that the specified element of a signed numeric type is strictly negative.
Usage ¶
assertions.NegativeT(t, -1) assertions.NegativeT(t, -1.23)
Examples ¶
success: -1 failure: 1
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNegativeT(t *testing.T)
success := assert.NegativeT(t, -1)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NegativeTf ¶ added in v2.2.0
func NegativeTf[SignedNumber SignedNumeric](t T, e SignedNumber, msg string, args ...any) bool
NegativeTf is the same as NegativeT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Negativef ¶
Negativef is the same as Negative, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Never ¶
func Never(t T, condition func() bool, timeout time.Duration, tick time.Duration, msgAndArgs ...any) bool
Never asserts that the given condition is never satisfied until timeout, periodically checking the target function at each tick.
Never is the opposite of Eventually ("at least once"). It succeeds if the timeout is reached without the condition ever returning true.
If the parent context is cancelled before the timeout, Never fails.
Usage ¶
assertions.Never(t, func() bool { return false }, time.Second, 10*time.Millisecond)
See also Eventually for details about using context and concurrency.
Alternative condition signature ¶
The simplest form of condition is:
func() bool
Use Consistently instead if you want to use a condition returning an error.
Concurrency ¶
See Eventually.
Attention point ¶
See Eventually.
Examples ¶
success: func() bool { return false }, 100*time.Millisecond, 20*time.Millisecond
failure: func() bool { return true }, 100*time.Millisecond, 20*time.Millisecond
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"time"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNever(t *testing.T)
success := assert.Never(t, func() bool {
return false
}, 100*time.Millisecond, 20*time.Millisecond)
fmt.Printf("success: %t\n", success)
}
Output: success: true
Example (NoSpuriousEvents) ¶
ExampleNever_noSpuriousEvents demonstrates asserting that a condition never becomes true during the observation period.
package main
import (
"fmt"
"testing"
"time"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // normally provided by test
// A channel that should remain empty during the test.
events := make(chan struct{}, 1)
result := assert.Never(t, func() bool {
select {
case <-events:
return true // event received = condition becomes true = Never fails
default:
return false
}
}, 100*time.Millisecond, 10*time.Millisecond)
fmt.Printf("never received: %t", result)
}
Output: never received: true
func Neverf ¶
func Neverf(t T, condition func() bool, timeout time.Duration, tick time.Duration, msg string, args ...any) bool
Neverf is the same as Never, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Nil ¶
Nil asserts that the specified object is nil.
Usage ¶
assertions.Nil(t, err)
Examples ¶
success: nil failure: "not nil"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNil(t *testing.T)
success := assert.Nil(t, nil)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func Nilf ¶
Nilf is the same as Nil, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NoError ¶
NoError asserts that a function returned a nil error (i.e. no error).
Usage ¶
actualObj, err := SomeFunction()
if assert.NoError(t, err) {
assertions.Equal(t, expectedObj, actualObj)
}
Examples ¶
success: nil failure: ErrTest
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNoError(t *testing.T)
success := assert.NoError(t, nil)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NoErrorf ¶
NoErrorf is the same as NoError, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NoFileDescriptorLeak ¶ added in v2.4.0
NoFileDescriptorLeak ensures that no file descriptor leaks from inside the tested function.
This assertion works on Linux only (via /proc/self/fd). On other platforms, the test is skipped.
NOTE: this assertion is not compatible with parallel tests. File descriptors are a process-wide resource; concurrent tests opening files would cause false positives.
Sockets, pipes, and anonymous inodes are filtered out by default, as these are typically managed by the Go runtime.
Concurrency ¶
NoFileDescriptorLeak is not compatible with parallel tests. File descriptors are a process-wide resource; any concurrent I/O from other goroutines may cause false positives.
Calls to NoFileDescriptorLeak are serialized with a mutex to prevent multiple leak checks from interfering with each other.
Usage ¶
NoFileDescriptorLeak(t, func() {
// code that should not leak file descriptors
})
Examples ¶
success: func() {}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"runtime"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
if runtime.GOOS != "linux" {
// This example is only runnable on linux. On other platforms, the assertion skips the test.
// We force the expected output below, so that tests don't fail on other platforms.
fmt.Println("success: true")
return
}
t := new(testing.T) // should come from testing, e.g. func TestNoFileDescriptorLeak(t *testing.T)
success := assert.NoFileDescriptorLeak(t, func() {
})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NoFileDescriptorLeakf ¶ added in v2.4.0
NoFileDescriptorLeakf is the same as NoFileDescriptorLeak, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NoGoRoutineLeak ¶ added in v2.3.0
NoGoRoutineLeak ensures that no goroutine did leak from inside the tested function.
NOTE: only the go routines spawned from inside the tested function are checked for leaks. No filter or configuration is needed to exclude "known go routines".
Resource cleanup should be done inside the tested function, and not using testing.T.Cleanup, as t.Cleanup is called after the leak check.
Edge cases ¶
- if the tested function panics leaving behind leaked goroutines, these are detected.
- if the tested function calls runtime.Goexit (e.g. from testing.T.FailNow) leaving behind leaked goroutines, these are detected.
- if a panic occurs in one of the leaked go routines, it cannot be recovered with certainty and the calling program will usually panic.
Concurrency ¶
NoGoRoutineLeak may be used safely in parallel tests.
Usage ¶
NoGoRoutineLeak(t, func() {
...
},
"should not leak any go routine",
)
Examples ¶
success: func() {}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNoGoRoutineLeak(t *testing.T)
success := assert.NoGoRoutineLeak(t, func() {
})
fmt.Printf("success: %t\n", success)
}
Output: success: true
Example (Fail) ¶
package main
import (
"fmt"
"sync"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // normally provided by test
blocker := make(chan struct{})
var wg sync.WaitGroup
defer func() {
// clean resources _after_ the test
close(blocker)
wg.Wait()
}()
wg.Add(1)
// This examplifies how a function that leaks a goroutine is detected.
result := assert.NoGoRoutineLeak(t, func() { // true when there is no leak
go func() {
defer wg.Done()
<-blocker // leaked: blocks until cleanup
}()
})
// Error message from test would typically return the leaked goroutine, e.g.:
// # 0x69c8e8 github.com/go-openapi/testify/v2/assert_test.ExampleNoGoRoutineLeak.func2.1+0x48 .../assert_adhoc_example_7_test.go:30
fmt.Printf("has leak: %t", !result)
}
Output: has leak: true
func NoGoRoutineLeakf ¶ added in v2.3.0
NoGoRoutineLeakf is the same as NoGoRoutineLeak, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotContains ¶
NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the specified substring or element.
Usage ¶
assertions.NotContains(t, "Hello World", "Earth")
assertions.NotContains(t, ["Hello", "World"], "Earth")
assertions.NotContains(t, {"Hello": "World"}, "Earth")
Examples ¶
success: []string{"A","B"}, "C"
failure: []string{"A","B"}, "B"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotContains(t *testing.T)
success := assert.NotContains(t, []string{"A", "B"}, "C")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NotContainsf ¶
NotContainsf is the same as NotContains, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotElementsMatch ¶
NotElementsMatch asserts that the specified listA(array, slice...) is NOT equal to specified listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, the number of appearances of each of them in both lists should not match. This is an inverse of ElementsMatch.
Usage ¶
assertions.NotElementsMatch(t, []int{1, 1, 2, 3}, []int{1, 1, 2, 3}) -> false
assertions.NotElementsMatch(t, []int{1, 1, 2, 3}, []int{1, 2, 3}) -> true
assertions.NotElementsMatch(t, []int{1, 2, 3}, []int{1, 2, 4}) -> true
Examples ¶
success: []int{1, 2, 3}, []int{1, 2, 4}
failure: []int{1, 3, 2, 3}, []int{1, 3, 3, 2}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotElementsMatch(t *testing.T)
success := assert.NotElementsMatch(t, []int{1, 2, 3}, []int{1, 2, 4})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NotElementsMatchT ¶ added in v2.2.0
func NotElementsMatchT[E comparable](t T, listA []E, listB []E, msgAndArgs ...any) (ok bool)
NotElementsMatchT asserts that the specified listA(array, slice...) is NOT equal to specified listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, the number of appearances of each of them in both lists should not match. This is an inverse of ElementsMatch.
Usage ¶
assertions.NotElementsMatchT(t, []int{1, 1, 2, 3}, []int{1, 1, 2, 3}) -> false
assertions.NotElementsMatchT(t, []int{1, 1, 2, 3}, []int{1, 2, 3}) -> true
assertions.NotElementsMatchT(t, []int{1, 2, 3}, []int{1, 2, 4}) -> true
Examples ¶
success: []int{1, 2, 3}, []int{1, 2, 4}
failure: []int{1, 3, 2, 3}, []int{1, 3, 3, 2}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotElementsMatchT(t *testing.T)
success := assert.NotElementsMatchT(t, []int{1, 2, 3}, []int{1, 2, 4})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NotElementsMatchTf ¶ added in v2.2.0
func NotElementsMatchTf[E comparable](t T, listA []E, listB []E, msg string, args ...any) (ok bool)
NotElementsMatchTf is the same as NotElementsMatchT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotElementsMatchf ¶
NotElementsMatchf is the same as NotElementsMatch, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotEmpty ¶
NotEmpty asserts that the specified object is NOT Empty.
Usage ¶
if assert.NotEmpty(t, obj) {
assertions.Equal(t, "two", obj[1])
}
Examples ¶
success: "not empty" failure: ""
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotEmpty(t *testing.T)
success := assert.NotEmpty(t, "not empty")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NotEmptyf ¶
NotEmptyf is the same as NotEmpty, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotEqual ¶
NotEqual asserts that the specified values are NOT equal.
Usage ¶
assertions.NotEqual(t, obj1, obj2)
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.
Examples ¶
success: 123, 456 failure: 123, 123
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotEqual(t *testing.T)
success := assert.NotEqual(t, 123, 456)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NotEqualT ¶ added in v2.2.0
func NotEqualT[V comparable](t T, expected V, actual V, msgAndArgs ...any) bool
NotEqualT asserts that the specified values of the same comparable type are NOT equal.
See EqualT.
Usage ¶
assertions.NotEqualT(t, obj1, obj2)
Examples ¶
success: 123, 456 failure: 123, 123
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotEqualT(t *testing.T)
success := assert.NotEqualT(t, 123, 456)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NotEqualTf ¶ added in v2.2.0
func NotEqualTf[V comparable](t T, expected V, actual V, msg string, args ...any) bool
NotEqualTf is the same as NotEqualT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotEqualValues ¶
NotEqualValues asserts that two objects are not equal even when converted to the same type.
Function equality cannot be determined and will always fail.
Usage ¶
assertions.NotEqualValues(t, obj1, obj2)
Examples ¶
success: uint32(123), int32(456) failure: uint32(123), int32(123)
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotEqualValues(t *testing.T)
success := assert.NotEqualValues(t, uint32(123), int32(456))
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NotEqualValuesf ¶
NotEqualValuesf is the same as NotEqualValues, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotEqualf ¶
NotEqualf is the same as NotEqual, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotErrorAs ¶
NotErrorAs asserts that none of the errors in err's chain matches target, but if so, sets target to that error value.
Usage ¶
assertions.NotErrorAs(t, err, &target)
Examples ¶
success: ErrTest, new(*dummyError)
failure: fmt.Errorf("wrap: %w", &dummyError{}), new(*dummyError)
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotErrorAs(t *testing.T)
success := assert.NotErrorAs(t, assert.ErrTest, new(*dummyError))
fmt.Printf("success: %t\n", success)
}
type dummyError struct {
}
func (d *dummyError) Error() string {
return "dummy error"
}
Output: success: true
func NotErrorAsf ¶
NotErrorAsf is the same as NotErrorAs, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotErrorIs ¶
NotErrorIs asserts that none of the errors in err's chain matches target.
This is a wrapper for errors.Is.
Usage ¶
assertions.NotErrorIs(t, err, io.EOF)
Examples ¶
success: ErrTest, io.EOF
failure: fmt.Errorf("wrap: %w", io.EOF), io.EOF
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"io"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotErrorIs(t *testing.T)
success := assert.NotErrorIs(t, assert.ErrTest, io.EOF)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NotErrorIsf ¶
NotErrorIsf is the same as NotErrorIs, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotImplements ¶
NotImplements asserts that an object does not implement the specified interface.
Usage ¶
assertions.NotImplements(t, (*MyInterface)(nil), new(MyObject))
Examples ¶
success: (*error)(nil), new(testing.T) failure: ptr(dummyInterface), new(testing.T)
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotImplements(t *testing.T)
success := assert.NotImplements(t, (*error)(nil), new(testing.T))
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NotImplementsf ¶
NotImplementsf is the same as NotImplements, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotKind ¶ added in v2.2.0
NotKind asserts that the reflect.Kind of a given object does not match the expected reflect.Kind.
Kind reflects the concrete value stored in the object. The nil value (or interface with nil value) are comparable to reflect.Invalid. See also reflect.Value.Kind.
Usage ¶
assertions.NotKind(t, reflect.Int, "Hello World")
Examples ¶
success: reflect.String, 0 failure: reflect.String, "hello"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"reflect"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotKind(t *testing.T)
success := assert.NotKind(t, reflect.String, 0)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NotKindf ¶ added in v2.2.0
NotKindf is the same as NotKind, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotNil ¶
NotNil asserts that the specified object is not nil.
Usage ¶
assertions.NotNil(t, err)
Examples ¶
success: "not nil" failure: nil
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotNil(t *testing.T)
success := assert.NotNil(t, "not nil")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NotNilf ¶
NotNilf is the same as NotNil, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotPanics ¶
NotPanics asserts that the code inside the specified function does NOT panic.
Usage ¶
assertions.NotPanics(t, func(){ RemainCalm() })
Examples ¶
success: func() { }
failure: func() { panic("panicking") }
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotPanics(t *testing.T)
success := assert.NotPanics(t, func() {
})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NotPanicsf ¶
NotPanicsf is the same as NotPanics, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotRegexp ¶
NotRegexp asserts that a specified regular expression does not match a string.
See Regexp.
Usage ¶
assertions.NotRegexp(t, regexp.MustCompile("starts"), "it's starting")
assertions.NotRegexp(t, "^start", "it's not starting")
Examples ¶
success: "^start", "not starting" failure: "^start", "starting"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotRegexp(t *testing.T)
success := assert.NotRegexp(t, "^start", "not starting")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NotRegexpT ¶ added in v2.2.0
NotRegexpT asserts that a specified regular expression does not match a string.
See RegexpT.
Usage ¶
assertions.NotRegexp(t, regexp.MustCompile("starts"), "it's starting")
assertions.NotRegexp(t, "^start", "it's not starting")
Examples ¶
success: "^start", "not starting" failure: "^start", "starting"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotRegexpT(t *testing.T)
success := assert.NotRegexpT(t, "^start", "not starting")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NotRegexpTf ¶ added in v2.2.0
NotRegexpTf is the same as NotRegexpT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotRegexpf ¶
NotRegexpf is the same as NotRegexp, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotSame ¶
NotSame asserts that two pointers do not reference the same object.
See Same.
Usage ¶
assertions.NotSame(t, ptr1, ptr2)
Examples ¶
success: &staticVar, ptr("static string")
failure: &staticVar, staticVarPtr
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotSame(t *testing.T)
success := assert.NotSame(t, &staticVar, ptr("static string"))
fmt.Printf("success: %t\n", success)
}
//nolint:gochecknoglobals // this is on purpose to share a common pointer when testing
var staticVar = "static string"
func ptr[T any](value T) *T {
p := value
return &p
}
Output: success: true
func NotSameT ¶ added in v2.2.0
NotSameT asserts that two pointers do not reference the same object.
See SameT.
Usage ¶
assertions.NotSameT(t, ptr1, ptr2)
Examples ¶
success: &staticVar, ptr("static string")
failure: &staticVar, staticVarPtr
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotSameT(t *testing.T)
success := assert.NotSameT(t, &staticVar, ptr("static string"))
fmt.Printf("success: %t\n", success)
}
//nolint:gochecknoglobals // this is on purpose to share a common pointer when testing
var staticVar = "static string"
func ptr[T any](value T) *T {
p := value
return &p
}
Output: success: true
func NotSameTf ¶ added in v2.2.0
NotSameTf is the same as NotSameT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotSamef ¶
NotSamef is the same as NotSame, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotSortedT ¶ added in v2.2.0
NotSortedT asserts that the slice of Ordered is NOT sorted (i.e. non-strictly increasing).
Unlike IsDecreasingT, it accepts slices that are neither increasing nor decreasing.
Usage ¶
assertions.NotSortedT(t, []int{3, 2, 3})
assertions.NotSortedT(t, []float{2, 1})
assertions.NotSortedT(t, []string{"b", "a"})
Examples ¶
success: []int{3, 1, 3}
failure: []int{1, 4, 8}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotSortedT(t *testing.T)
success := assert.NotSortedT(t, []int{3, 1, 3})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NotSortedTf ¶ added in v2.2.0
func NotSortedTf[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msg string, args ...any) bool
NotSortedTf is the same as NotSortedT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotSubset ¶
NotSubset asserts that the list (array, slice, or map) does NOT contain all elements given in the subset (array, slice, or map). Map elements are key-value pairs unless compared with an array or slice where only the map key is evaluated.
Usage ¶
assertions.NotSubset(t, [1, 3, 4], [1, 2])
assertions.NotSubset(t, {"x": 1, "y": 2}, {"z": 3})
assertions.NotSubset(t, [1, 3, 4], {1: "one", 2: "two"})
assertions.NotSubset(t, {"x": 1, "y": 2}, ["z"])
Examples ¶
success: []int{1, 2, 3}, []int{4, 5}
failure: []int{1, 2, 3}, []int{1, 2}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotSubset(t *testing.T)
success := assert.NotSubset(t, []int{1, 2, 3}, []int{4, 5})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NotSubsetf ¶
NotSubsetf is the same as NotSubset, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func NotZero ¶
NotZero asserts that i is not the zero value for its type.
Usage ¶
assertions.NotZero(t, obj)
Examples ¶
success: 1 failure: 0
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestNotZero(t *testing.T)
success := assert.NotZero(t, 1)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func NotZerof ¶
NotZerof is the same as NotZero, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func ObjectsAreEqual ¶
ObjectsAreEqual determines if two objects are considered equal.
This function does no assertion of any kind.
func ObjectsAreEqualValues ¶
ObjectsAreEqualValues gets whether two objects are equal, or if their values are equal.
func Panics ¶
Panics asserts that the code inside the specified function panics.
Usage ¶
assertions.Panics(t, func(){ GoCrazy() })
Examples ¶
success: func() { panic("panicking") }
failure: func() { }
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestPanics(t *testing.T)
success := assert.Panics(t, func() {
panic("panicking")
})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func PanicsWithError ¶
PanicsWithError asserts that the code inside the specified function panics, and that the recovered panic value is an error that satisfies the EqualError comparison.
Usage ¶
assertions.PanicsWithError(t, "crazy error", func(){ GoCrazy() })
Examples ¶
success: ErrTest.Error(), func() { panic(ErrTest) }
failure: ErrTest.Error(), func() { }
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestPanicsWithError(t *testing.T)
success := assert.PanicsWithError(t, assert.ErrTest.Error(), func() {
panic(assert.ErrTest)
})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func PanicsWithErrorf ¶
PanicsWithErrorf is the same as PanicsWithError, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func PanicsWithValue ¶
PanicsWithValue asserts that the code inside the specified function panics, and that the recovered panic value equals the expected panic value.
Usage ¶
assertions.PanicsWithValue(t, "crazy error", func(){ GoCrazy() })
Examples ¶
success: "panicking", func() { panic("panicking") }
failure: "panicking", func() { }
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestPanicsWithValue(t *testing.T)
success := assert.PanicsWithValue(t, "panicking", func() {
panic("panicking")
})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func PanicsWithValuef ¶
PanicsWithValuef is the same as PanicsWithValue, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Panicsf ¶
Panicsf is the same as Panics, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Positive ¶
Positive asserts that the specified element is strictly positive.
Usage ¶
assertions.Positive(t, 1) assertions.Positive(t, 1.23)
Examples ¶
success: 1 failure: -1
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestPositive(t *testing.T)
success := assert.Positive(t, 1)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func PositiveT ¶ added in v2.2.0
func PositiveT[SignedNumber SignedNumeric](t T, e SignedNumber, msgAndArgs ...any) bool
PositiveT asserts that the specified element of a signed numeric type is strictly positive.
Usage ¶
assertions.PositiveT(t, 1) assertions.PositiveT(t, 1.23)
Examples ¶
success: 1 failure: -1
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestPositiveT(t *testing.T)
success := assert.PositiveT(t, 1)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func PositiveTf ¶ added in v2.2.0
func PositiveTf[SignedNumber SignedNumeric](t T, e SignedNumber, msg string, args ...any) bool
PositiveTf is the same as PositiveT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Positivef ¶
Positivef is the same as Positive, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Regexp ¶
Regexp asserts that a specified regular expression matches a string.
The regular expression may be passed as a regexp.Regexp, a string or a []byte and will be compiled.
The actual argument to be matched may be a string, []byte or anything that prints as a string with fmt.Sprint.
Usage ¶
assertions.Regexp(t, regexp.MustCompile("start"), "it's starting")
assertions.Regexp(t, "start...$", "it's not starting")
Examples ¶
success: "^start", "starting" failure: "^start", "not starting"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestRegexp(t *testing.T)
success := assert.Regexp(t, "^start", "starting")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func RegexpT ¶ added in v2.2.0
RegexpT asserts that a specified regular expression matches a string.
The actual argument to be matched may be a string or []byte.
See Regexp.
Examples ¶
success: "^start", "starting" failure: "^start", "not starting"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestRegexpT(t *testing.T)
success := assert.RegexpT(t, "^start", "starting")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func RegexpTf ¶ added in v2.2.0
RegexpTf is the same as RegexpT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Regexpf ¶
Regexpf is the same as Regexp, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Same ¶
Same asserts that two pointers reference the same object.
Both arguments must be pointer variables.
Pointer variable sameness is determined based on the equality of both type and value.
Unlike Equal pointers, Same pointers point to the same memory address.
Usage ¶
assertions.Same(t, ptr1, ptr2)
Examples ¶
success: &staticVar, staticVarPtr
failure: &staticVar, ptr("static string")
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestSame(t *testing.T)
success := assert.Same(t, &staticVar, staticVarPtr)
fmt.Printf("success: %t\n", success)
}
//nolint:gochecknoglobals // this is on purpose to share a common pointer when testing
var (
staticVar = "static string"
staticVarPtr = &staticVar
)
Output: success: true
func SameT ¶ added in v2.2.0
SameT asserts that two pointers of the same type reference the same object.
See Same.
Usage ¶
assertions.SameT(t, ptr1, ptr2)
Examples ¶
success: &staticVar, staticVarPtr
failure: &staticVar, ptr("static string")
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestSameT(t *testing.T)
success := assert.SameT(t, &staticVar, staticVarPtr)
fmt.Printf("success: %t\n", success)
}
//nolint:gochecknoglobals // this is on purpose to share a common pointer when testing
var (
staticVar = "static string"
staticVarPtr = &staticVar
)
Output: success: true
func SameTf ¶ added in v2.2.0
SameTf is the same as SameT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Samef ¶
Samef is the same as Same, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func SeqContainsT ¶ added in v2.2.0
SeqContainsT asserts that the specified iterator contains a comparable element.
The sequence may not be consumed entirely: the iteration stops as soon as the specified element is found.
Go native comparable types are explained there: comparable-types.
Usage ¶
assertions.SeqContainsT(t, slices.Values([]{"Hello","World"}), "World")
Examples ¶
success: slices.Values([]string{"A","B"}), "A"
failure: slices.Values([]string{"A","B"}), "C"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"slices"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestSeqContainsT(t *testing.T)
success := assert.SeqContainsT(t, slices.Values([]string{"A", "B"}), "A")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func SeqContainsTf ¶ added in v2.2.0
SeqContainsTf is the same as SeqContainsT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func SeqNotContainsT ¶ added in v2.2.0
SeqNotContainsT asserts that the specified iterator does not contain a comparable element.
See SeqContainsT.
Usage ¶
assertions.SeqContainsT(t, slices.Values([]{"Hello","World"}), "World")
Examples ¶
success: slices.Values([]string{"A","B"}), "C"
failure: slices.Values([]string{"A","B"}), "A"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"slices"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestSeqNotContainsT(t *testing.T)
success := assert.SeqNotContainsT(t, slices.Values([]string{"A", "B"}), "C")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func SeqNotContainsTf ¶ added in v2.2.0
SeqNotContainsTf is the same as SeqNotContainsT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func SliceContainsT ¶ added in v2.2.0
func SliceContainsT[Slice ~[]E, E comparable](t T, s Slice, element E, msgAndArgs ...any) bool
SliceContainsT asserts that the specified slice contains a comparable element.
Go native comparable types are explained there: comparable-types.
Usage ¶
assertions.SliceContainsT(t, []{"Hello","World"}, "World")
Examples ¶
success: []string{"A","B"}, "A"
failure: []string{"A","B"}, "C"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestSliceContainsT(t *testing.T)
success := assert.SliceContainsT(t, []string{"A", "B"}, "A")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func SliceContainsTf ¶ added in v2.2.0
func SliceContainsTf[Slice ~[]E, E comparable](t T, s Slice, element E, msg string, args ...any) bool
SliceContainsTf is the same as SliceContainsT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func SliceNotContainsT ¶ added in v2.2.0
func SliceNotContainsT[Slice ~[]E, E comparable](t T, s Slice, element E, msgAndArgs ...any) bool
SliceNotContainsT asserts that the specified slice does not contain a comparable element.
See SliceContainsT.
Usage ¶
assertions.SliceNotContainsT(t, []{"Hello","World"}, "hi")
Examples ¶
success: []string{"A","B"}, "C"
failure: []string{"A","B"}, "A"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestSliceNotContainsT(t *testing.T)
success := assert.SliceNotContainsT(t, []string{"A", "B"}, "C")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func SliceNotContainsTf ¶ added in v2.2.0
func SliceNotContainsTf[Slice ~[]E, E comparable](t T, s Slice, element E, msg string, args ...any) bool
SliceNotContainsTf is the same as SliceNotContainsT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func SliceNotSubsetT ¶ added in v2.2.0
func SliceNotSubsetT[Slice ~[]E, E comparable](t T, list Slice, subset Slice, msgAndArgs ...any) (ok bool)
SliceNotSubsetT asserts that a slice of comparable elements does not contain all the elements given in the subset.
Usage ¶
assertions.SliceNotSubsetT(t, []int{1, 2, 3}, []int{1, 4})
Examples ¶
success: []int{1, 2, 3}, []int{4, 5}
failure: []int{1, 2, 3}, []int{1, 2}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestSliceNotSubsetT(t *testing.T)
success := assert.SliceNotSubsetT(t, []int{1, 2, 3}, []int{4, 5})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func SliceNotSubsetTf ¶ added in v2.2.0
func SliceNotSubsetTf[Slice ~[]E, E comparable](t T, list Slice, subset Slice, msg string, args ...any) (ok bool)
SliceNotSubsetTf is the same as SliceNotSubsetT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func SliceSubsetT ¶ added in v2.2.0
func SliceSubsetT[Slice ~[]E, E comparable](t T, list Slice, subset Slice, msgAndArgs ...any) (ok bool)
SliceSubsetT asserts that a slice of comparable elements contains all the elements given in the subset.
Usage ¶
assertions.SliceSubsetT(t, []int{1, 2, 3}, []int{1, 2})
Examples ¶
success: []int{1, 2, 3}, []int{1, 2}
failure: []int{1, 2, 3}, []int{4, 5}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestSliceSubsetT(t *testing.T)
success := assert.SliceSubsetT(t, []int{1, 2, 3}, []int{1, 2})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func SliceSubsetTf ¶ added in v2.2.0
func SliceSubsetTf[Slice ~[]E, E comparable](t T, list Slice, subset Slice, msg string, args ...any) (ok bool)
SliceSubsetTf is the same as SliceSubsetT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func SortedT ¶ added in v2.2.0
SortedT asserts that the slice of Ordered is sorted (i.e. non-strictly increasing).
Unlike IsIncreasingT, it accepts elements to be equal.
Usage ¶
assertions.SortedT(t, []int{1, 2, 3})
assertions.SortedT(t, []float{1, 2})
assertions.SortedT(t, []string{"a", "b"})
Examples ¶
success: []int{1, 1, 3}
failure: []int{1, 4, 2}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestSortedT(t *testing.T)
success := assert.SortedT(t, []int{1, 1, 3})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func SortedTf ¶ added in v2.2.0
func SortedTf[OrderedSlice ~[]E, E Ordered](t T, collection OrderedSlice, msg string, args ...any) bool
SortedTf is the same as SortedT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func StringContainsT ¶ added in v2.2.0
StringContainsT asserts that a string contains the specified substring.
Strings may be go strings or []byte according to the type constraint Text.
Usage ¶
assertions.StringContainsT(t, "Hello World", "World")
Examples ¶
success: "AB", "A" failure: "AB", "C"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestStringContainsT(t *testing.T)
success := assert.StringContainsT(t, "AB", "A")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func StringContainsTf ¶ added in v2.2.0
StringContainsTf is the same as StringContainsT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func StringNotContainsT ¶ added in v2.2.0
StringNotContainsT asserts that a string does not contain the specified substring.
See StringContainsT.
Usage ¶
assertions.StringNotContainsT(t, "Hello World", "hi")
Examples ¶
success: "AB", "C" failure: "AB", "A"
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestStringNotContainsT(t *testing.T)
success := assert.StringNotContainsT(t, "AB", "C")
fmt.Printf("success: %t\n", success)
}
Output: success: true
func StringNotContainsTf ¶ added in v2.2.0
func StringNotContainsTf[ADoc, EDoc Text](t T, str ADoc, substring EDoc, msg string, args ...any) bool
StringNotContainsTf is the same as StringNotContainsT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Subset ¶
Subset asserts that the list (array, slice, or map) contains all elements given in the subset (array, slice, or map).
Map elements are key-value pairs unless compared with an array or slice where only the map key is evaluated.
nil values are considered as empty sets.
Usage ¶
assertions.Subset(t, []int{1, 2, 3}, []int{1, 2})
assertions.Subset(t, []string{"x": 1, "y": 2}, []string{"x": 1})
assertions.Subset(t, []int{1, 2, 3}, map[int]string{1: "one", 2: "two"})
assertions.Subset(t, map[string]int{"x": 1, "y": 2}, []string{"x"})
Examples ¶
success: []int{1, 2, 3}, []int{1, 2}
failure: []int{1, 2, 3}, []int{4, 5}
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestSubset(t *testing.T)
success := assert.Subset(t, []int{1, 2, 3}, []int{1, 2})
fmt.Printf("success: %t\n", success)
}
Output: success: true
func Subsetf ¶
Subsetf is the same as Subset, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func True ¶
True asserts that the specified value is true.
Usage ¶
assertions.True(t, myBool)
Examples ¶
success: 1 == 1 failure: 1 == 0
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestTrue(t *testing.T)
success := assert.True(t, 1 == 1)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func TrueT ¶ added in v2.2.0
TrueT asserts that the specified value is true.
The type constraint Boolean accepts any type which underlying type is bool.
Usage ¶
type B bool var b B = true assertions.True(t, b)
Examples ¶
success: 1 == 1 failure: 1 == 0
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestTrueT(t *testing.T)
success := assert.TrueT(t, 1 == 1)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func TrueTf ¶ added in v2.2.0
TrueTf is the same as TrueT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Truef ¶
Truef is the same as True, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func WithinDuration ¶
func WithinDuration(t T, expected time.Time, actual time.Time, delta time.Duration, msgAndArgs ...any) bool
WithinDuration asserts that the two times are within duration delta of each other.
Usage ¶
assertions.WithinDuration(t, time.Now(), 10*time.Second)
Examples ¶
success: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC), time.Date(2024, 1, 1, 12, 0, 1, 0, time.UTC), 2*time.Second failure: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC), time.Date(2024, 1, 1, 12, 0, 10, 0, time.UTC), 1*time.Second
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"time"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestWithinDuration(t *testing.T)
success := assert.WithinDuration(t, time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC), time.Date(2024, 1, 1, 12, 0, 1, 0, time.UTC), 2*time.Second)
fmt.Printf("success: %t\n", success)
}
Output: success: true
func WithinDurationf ¶
func WithinDurationf(t T, expected time.Time, actual time.Time, delta time.Duration, msg string, args ...any) bool
WithinDurationf is the same as WithinDuration, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func WithinRange ¶
WithinRange asserts that a time is within a time range (inclusive).
Usage ¶
assertions.WithinRange(t, time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second))
Examples ¶
success: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC), time.Date(2024, 1, 1, 11, 0, 0, 0, time.UTC), time.Date(2024, 1, 1, 13, 0, 0, 0, time.UTC) failure: time.Date(2024, 1, 1, 14, 0, 0, 0, time.UTC), time.Date(2024, 1, 1, 11, 0, 0, 0, time.UTC), time.Date(2024, 1, 1, 13, 0, 0, 0, time.UTC)
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"time"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestWithinRange(t *testing.T)
success := assert.WithinRange(t, time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC), time.Date(2024, 1, 1, 11, 0, 0, 0, time.UTC), time.Date(2024, 1, 1, 13, 0, 0, 0, time.UTC))
fmt.Printf("success: %t\n", success)
}
Output: success: true
func WithinRangef ¶
func WithinRangef(t T, actual time.Time, start time.Time, end time.Time, msg string, args ...any) bool
WithinRangef is the same as WithinRange, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func YAMLEq ¶
YAMLEq asserts that two YAML strings are equivalent.
See YAMLEqBytes.
Examples ¶
panic: "key: value", "key: value" should panic without the yaml feature enabled.
Upon failure, the test T is marked as failed and continues execution.
func YAMLEqBytes ¶ added in v2.2.0
YAMLEqBytes asserts that two YAML slices of bytes are equivalent.
Expected and actual must be valid YAML.
Important ¶
By default, this function is disabled and will panic.
To enable it, you should add a blank import like so:
import( "github.com/go-openapi/testify/enable/yaml/v2" )
Usage ¶
expected := `--- key: value --- key: this is a second document, it is not evaluated ` actual := `--- key: value --- key: this is a subsequent document, it is not evaluated ` assertions.YAMLEq(t, expected, actual)
Examples ¶
panic: []byte("key: value"), []byte("key: value")
should panic without the yaml feature enabled.
Upon failure, the test T is marked as failed and continues execution.
func YAMLEqBytesf ¶ added in v2.2.0
YAMLEqBytesf is the same as YAMLEqBytes, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func YAMLEqT ¶ added in v2.2.0
YAMLEqT asserts that two YAML documents are equivalent.
The expected and actual arguments may be string or []byte. They do not need to be of the same type.
See YAMLEqBytes.
Examples ¶
panic: "key: value", "key: value" should panic without the yaml feature enabled.
Upon failure, the test T is marked as failed and continues execution.
func YAMLEqTf ¶ added in v2.2.0
YAMLEqTf is the same as YAMLEqT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func YAMLEqf ¶
YAMLEqf is the same as YAMLEq, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func YAMLMarshalAsT ¶ added in v2.3.0
YAMLMarshalAsT wraps YAMLEq after [yaml.Marshal].
The input YAML may be a string or []byte.
It fails if the marshaling returns an error or if the expected YAML bytes differ semantically from the expected ones.
Usage ¶
actual := struct {
A int `yaml:"a"`
}{
A: 10,
}
assertions.YAMLUnmarshalAsT(t,expected, `{"a": 10}`)
Examples ¶
panic: "key: value", "key: value" should panic without the yaml feature enabled.
Upon failure, the test T is marked as failed and continues execution.
func YAMLMarshalAsTf ¶ added in v2.3.0
YAMLMarshalAsTf is the same as YAMLMarshalAsT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func YAMLUnmarshalAsT ¶ added in v2.3.0
func YAMLUnmarshalAsT[Object any, ADoc Text](t T, expected Object, jazon ADoc, msgAndArgs ...any) bool
YAMLUnmarshalAsT wraps Equal after [yaml.Unmarshal].
The input YAML may be a string or []byte.
It fails if the unmarshaling returns an error or if the resulting object is not equal to the expected one.
Be careful not to wrap the expected object into an "any" interface if this is not what you expected: the unmarshaling would take this type to unmarshal as a map[string]any.
Usage ¶
expected := struct {
A int `yaml:"a"`
}{
A: 10,
}
assertions.YAMLUnmarshalAsT(t,expected, `{"a": 10}`)
Examples ¶
panic: "key: value", "key: value" should panic without the yaml feature enabled.
Upon failure, the test T is marked as failed and continues execution.
func YAMLUnmarshalAsTf ¶ added in v2.3.0
func YAMLUnmarshalAsTf[Object any, ADoc Text](t T, expected Object, jazon ADoc, msg string, args ...any) bool
YAMLUnmarshalAsTf is the same as YAMLUnmarshalAsT, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func Zero ¶
Zero asserts that i is the zero value for its type.
Usage ¶
assertions.Zero(t, obj)
Examples ¶
success: 0 failure: 1
Upon failure, the test T is marked as failed and continues execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // should come from testing, e.g. func TestZero(t *testing.T)
success := assert.Zero(t, 0)
fmt.Printf("success: %t\n", success)
}
Output: success: true
Types ¶
type Assertions ¶
type Assertions struct {
T
}
Assertions exposes all assertion functions as methods.
NOTE: assertion methods with parameterized types (generics) are not supported as methods.
Upon failure, the test T is marked as failed and continues execution.
Example (With_generics) ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // normally provided by test
a := assert.New(t)
const expected = "hello"
goodValue := "hello"
wrongValue := "world"
r0 := a.Equal(expected, goodValue) // classic reflect-based assertion
fmt.Printf("good value is %t\n", r0)
r1 := assert.EqualT(a.T, expected, goodValue) // usage with generic assertion
fmt.Printf("good value is %t\n", r1)
r2 := assert.EqualT(a.T, expected, wrongValue)
fmt.Printf("wrong value is %t\n", r2)
}
Output: good value is true good value is true wrong value is false
func New ¶
func New(t T) *Assertions
New makes a new Assertions object for the specified T (e.g. testing.T).
func (*Assertions) Condition ¶
func (a *Assertions) Condition(comp func() bool, msgAndArgs ...any) bool
Condition is the same as Condition, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Conditionf ¶
func (a *Assertions) Conditionf(comp func() bool, msg string, args ...any) bool
Conditionf is the same as Assertions.Condition, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Contains ¶
func (a *Assertions) Contains(s any, contains any, msgAndArgs ...any) bool
Contains is the same as Contains, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Containsf ¶
Containsf is the same as Assertions.Contains, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) DirExists ¶
func (a *Assertions) DirExists(path string, msgAndArgs ...any) bool
DirExists is the same as DirExists, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) DirExistsf ¶
func (a *Assertions) DirExistsf(path string, msg string, args ...any) bool
DirExistsf is the same as Assertions.DirExists, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) DirNotExists ¶ added in v2.2.0
func (a *Assertions) DirNotExists(path string, msgAndArgs ...any) bool
DirNotExists is the same as DirNotExists, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) DirNotExistsf ¶ added in v2.2.0
func (a *Assertions) DirNotExistsf(path string, msg string, args ...any) bool
DirNotExistsf is the same as Assertions.DirNotExists, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) ElementsMatch ¶
func (a *Assertions) ElementsMatch(listA any, listB any, msgAndArgs ...any) (ok bool)
ElementsMatch is the same as ElementsMatch, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) ElementsMatchf ¶
ElementsMatchf is the same as Assertions.ElementsMatch, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Empty ¶
func (a *Assertions) Empty(object any, msgAndArgs ...any) bool
Empty is the same as Empty, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Emptyf ¶
func (a *Assertions) Emptyf(object any, msg string, args ...any) bool
Emptyf is the same as Assertions.Empty, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Equal ¶
func (a *Assertions) Equal(expected any, actual any, msgAndArgs ...any) bool
Equal is the same as Equal, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) EqualError ¶
func (a *Assertions) EqualError(err error, errString string, msgAndArgs ...any) bool
EqualError is the same as EqualError, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) EqualErrorf ¶
EqualErrorf is the same as Assertions.EqualError, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) EqualExportedValues ¶
func (a *Assertions) EqualExportedValues(expected any, actual any, msgAndArgs ...any) bool
EqualExportedValues is the same as EqualExportedValues, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) EqualExportedValuesf ¶
EqualExportedValuesf is the same as Assertions.EqualExportedValues, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) EqualValues ¶
func (a *Assertions) EqualValues(expected any, actual any, msgAndArgs ...any) bool
EqualValues is the same as EqualValues, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) EqualValuesf ¶
EqualValuesf is the same as Assertions.EqualValues, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Equalf ¶
Equalf is the same as Assertions.Equal, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Error ¶
func (a *Assertions) Error(err error, msgAndArgs ...any) bool
Error is the same as Error, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) ErrorAs ¶
func (a *Assertions) ErrorAs(err error, target any, msgAndArgs ...any) bool
ErrorAs is the same as ErrorAs, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) ErrorAsf ¶
ErrorAsf is the same as Assertions.ErrorAs, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) ErrorContains ¶
func (a *Assertions) ErrorContains(err error, contains string, msgAndArgs ...any) bool
ErrorContains is the same as ErrorContains, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) ErrorContainsf ¶
ErrorContainsf is the same as Assertions.ErrorContains, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) ErrorIs ¶
func (a *Assertions) ErrorIs(err error, target error, msgAndArgs ...any) bool
ErrorIs is the same as ErrorIs, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) ErrorIsf ¶
ErrorIsf is the same as Assertions.ErrorIs, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Errorf ¶
func (a *Assertions) Errorf(err error, msg string, args ...any) bool
Errorf is the same as Assertions.Error, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Exactly ¶
func (a *Assertions) Exactly(expected any, actual any, msgAndArgs ...any) bool
Exactly is the same as Exactly, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Exactlyf ¶
Exactlyf is the same as Assertions.Exactly, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Fail ¶
func (a *Assertions) Fail(failureMessage string, msgAndArgs ...any) bool
Fail is the same as Fail, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) FailNow ¶
func (a *Assertions) FailNow(failureMessage string, msgAndArgs ...any) bool
FailNow is the same as FailNow, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) FailNowf ¶
func (a *Assertions) FailNowf(failureMessage string, msg string, args ...any) bool
FailNowf is the same as Assertions.FailNow, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Failf ¶
func (a *Assertions) Failf(failureMessage string, msg string, args ...any) bool
Failf is the same as Assertions.Fail, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) False ¶
func (a *Assertions) False(value bool, msgAndArgs ...any) bool
False is the same as False, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Falsef ¶
func (a *Assertions) Falsef(value bool, msg string, args ...any) bool
Falsef is the same as Assertions.False, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) FileEmpty ¶ added in v2.1.0
func (a *Assertions) FileEmpty(path string, msgAndArgs ...any) bool
FileEmpty is the same as FileEmpty, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) FileEmptyf ¶ added in v2.1.0
func (a *Assertions) FileEmptyf(path string, msg string, args ...any) bool
FileEmptyf is the same as Assertions.FileEmpty, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) FileExists ¶
func (a *Assertions) FileExists(path string, msgAndArgs ...any) bool
FileExists is the same as FileExists, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) FileExistsf ¶
func (a *Assertions) FileExistsf(path string, msg string, args ...any) bool
FileExistsf is the same as Assertions.FileExists, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) FileNotEmpty ¶ added in v2.1.0
func (a *Assertions) FileNotEmpty(path string, msgAndArgs ...any) bool
FileNotEmpty is the same as FileNotEmpty, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) FileNotEmptyf ¶ added in v2.1.0
func (a *Assertions) FileNotEmptyf(path string, msg string, args ...any) bool
FileNotEmptyf is the same as Assertions.FileNotEmpty, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) FileNotExists ¶ added in v2.2.0
func (a *Assertions) FileNotExists(path string, msgAndArgs ...any) bool
FileNotExists is the same as FileNotExists, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) FileNotExistsf ¶ added in v2.2.0
func (a *Assertions) FileNotExistsf(path string, msg string, args ...any) bool
FileNotExistsf is the same as Assertions.FileNotExists, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Greater ¶
func (a *Assertions) Greater(e1 any, e2 any, msgAndArgs ...any) bool
Greater is the same as Greater, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) GreaterOrEqual ¶
func (a *Assertions) GreaterOrEqual(e1 any, e2 any, msgAndArgs ...any) bool
GreaterOrEqual is the same as GreaterOrEqual, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) GreaterOrEqualf ¶
GreaterOrEqualf is the same as Assertions.GreaterOrEqual, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Greaterf ¶
Greaterf is the same as Assertions.Greater, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) HTTPBodyContains ¶
func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method string, url string, values url.Values, str any, msgAndArgs ...any) bool
HTTPBodyContains is the same as HTTPBodyContains, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) HTTPBodyContainsf ¶
func (a *Assertions) HTTPBodyContainsf(handler http.HandlerFunc, method string, url string, values url.Values, str any, msg string, args ...any) bool
HTTPBodyContainsf is the same as Assertions.HTTPBodyContains, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) HTTPBodyNotContains ¶
func (a *Assertions) HTTPBodyNotContains(handler http.HandlerFunc, method string, url string, values url.Values, str any, msgAndArgs ...any) bool
HTTPBodyNotContains is the same as HTTPBodyNotContains, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) HTTPBodyNotContainsf ¶
func (a *Assertions) HTTPBodyNotContainsf(handler http.HandlerFunc, method string, url string, values url.Values, str any, msg string, args ...any) bool
HTTPBodyNotContainsf is the same as Assertions.HTTPBodyNotContains, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) HTTPError ¶
func (a *Assertions) HTTPError(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...any) bool
HTTPError is the same as HTTPError, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) HTTPErrorf ¶
func (a *Assertions) HTTPErrorf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...any) bool
HTTPErrorf is the same as Assertions.HTTPError, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) HTTPRedirect ¶
func (a *Assertions) HTTPRedirect(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...any) bool
HTTPRedirect is the same as HTTPRedirect, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) HTTPRedirectf ¶
func (a *Assertions) HTTPRedirectf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...any) bool
HTTPRedirectf is the same as Assertions.HTTPRedirect, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) HTTPStatusCode ¶
func (a *Assertions) HTTPStatusCode(handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msgAndArgs ...any) bool
HTTPStatusCode is the same as HTTPStatusCode, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) HTTPStatusCodef ¶
func (a *Assertions) HTTPStatusCodef(handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msg string, args ...any) bool
HTTPStatusCodef is the same as Assertions.HTTPStatusCode, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) HTTPSuccess ¶
func (a *Assertions) HTTPSuccess(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...any) bool
HTTPSuccess is the same as HTTPSuccess, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) HTTPSuccessf ¶
func (a *Assertions) HTTPSuccessf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...any) bool
HTTPSuccessf is the same as Assertions.HTTPSuccess, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Implements ¶
func (a *Assertions) Implements(interfaceObject any, object any, msgAndArgs ...any) bool
Implements is the same as Implements, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Implementsf ¶
Implementsf is the same as Assertions.Implements, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) InDelta ¶
InDelta is the same as InDelta, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) InDeltaMapValues ¶
func (a *Assertions) InDeltaMapValues(expected any, actual any, delta float64, msgAndArgs ...any) bool
InDeltaMapValues is the same as InDeltaMapValues, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) InDeltaMapValuesf ¶
func (a *Assertions) InDeltaMapValuesf(expected any, actual any, delta float64, msg string, args ...any) bool
InDeltaMapValuesf is the same as Assertions.InDeltaMapValues, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) InDeltaSlice ¶
InDeltaSlice is the same as InDeltaSlice, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) InDeltaSlicef ¶
func (a *Assertions) InDeltaSlicef(expected any, actual any, delta float64, msg string, args ...any) bool
InDeltaSlicef is the same as Assertions.InDeltaSlice, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) InDeltaf ¶
func (a *Assertions) InDeltaf(expected any, actual any, delta float64, msg string, args ...any) bool
InDeltaf is the same as Assertions.InDelta, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) InEpsilon ¶
InEpsilon is the same as InEpsilon, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) InEpsilonSlice ¶
func (a *Assertions) InEpsilonSlice(expected any, actual any, epsilon float64, msgAndArgs ...any) bool
InEpsilonSlice is the same as InEpsilonSlice, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) InEpsilonSlicef ¶
func (a *Assertions) InEpsilonSlicef(expected any, actual any, epsilon float64, msg string, args ...any) bool
InEpsilonSlicef is the same as Assertions.InEpsilonSlice, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) InEpsilonf ¶
func (a *Assertions) InEpsilonf(expected any, actual any, epsilon float64, msg string, args ...any) bool
InEpsilonf is the same as Assertions.InEpsilon, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) IsDecreasing ¶
func (a *Assertions) IsDecreasing(collection any, msgAndArgs ...any) bool
IsDecreasing is the same as IsDecreasing, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) IsDecreasingf ¶
func (a *Assertions) IsDecreasingf(collection any, msg string, args ...any) bool
IsDecreasingf is the same as Assertions.IsDecreasing, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) IsIncreasing ¶
func (a *Assertions) IsIncreasing(collection any, msgAndArgs ...any) bool
IsIncreasing is the same as IsIncreasing, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) IsIncreasingf ¶
func (a *Assertions) IsIncreasingf(collection any, msg string, args ...any) bool
IsIncreasingf is the same as Assertions.IsIncreasing, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) IsNonDecreasing ¶
func (a *Assertions) IsNonDecreasing(collection any, msgAndArgs ...any) bool
IsNonDecreasing is the same as IsNonDecreasing, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) IsNonDecreasingf ¶
func (a *Assertions) IsNonDecreasingf(collection any, msg string, args ...any) bool
IsNonDecreasingf is the same as Assertions.IsNonDecreasing, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) IsNonIncreasing ¶
func (a *Assertions) IsNonIncreasing(collection any, msgAndArgs ...any) bool
IsNonIncreasing is the same as IsNonIncreasing, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) IsNonIncreasingf ¶
func (a *Assertions) IsNonIncreasingf(collection any, msg string, args ...any) bool
IsNonIncreasingf is the same as Assertions.IsNonIncreasing, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) IsNotType ¶
func (a *Assertions) IsNotType(theType any, object any, msgAndArgs ...any) bool
IsNotType is the same as IsNotType, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) IsNotTypef ¶
IsNotTypef is the same as Assertions.IsNotType, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) IsType ¶
func (a *Assertions) IsType(expectedType any, object any, msgAndArgs ...any) bool
IsType is the same as IsType, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) IsTypef ¶
IsTypef is the same as Assertions.IsType, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) JSONEq ¶
func (a *Assertions) JSONEq(expected string, actual string, msgAndArgs ...any) bool
JSONEq is the same as JSONEq, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) JSONEqBytes ¶ added in v2.0.2
func (a *Assertions) JSONEqBytes(expected []byte, actual []byte, msgAndArgs ...any) bool
JSONEqBytes is the same as JSONEqBytes, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) JSONEqBytesf ¶ added in v2.0.2
JSONEqBytesf is the same as Assertions.JSONEqBytes, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) JSONEqf ¶
JSONEqf is the same as Assertions.JSONEq, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Kind ¶ added in v2.2.0
Kind is the same as Kind, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Kindf ¶ added in v2.2.0
Kindf is the same as Assertions.Kind, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Len ¶
func (a *Assertions) Len(object any, length int, msgAndArgs ...any) bool
Len is the same as Len, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Lenf ¶
Lenf is the same as Assertions.Len, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Less ¶
func (a *Assertions) Less(e1 any, e2 any, msgAndArgs ...any) bool
Less is the same as Less, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) LessOrEqual ¶
func (a *Assertions) LessOrEqual(e1 any, e2 any, msgAndArgs ...any) bool
LessOrEqual is the same as LessOrEqual, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) LessOrEqualf ¶
LessOrEqualf is the same as Assertions.LessOrEqual, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Lessf ¶
Lessf is the same as Assertions.Less, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Negative ¶
func (a *Assertions) Negative(e any, msgAndArgs ...any) bool
Negative is the same as Negative, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Negativef ¶
func (a *Assertions) Negativef(e any, msg string, args ...any) bool
Negativef is the same as Assertions.Negative, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Never ¶
func (a *Assertions) Never(condition func() bool, timeout time.Duration, tick time.Duration, msgAndArgs ...any) bool
Never is the same as Never, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Neverf ¶
func (a *Assertions) Neverf(condition func() bool, timeout time.Duration, tick time.Duration, msg string, args ...any) bool
Neverf is the same as Assertions.Never, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Nil ¶
func (a *Assertions) Nil(object any, msgAndArgs ...any) bool
Nil is the same as Nil, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Nilf ¶
func (a *Assertions) Nilf(object any, msg string, args ...any) bool
Nilf is the same as Assertions.Nil, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NoError ¶
func (a *Assertions) NoError(err error, msgAndArgs ...any) bool
NoError is the same as NoError, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NoErrorf ¶
func (a *Assertions) NoErrorf(err error, msg string, args ...any) bool
NoErrorf is the same as Assertions.NoError, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NoFileDescriptorLeak ¶ added in v2.4.0
func (a *Assertions) NoFileDescriptorLeak(tested func(), msgAndArgs ...any) bool
NoFileDescriptorLeak is the same as NoFileDescriptorLeak, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NoFileDescriptorLeakf ¶ added in v2.4.0
func (a *Assertions) NoFileDescriptorLeakf(tested func(), msg string, args ...any) bool
NoFileDescriptorLeakf is the same as Assertions.NoFileDescriptorLeak, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NoGoRoutineLeak ¶ added in v2.3.0
func (a *Assertions) NoGoRoutineLeak(tested func(), msgAndArgs ...any) bool
NoGoRoutineLeak is the same as NoGoRoutineLeak, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NoGoRoutineLeakf ¶ added in v2.3.0
func (a *Assertions) NoGoRoutineLeakf(tested func(), msg string, args ...any) bool
NoGoRoutineLeakf is the same as Assertions.NoGoRoutineLeak, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotContains ¶
func (a *Assertions) NotContains(s any, contains any, msgAndArgs ...any) bool
NotContains is the same as NotContains, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotContainsf ¶
NotContainsf is the same as Assertions.NotContains, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotElementsMatch ¶
func (a *Assertions) NotElementsMatch(listA any, listB any, msgAndArgs ...any) (ok bool)
NotElementsMatch is the same as NotElementsMatch, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotElementsMatchf ¶
NotElementsMatchf is the same as Assertions.NotElementsMatch, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotEmpty ¶
func (a *Assertions) NotEmpty(object any, msgAndArgs ...any) bool
NotEmpty is the same as NotEmpty, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotEmptyf ¶
func (a *Assertions) NotEmptyf(object any, msg string, args ...any) bool
NotEmptyf is the same as Assertions.NotEmpty, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotEqual ¶
func (a *Assertions) NotEqual(expected any, actual any, msgAndArgs ...any) bool
NotEqual is the same as NotEqual, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotEqualValues ¶
func (a *Assertions) NotEqualValues(expected any, actual any, msgAndArgs ...any) bool
NotEqualValues is the same as NotEqualValues, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotEqualValuesf ¶
NotEqualValuesf is the same as Assertions.NotEqualValues, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotEqualf ¶
NotEqualf is the same as Assertions.NotEqual, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotErrorAs ¶
func (a *Assertions) NotErrorAs(err error, target any, msgAndArgs ...any) bool
NotErrorAs is the same as NotErrorAs, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotErrorAsf ¶
NotErrorAsf is the same as Assertions.NotErrorAs, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotErrorIs ¶
func (a *Assertions) NotErrorIs(err error, target error, msgAndArgs ...any) bool
NotErrorIs is the same as NotErrorIs, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotErrorIsf ¶
NotErrorIsf is the same as Assertions.NotErrorIs, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotImplements ¶
func (a *Assertions) NotImplements(interfaceObject any, object any, msgAndArgs ...any) bool
NotImplements is the same as NotImplements, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotImplementsf ¶
NotImplementsf is the same as Assertions.NotImplements, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotKind ¶ added in v2.2.0
NotKind is the same as NotKind, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotKindf ¶ added in v2.2.0
NotKindf is the same as Assertions.NotKind, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotNil ¶
func (a *Assertions) NotNil(object any, msgAndArgs ...any) bool
NotNil is the same as NotNil, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotNilf ¶
func (a *Assertions) NotNilf(object any, msg string, args ...any) bool
NotNilf is the same as Assertions.NotNil, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotPanics ¶
func (a *Assertions) NotPanics(f func(), msgAndArgs ...any) bool
NotPanics is the same as NotPanics, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotPanicsf ¶
func (a *Assertions) NotPanicsf(f func(), msg string, args ...any) bool
NotPanicsf is the same as Assertions.NotPanics, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotRegexp ¶
func (a *Assertions) NotRegexp(rx any, actual any, msgAndArgs ...any) bool
NotRegexp is the same as NotRegexp, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotRegexpf ¶
NotRegexpf is the same as Assertions.NotRegexp, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotSame ¶
func (a *Assertions) NotSame(expected any, actual any, msgAndArgs ...any) bool
NotSame is the same as NotSame, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotSamef ¶
NotSamef is the same as Assertions.NotSame, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotSubset ¶
func (a *Assertions) NotSubset(list any, subset any, msgAndArgs ...any) (ok bool)
NotSubset is the same as NotSubset, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotSubsetf ¶
NotSubsetf is the same as Assertions.NotSubset, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotZero ¶
func (a *Assertions) NotZero(i any, msgAndArgs ...any) bool
NotZero is the same as NotZero, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) NotZerof ¶
func (a *Assertions) NotZerof(i any, msg string, args ...any) bool
NotZerof is the same as Assertions.NotZero, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Panics ¶
func (a *Assertions) Panics(f func(), msgAndArgs ...any) bool
Panics is the same as Panics, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) PanicsWithError ¶
func (a *Assertions) PanicsWithError(errString string, f func(), msgAndArgs ...any) bool
PanicsWithError is the same as PanicsWithError, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) PanicsWithErrorf ¶
func (a *Assertions) PanicsWithErrorf(errString string, f func(), msg string, args ...any) bool
PanicsWithErrorf is the same as Assertions.PanicsWithError, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) PanicsWithValue ¶
func (a *Assertions) PanicsWithValue(expected any, f func(), msgAndArgs ...any) bool
PanicsWithValue is the same as PanicsWithValue, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) PanicsWithValuef ¶
func (a *Assertions) PanicsWithValuef(expected any, f func(), msg string, args ...any) bool
PanicsWithValuef is the same as Assertions.PanicsWithValue, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Panicsf ¶
func (a *Assertions) Panicsf(f func(), msg string, args ...any) bool
Panicsf is the same as Assertions.Panics, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Positive ¶
func (a *Assertions) Positive(e any, msgAndArgs ...any) bool
Positive is the same as Positive, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Positivef ¶
func (a *Assertions) Positivef(e any, msg string, args ...any) bool
Positivef is the same as Assertions.Positive, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Regexp ¶
func (a *Assertions) Regexp(rx any, actual any, msgAndArgs ...any) bool
Regexp is the same as Regexp, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Regexpf ¶
Regexpf is the same as Assertions.Regexp, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Same ¶
func (a *Assertions) Same(expected any, actual any, msgAndArgs ...any) bool
Same is the same as Same, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Samef ¶
Samef is the same as Assertions.Same, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Subset ¶
func (a *Assertions) Subset(list any, subset any, msgAndArgs ...any) (ok bool)
Subset is the same as Subset, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Subsetf ¶
Subsetf is the same as Assertions.Subset, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) True ¶
func (a *Assertions) True(value bool, msgAndArgs ...any) bool
True is the same as True, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Truef ¶
func (a *Assertions) Truef(value bool, msg string, args ...any) bool
Truef is the same as Assertions.True, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) WithinDuration ¶
func (a *Assertions) WithinDuration(expected time.Time, actual time.Time, delta time.Duration, msgAndArgs ...any) bool
WithinDuration is the same as WithinDuration, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) WithinDurationf ¶
func (a *Assertions) WithinDurationf(expected time.Time, actual time.Time, delta time.Duration, msg string, args ...any) bool
WithinDurationf is the same as Assertions.WithinDuration, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) WithinRange ¶
func (a *Assertions) WithinRange(actual time.Time, start time.Time, end time.Time, msgAndArgs ...any) bool
WithinRange is the same as WithinRange, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) WithinRangef ¶
func (a *Assertions) WithinRangef(actual time.Time, start time.Time, end time.Time, msg string, args ...any) bool
WithinRangef is the same as Assertions.WithinRange, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) YAMLEq ¶
func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...any) bool
YAMLEq is the same as YAMLEq, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) YAMLEqBytes ¶ added in v2.2.0
func (a *Assertions) YAMLEqBytes(expected []byte, actual []byte, msgAndArgs ...any) bool
YAMLEqBytes is the same as YAMLEqBytes, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) YAMLEqBytesf ¶ added in v2.2.0
YAMLEqBytesf is the same as Assertions.YAMLEqBytes, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) YAMLEqf ¶
YAMLEqf is the same as Assertions.YAMLEq, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Zero ¶
func (a *Assertions) Zero(i any, msgAndArgs ...any) bool
Zero is the same as Zero, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and continues execution.
func (*Assertions) Zerof ¶
func (a *Assertions) Zerof(i any, msg string, args ...any) bool
Zerof is the same as Assertions.Zero, but it accepts a format string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and continues execution.
type BoolAssertionFunc ¶
type BoolAssertionFunc = assertions.BoolAssertionFunc
BoolAssertionFunc is a common function prototype when validating a bool value. Can be useful for table driven tests.
Example ¶
// SPDX-FileCopyrightText: Copyright 2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"iter"
"slices"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // normally provided by test
isOkay := func(x int) bool {
return x >= 42
}
for tt := range boolAssertionCases() {
tt.assertion(t, isOkay(tt.arg))
}
fmt.Printf("passed: %t", !t.Failed())
}
type boolAssertionCase struct {
name string
arg int
assertion assert.BoolAssertionFunc
}
func boolAssertionCases() iter.Seq[boolAssertionCase] {
return slices.Values([]boolAssertionCase{
{"-1 is bad", -1, assert.False},
{"42 is good", 42, assert.True},
{"41 is bad", 41, assert.False},
{"45 is cool", 45, assert.True},
})
}
Output: passed: true
type Boolean ¶ added in v2.2.0
type Boolean = assertions.Boolean
Boolean is a bool or any type that can be converted to a bool.
type CollectT ¶
type CollectT = assertions.CollectT
CollectT implements the T interface and collects all errors.
CollectT is specifically intended to be used with EventuallyWith and should not be used outside of that context.
type CollectibleConditioner ¶ added in v2.4.0
type CollectibleConditioner = assertions.CollectibleConditioner
CollectibleConditioner is a function used in asynchronous condition assertions that use CollectT.
This type constraint allows for "overloaded" versions of the condition assertions (EventuallyWith).
type ComparisonAssertionFunc ¶
type ComparisonAssertionFunc = assertions.ComparisonAssertionFunc
ComparisonAssertionFunc is a common function prototype when comparing two values. Can be useful for table driven tests.
Example ¶
// SPDX-FileCopyrightText: Copyright 2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"iter"
"slices"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // normally provided by test
adder := func(x, y int) int {
return x + y
}
for tt := range comparisonFuncCases() {
tt.assertion(t, tt.expect, adder(tt.args.x, tt.args.y))
}
fmt.Printf("passed: %t", !t.Failed())
}
type args struct {
x int
y int
}
type comparisonFuncCase struct {
name string
args args
expect int
assertion assert.ComparisonAssertionFunc
}
func comparisonFuncCases() iter.Seq[comparisonFuncCase] {
return slices.Values([]comparisonFuncCase{
{"2+2=4", args{2, 2}, 4, assert.Equal},
{"2+2!=5", args{2, 2}, 5, assert.NotEqual},
{"2+3==5", args{2, 3}, 5, assert.Exactly},
})
}
Output: passed: true
type Conditioner ¶ added in v2.4.0
type Conditioner = assertions.Conditioner
Conditioner is a function used in asynchronous condition assertions.
This type constraint allows for "overloaded" versions of the condition assertions (Eventually, Consistently).
type ErrorAssertionFunc ¶
type ErrorAssertionFunc = assertions.ErrorAssertionFunc
ErrorAssertionFunc is a common function prototype when validating an error value. Can be useful for table driven tests.
Example ¶
// SPDX-FileCopyrightText: Copyright 2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package main
import (
"encoding/json"
"fmt"
"iter"
"slices"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // normally provided by test
dumbParseNum := func(input string, v any) error {
return json.Unmarshal([]byte(input), v)
}
for tt := range errorAssertionCases() {
var x float64
tt.assertion(t, dumbParseNum(tt.arg, &x))
}
fmt.Printf("passed: %t", !t.Failed())
}
type errorAssertionCase struct {
name string
arg string
assertion assert.ErrorAssertionFunc
}
func errorAssertionCases() iter.Seq[errorAssertionCase] {
return slices.Values([]errorAssertionCase{
{"1.2 is number", "1.2", assert.NoError},
{"1.2.3 not number", "1.2.3", assert.Error},
{"true is not number", "true", assert.Error},
{"3 is number", "3", assert.NoError},
})
}
Output: passed: true
type H ¶ added in v2.1.0
type H = assertions.H
H is an interface for types that implement the Helper method. This allows marking functions as test helpers, e.g. testing.T.Helper.
type Measurable ¶ added in v2.2.0
type Measurable = assertions.Measurable
Measurable is any number for which we can compute a delta (floats or integers).
This is used by InDeltaT and InEpsilonT.
NOTE: unfortunately complex64 and complex128 are not supported.
type Ordered ¶ added in v2.2.0
type Ordered = assertions.Ordered
Ordered is a standard ordered type (i.e. types that support "<": cmp.Ordered) plus []byte and time.Time.
This is used by GreaterT, GreaterOrEqualT, LessT, LessOrEqualT, IsIncreasingT, IsDecreasingT.
NOTE: since time.Time is a struct, custom types which redeclare time.Time are not supported.
type PanicAssertionFunc ¶
type PanicAssertionFunc = assertions.PanicAssertionFunc
PanicAssertionFunc is a common function prototype when validating a panic value. Can be useful for table driven tests.
Example ¶
// SPDX-FileCopyrightText: Copyright 2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"iter"
"slices"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // normally provided by test
for tt := range panicAssertionCases() {
tt.assertion(t, tt.panicFn)
}
fmt.Printf("passed: %t", !t.Failed())
}
type panicAssertionCase struct {
name string
panicFn func()
assertion assert.PanicAssertionFunc
}
func panicAssertionCases() iter.Seq[panicAssertionCase] {
return slices.Values([]panicAssertionCase{
{"with panic", func() { panic(nil) }, assert.Panics},
{"without panic", func() {}, assert.NotPanics},
})
}
Output: passed: true
type RegExp ¶ added in v2.2.0
type RegExp = assertions.RegExp
RegExp is either a text containing a regular expression to compile (string or []byte), or directly the compiled regexp.
This is used by RegexpT and NotRegexpT.
type SignedNumeric ¶ added in v2.2.0
type SignedNumeric = assertions.SignedNumeric
SignedNumeric is a signed integer or a floating point number or any type that can be converted to one of these.
type TestExampleError ¶ added in v2.3.0
type TestExampleError = assertions.TestExampleError
TestExampleError is a sentinel error type that may be used for testing.
type Text ¶ added in v2.2.0
type Text = assertions.Text
Text is any type of underlying type string or []byte.
This is used by RegexpT, NotRegexpT, JSONEqT, and YAMLEqT.
NOTE: unfortunately, []rune is not supported.
type UnsignedNumeric ¶ added in v2.2.0
type UnsignedNumeric = assertions.UnsignedNumeric
UnsignedNumeric is an unsigned integer.
NOTE: there are no unsigned floating point numbers.
type ValueAssertionFunc ¶
type ValueAssertionFunc = assertions.ValueAssertionFunc
ValueAssertionFunc is a common function prototype when validating a single value. Can be useful for table driven tests.
Example ¶
// SPDX-FileCopyrightText: Copyright 2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package main
import (
"encoding/json"
"fmt"
"iter"
"slices"
"testing"
"github.com/go-openapi/testify/v2/assert"
)
func main() {
t := new(testing.T) // normally provided by test
dumbParse := func(input string) any {
var x any
_ = json.Unmarshal([]byte(input), &x)
return x
}
for tt := range valueAssertionCases() {
tt.assertion(t, dumbParse(tt.arg))
}
fmt.Printf("passed: %t", !t.Failed())
}
type valueAssertionCase struct {
name string
arg string
assertion assert.ValueAssertionFunc
}
func valueAssertionCases() iter.Seq[valueAssertionCase] {
return slices.Values([]valueAssertionCase{
{"true is not nil", "true", assert.NotNil},
{"empty string is nil", "", assert.Nil},
{"zero is not nil", "0", assert.NotNil},
{"zero is zero", "0", assert.Zero},
{"false is zero", "false", assert.Zero},
})
}
Output: passed: true