Documentation
¶
Overview ¶
Package require implements the same assertions as the assert package but stops test execution when a test fails.
Example Usage ¶
The following is a complete example using require in a standard test function:
import (
"testing"
"github.com/go-openapi/testify/v2/require"
)
func TestSomething(t *testing.T) {
var a string = "Hello"
var b string = "Hello"
require.Equal(t, a, b, "The two words should be the same.")
}
Assertions ¶
The require package have same global functions as in the assert package, but instead of returning a boolean result they call testing.T.FailNow. A consequence of this is that it must be called from the goroutine running the test function, not from other goroutines created during the test.
Every assertion function also takes an optional string message as the final argument, allowing custom error messages to be appended to the message the assertion method outputs.
Index ¶
- Variables
- func CallerInfo() []string
- func Condition(t T, comp Comparison, msgAndArgs ...any)
- func Conditionf(t T, comp Comparison, msg string, args ...any)
- func Contains(t T, s any, contains any, msgAndArgs ...any)
- func Containsf(t T, s any, contains any, msg string, args ...any)
- func DirExists(t T, path string, msgAndArgs ...any)
- func DirExistsf(t T, path string, msg string, args ...any)
- func ElementsMatch(t T, listA any, listB any, msgAndArgs ...any)
- func ElementsMatchf(t T, listA any, listB any, msg string, args ...any)
- func Empty(t T, object any, msgAndArgs ...any)
- func Emptyf(t T, object any, msg string, args ...any)
- func Equal(t T, expected any, actual any, msgAndArgs ...any)
- func EqualError(t T, theError error, errString string, msgAndArgs ...any)
- func EqualErrorf(t T, theError error, errString string, msg string, args ...any)
- func EqualExportedValues(t T, expected any, actual any, msgAndArgs ...any)
- func EqualExportedValuesf(t T, expected any, actual any, msg string, args ...any)
- func EqualValues(t T, expected any, actual any, msgAndArgs ...any)
- func EqualValuesf(t T, expected any, actual any, msg string, args ...any)
- func Equalf(t T, expected any, actual any, msg string, args ...any)
- func Error(t T, err error, msgAndArgs ...any)
- func ErrorAs(t T, err error, target any, msgAndArgs ...any)
- func ErrorAsf(t T, err error, target any, msg string, args ...any)
- func ErrorContains(t T, theError error, contains string, msgAndArgs ...any)
- func ErrorContainsf(t T, theError error, contains string, msg string, args ...any)
- func ErrorIs(t T, err error, target error, msgAndArgs ...any)
- func ErrorIsf(t T, err error, target error, msg string, args ...any)
- func Errorf(t T, err error, msg string, args ...any)
- func Eventually(t T, condition func() bool, waitFor time.Duration, tick time.Duration, ...)
- func EventuallyWithT(t T, condition func(collect *CollectT), waitFor time.Duration, ...)
- func EventuallyWithTf(t T, condition func(collect *CollectT), waitFor time.Duration, ...)
- func Eventuallyf(t T, condition func() bool, waitFor time.Duration, tick time.Duration, ...)
- func Exactly(t T, expected any, actual any, msgAndArgs ...any)
- func Exactlyf(t T, expected any, actual any, msg string, args ...any)
- func Fail(t T, failureMessage string, msgAndArgs ...any)
- func FailNow(t T, failureMessage string, msgAndArgs ...any)
- func FailNowf(t T, failureMessage string, msg string, args ...any)
- func Failf(t T, failureMessage string, msg string, args ...any)
- func False(t T, value bool, msgAndArgs ...any)
- func Falsef(t T, value bool, msg string, args ...any)
- func FileEmpty(t T, path string, msgAndArgs ...any)
- func FileEmptyf(t T, path string, msg string, args ...any)
- func FileExists(t T, path string, msgAndArgs ...any)
- func FileExistsf(t T, path string, msg string, args ...any)
- func FileNotEmpty(t T, path string, msgAndArgs ...any)
- func FileNotEmptyf(t T, path string, msg string, args ...any)
- func Greater(t T, e1 any, e2 any, msgAndArgs ...any)
- func GreaterOrEqual(t T, e1 any, e2 any, msgAndArgs ...any)
- func GreaterOrEqualf(t T, e1 any, e2 any, msg string, args ...any)
- func Greaterf(t T, e1 any, e2 any, msg string, args ...any)
- 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, ...)
- func HTTPBodyContainsf(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func HTTPBodyNotContains(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func HTTPBodyNotContainsf(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func HTTPError(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func HTTPErrorf(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func HTTPRedirect(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func HTTPRedirectf(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func HTTPStatusCode(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func HTTPStatusCodef(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func HTTPSuccess(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func HTTPSuccessf(t T, handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func Implements(t T, interfaceObject any, object any, msgAndArgs ...any)
- func Implementsf(t T, interfaceObject any, object any, msg string, args ...any)
- func InDelta(t T, expected any, actual any, delta float64, msgAndArgs ...any)
- func InDeltaMapValues(t T, expected any, actual any, delta float64, msgAndArgs ...any)
- func InDeltaMapValuesf(t T, expected any, actual any, delta float64, msg string, args ...any)
- func InDeltaSlice(t T, expected any, actual any, delta float64, msgAndArgs ...any)
- func InDeltaSlicef(t T, expected any, actual any, delta float64, msg string, args ...any)
- func InDeltaf(t T, expected any, actual any, delta float64, msg string, args ...any)
- func InEpsilon(t T, expected any, actual any, epsilon float64, msgAndArgs ...any)
- func InEpsilonSlice(t T, expected any, actual any, epsilon float64, msgAndArgs ...any)
- func InEpsilonSlicef(t T, expected any, actual any, epsilon float64, msg string, args ...any)
- func InEpsilonf(t T, expected any, actual any, epsilon float64, msg string, args ...any)
- func IsDecreasing(t T, object any, msgAndArgs ...any)
- func IsDecreasingf(t T, object any, msg string, args ...any)
- func IsIncreasing(t T, object any, msgAndArgs ...any)
- func IsIncreasingf(t T, object any, msg string, args ...any)
- func IsNonDecreasing(t T, object any, msgAndArgs ...any)
- func IsNonDecreasingf(t T, object any, msg string, args ...any)
- func IsNonIncreasing(t T, object any, msgAndArgs ...any)
- func IsNonIncreasingf(t T, object any, msg string, args ...any)
- func IsNotType(t T, theType any, object any, msgAndArgs ...any)
- func IsNotTypef(t T, theType any, object any, msg string, args ...any)
- func IsType(t T, expectedType any, object any, msgAndArgs ...any)
- func IsTypef(t T, expectedType any, object any, msg string, args ...any)
- func JSONEq(t T, expected string, actual string, msgAndArgs ...any)
- func JSONEqBytes(t T, expected []byte, actual []byte, msgAndArgs ...any)
- func JSONEqBytesf(t T, expected []byte, actual []byte, msg string, args ...any)
- func JSONEqf(t T, expected string, actual string, msg string, args ...any)
- func Len(t T, object any, length int, msgAndArgs ...any)
- func Lenf(t T, object any, length int, msg string, args ...any)
- func Less(t T, e1 any, e2 any, msgAndArgs ...any)
- func LessOrEqual(t T, e1 any, e2 any, msgAndArgs ...any)
- func LessOrEqualf(t T, e1 any, e2 any, msg string, args ...any)
- func Lessf(t T, e1 any, e2 any, msg string, args ...any)
- func Negative(t T, e any, msgAndArgs ...any)
- func Negativef(t T, e any, msg string, args ...any)
- func Never(t T, condition func() bool, waitFor time.Duration, tick time.Duration, ...)
- func Neverf(t T, condition func() bool, waitFor time.Duration, tick time.Duration, ...)
- func Nil(t T, object any, msgAndArgs ...any)
- func Nilf(t T, object any, msg string, args ...any)
- func NoDirExists(t T, path string, msgAndArgs ...any)
- func NoDirExistsf(t T, path string, msg string, args ...any)
- func NoError(t T, err error, msgAndArgs ...any)
- func NoErrorf(t T, err error, msg string, args ...any)
- func NoFileExists(t T, path string, msgAndArgs ...any)
- func NoFileExistsf(t T, path string, msg string, args ...any)
- func NotContains(t T, s any, contains any, msgAndArgs ...any)
- func NotContainsf(t T, s any, contains any, msg string, args ...any)
- func NotElementsMatch(t T, listA any, listB any, msgAndArgs ...any)
- func NotElementsMatchf(t T, listA any, listB any, msg string, args ...any)
- func NotEmpty(t T, object any, msgAndArgs ...any)
- func NotEmptyf(t T, object any, msg string, args ...any)
- func NotEqual(t T, expected any, actual any, msgAndArgs ...any)
- func NotEqualValues(t T, expected any, actual any, msgAndArgs ...any)
- func NotEqualValuesf(t T, expected any, actual any, msg string, args ...any)
- func NotEqualf(t T, expected any, actual any, msg string, args ...any)
- func NotErrorAs(t T, err error, target any, msgAndArgs ...any)
- func NotErrorAsf(t T, err error, target any, msg string, args ...any)
- func NotErrorIs(t T, err error, target error, msgAndArgs ...any)
- func NotErrorIsf(t T, err error, target error, msg string, args ...any)
- func NotImplements(t T, interfaceObject any, object any, msgAndArgs ...any)
- func NotImplementsf(t T, interfaceObject any, object any, msg string, args ...any)
- func NotNil(t T, object any, msgAndArgs ...any)
- func NotNilf(t T, object any, msg string, args ...any)
- func NotPanics(t T, f assertions.PanicTestFunc, msgAndArgs ...any)
- func NotPanicsf(t T, f assertions.PanicTestFunc, msg string, args ...any)
- func NotRegexp(t T, rx any, str any, msgAndArgs ...any)
- func NotRegexpf(t T, rx any, str any, msg string, args ...any)
- func NotSame(t T, expected any, actual any, msgAndArgs ...any)
- func NotSamef(t T, expected any, actual any, msg string, args ...any)
- func NotSubset(t T, list any, subset any, msgAndArgs ...any)
- func NotSubsetf(t T, list any, subset any, msg string, args ...any)
- func NotZero(t T, i any, msgAndArgs ...any)
- func NotZerof(t T, i any, msg string, args ...any)
- func ObjectsAreEqual(expected any, actual any) bool
- func ObjectsAreEqualValues(expected any, actual any) bool
- func Panics(t T, f assertions.PanicTestFunc, msgAndArgs ...any)
- func PanicsWithError(t T, errString string, f assertions.PanicTestFunc, msgAndArgs ...any)
- func PanicsWithErrorf(t T, errString string, f assertions.PanicTestFunc, msg string, args ...any)
- func PanicsWithValue(t T, expected any, f assertions.PanicTestFunc, msgAndArgs ...any)
- func PanicsWithValuef(t T, expected any, f assertions.PanicTestFunc, msg string, args ...any)
- func Panicsf(t T, f assertions.PanicTestFunc, msg string, args ...any)
- func Positive(t T, e any, msgAndArgs ...any)
- func Positivef(t T, e any, msg string, args ...any)
- func Regexp(t T, rx any, str any, msgAndArgs ...any)
- func Regexpf(t T, rx any, str any, msg string, args ...any)
- func Same(t T, expected any, actual any, msgAndArgs ...any)
- func Samef(t T, expected any, actual any, msg string, args ...any)
- func Subset(t T, list any, subset any, msgAndArgs ...any)
- func Subsetf(t T, list any, subset any, msg string, args ...any)
- func True(t T, value bool, msgAndArgs ...any)
- func Truef(t T, value bool, msg string, args ...any)
- func WithinDuration(t T, expected time.Time, actual time.Time, delta time.Duration, ...)
- func WithinDurationf(t T, expected time.Time, actual time.Time, delta time.Duration, msg string, ...)
- func WithinRange(t T, actual time.Time, start time.Time, end time.Time, msgAndArgs ...any)
- func WithinRangef(t T, actual time.Time, start time.Time, end time.Time, msg string, args ...any)
- func YAMLEq(t T, expected string, actual string, msgAndArgs ...any)
- func YAMLEqf(t T, expected string, actual string, msg string, args ...any)
- func Zero(t T, i any, msgAndArgs ...any)
- func Zerof(t T, i any, msg string, args ...any)
- type Assertions
- func (a *Assertions) Condition(comp Comparison, msgAndArgs ...any)
- func (a *Assertions) Conditionf(comp Comparison, msg string, args ...any)
- func (a *Assertions) Contains(s any, contains any, msgAndArgs ...any)
- func (a *Assertions) Containsf(s any, contains any, msg string, args ...any)
- func (a *Assertions) DirExists(path string, msgAndArgs ...any)
- func (a *Assertions) DirExistsf(path string, msg string, args ...any)
- func (a *Assertions) ElementsMatch(listA any, listB any, msgAndArgs ...any)
- func (a *Assertions) ElementsMatchf(listA any, listB any, msg string, args ...any)
- func (a *Assertions) Empty(object any, msgAndArgs ...any)
- func (a *Assertions) Emptyf(object any, msg string, args ...any)
- func (a *Assertions) Equal(expected any, actual any, msgAndArgs ...any)
- func (a *Assertions) EqualError(theError error, errString string, msgAndArgs ...any)
- func (a *Assertions) EqualErrorf(theError error, errString string, msg string, args ...any)
- func (a *Assertions) EqualExportedValues(expected any, actual any, msgAndArgs ...any)
- func (a *Assertions) EqualExportedValuesf(expected any, actual any, msg string, args ...any)
- func (a *Assertions) EqualValues(expected any, actual any, msgAndArgs ...any)
- func (a *Assertions) EqualValuesf(expected any, actual any, msg string, args ...any)
- func (a *Assertions) Equalf(expected any, actual any, msg string, args ...any)
- func (a *Assertions) Error(err error, msgAndArgs ...any)
- func (a *Assertions) ErrorAs(err error, target any, msgAndArgs ...any)
- func (a *Assertions) ErrorAsf(err error, target any, msg string, args ...any)
- func (a *Assertions) ErrorContains(theError error, contains string, msgAndArgs ...any)
- func (a *Assertions) ErrorContainsf(theError error, contains string, msg string, args ...any)
- func (a *Assertions) ErrorIs(err error, target error, msgAndArgs ...any)
- func (a *Assertions) ErrorIsf(err error, target error, msg string, args ...any)
- func (a *Assertions) Errorf(err error, msg string, args ...any)
- func (a *Assertions) Eventually(condition func() bool, waitFor time.Duration, tick time.Duration, ...)
- func (a *Assertions) EventuallyWithT(condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, ...)
- func (a *Assertions) EventuallyWithTf(condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, ...)
- func (a *Assertions) Eventuallyf(condition func() bool, waitFor time.Duration, tick time.Duration, msg string, ...)
- func (a *Assertions) Exactly(expected any, actual any, msgAndArgs ...any)
- func (a *Assertions) Exactlyf(expected any, actual any, msg string, args ...any)
- func (a *Assertions) Fail(failureMessage string, msgAndArgs ...any)
- func (a *Assertions) FailNow(failureMessage string, msgAndArgs ...any)
- func (a *Assertions) FailNowf(failureMessage string, msg string, args ...any)
- func (a *Assertions) Failf(failureMessage string, msg string, args ...any)
- func (a *Assertions) False(value bool, msgAndArgs ...any)
- func (a *Assertions) Falsef(value bool, msg string, args ...any)
- func (a *Assertions) FileEmpty(path string, msgAndArgs ...any)
- func (a *Assertions) FileEmptyf(path string, msg string, args ...any)
- func (a *Assertions) FileExists(path string, msgAndArgs ...any)
- func (a *Assertions) FileExistsf(path string, msg string, args ...any)
- func (a *Assertions) FileNotEmpty(path string, msgAndArgs ...any)
- func (a *Assertions) FileNotEmptyf(path string, msg string, args ...any)
- func (a *Assertions) Greater(e1 any, e2 any, msgAndArgs ...any)
- func (a *Assertions) GreaterOrEqual(e1 any, e2 any, msgAndArgs ...any)
- func (a *Assertions) GreaterOrEqualf(e1 any, e2 any, msg string, args ...any)
- func (a *Assertions) Greaterf(e1 any, e2 any, msg string, args ...any)
- func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func (a *Assertions) HTTPBodyContainsf(handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func (a *Assertions) HTTPBodyNotContains(handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func (a *Assertions) HTTPBodyNotContainsf(handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func (a *Assertions) HTTPError(handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func (a *Assertions) HTTPErrorf(handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func (a *Assertions) HTTPRedirect(handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func (a *Assertions) HTTPRedirectf(handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func (a *Assertions) HTTPStatusCode(handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func (a *Assertions) HTTPStatusCodef(handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func (a *Assertions) HTTPSuccess(handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func (a *Assertions) HTTPSuccessf(handler http.HandlerFunc, method string, url string, values url.Values, ...)
- func (a *Assertions) Implements(interfaceObject any, object any, msgAndArgs ...any)
- func (a *Assertions) Implementsf(interfaceObject any, object any, msg string, args ...any)
- func (a *Assertions) InDelta(expected any, actual any, delta float64, msgAndArgs ...any)
- func (a *Assertions) InDeltaMapValues(expected any, actual any, delta float64, msgAndArgs ...any)
- func (a *Assertions) InDeltaMapValuesf(expected any, actual any, delta float64, msg string, args ...any)
- func (a *Assertions) InDeltaSlice(expected any, actual any, delta float64, msgAndArgs ...any)
- func (a *Assertions) InDeltaSlicef(expected any, actual any, delta float64, msg string, args ...any)
- func (a *Assertions) InDeltaf(expected any, actual any, delta float64, msg string, args ...any)
- func (a *Assertions) InEpsilon(expected any, actual any, epsilon float64, msgAndArgs ...any)
- func (a *Assertions) InEpsilonSlice(expected any, actual any, epsilon float64, msgAndArgs ...any)
- func (a *Assertions) InEpsilonSlicef(expected any, actual any, epsilon float64, msg string, args ...any)
- func (a *Assertions) InEpsilonf(expected any, actual any, epsilon float64, msg string, args ...any)
- func (a *Assertions) IsDecreasing(object any, msgAndArgs ...any)
- func (a *Assertions) IsDecreasingf(object any, msg string, args ...any)
- func (a *Assertions) IsIncreasing(object any, msgAndArgs ...any)
- func (a *Assertions) IsIncreasingf(object any, msg string, args ...any)
- func (a *Assertions) IsNonDecreasing(object any, msgAndArgs ...any)
- func (a *Assertions) IsNonDecreasingf(object any, msg string, args ...any)
- func (a *Assertions) IsNonIncreasing(object any, msgAndArgs ...any)
- func (a *Assertions) IsNonIncreasingf(object any, msg string, args ...any)
- func (a *Assertions) IsNotType(theType any, object any, msgAndArgs ...any)
- func (a *Assertions) IsNotTypef(theType any, object any, msg string, args ...any)
- func (a *Assertions) IsType(expectedType any, object any, msgAndArgs ...any)
- func (a *Assertions) IsTypef(expectedType any, object any, msg string, args ...any)
- func (a *Assertions) JSONEq(expected string, actual string, msgAndArgs ...any)
- func (a *Assertions) JSONEqBytes(expected []byte, actual []byte, msgAndArgs ...any)
- func (a *Assertions) JSONEqBytesf(expected []byte, actual []byte, msg string, args ...any)
- func (a *Assertions) JSONEqf(expected string, actual string, msg string, args ...any)
- func (a *Assertions) Len(object any, length int, msgAndArgs ...any)
- func (a *Assertions) Lenf(object any, length int, msg string, args ...any)
- func (a *Assertions) Less(e1 any, e2 any, msgAndArgs ...any)
- func (a *Assertions) LessOrEqual(e1 any, e2 any, msgAndArgs ...any)
- func (a *Assertions) LessOrEqualf(e1 any, e2 any, msg string, args ...any)
- func (a *Assertions) Lessf(e1 any, e2 any, msg string, args ...any)
- func (a *Assertions) Negative(e any, msgAndArgs ...any)
- func (a *Assertions) Negativef(e any, msg string, args ...any)
- func (a *Assertions) Never(condition func() bool, waitFor time.Duration, tick time.Duration, ...)
- func (a *Assertions) Neverf(condition func() bool, waitFor time.Duration, tick time.Duration, msg string, ...)
- func (a *Assertions) Nil(object any, msgAndArgs ...any)
- func (a *Assertions) Nilf(object any, msg string, args ...any)
- func (a *Assertions) NoDirExists(path string, msgAndArgs ...any)
- func (a *Assertions) NoDirExistsf(path string, msg string, args ...any)
- func (a *Assertions) NoError(err error, msgAndArgs ...any)
- func (a *Assertions) NoErrorf(err error, msg string, args ...any)
- func (a *Assertions) NoFileExists(path string, msgAndArgs ...any)
- func (a *Assertions) NoFileExistsf(path string, msg string, args ...any)
- func (a *Assertions) NotContains(s any, contains any, msgAndArgs ...any)
- func (a *Assertions) NotContainsf(s any, contains any, msg string, args ...any)
- func (a *Assertions) NotElementsMatch(listA any, listB any, msgAndArgs ...any)
- func (a *Assertions) NotElementsMatchf(listA any, listB any, msg string, args ...any)
- func (a *Assertions) NotEmpty(object any, msgAndArgs ...any)
- func (a *Assertions) NotEmptyf(object any, msg string, args ...any)
- func (a *Assertions) NotEqual(expected any, actual any, msgAndArgs ...any)
- func (a *Assertions) NotEqualValues(expected any, actual any, msgAndArgs ...any)
- func (a *Assertions) NotEqualValuesf(expected any, actual any, msg string, args ...any)
- func (a *Assertions) NotEqualf(expected any, actual any, msg string, args ...any)
- func (a *Assertions) NotErrorAs(err error, target any, msgAndArgs ...any)
- func (a *Assertions) NotErrorAsf(err error, target any, msg string, args ...any)
- func (a *Assertions) NotErrorIs(err error, target error, msgAndArgs ...any)
- func (a *Assertions) NotErrorIsf(err error, target error, msg string, args ...any)
- func (a *Assertions) NotImplements(interfaceObject any, object any, msgAndArgs ...any)
- func (a *Assertions) NotImplementsf(interfaceObject any, object any, msg string, args ...any)
- func (a *Assertions) NotNil(object any, msgAndArgs ...any)
- func (a *Assertions) NotNilf(object any, msg string, args ...any)
- func (a *Assertions) NotPanics(f assertions.PanicTestFunc, msgAndArgs ...any)
- func (a *Assertions) NotPanicsf(f assertions.PanicTestFunc, msg string, args ...any)
- func (a *Assertions) NotRegexp(rx any, str any, msgAndArgs ...any)
- func (a *Assertions) NotRegexpf(rx any, str any, msg string, args ...any)
- func (a *Assertions) NotSame(expected any, actual any, msgAndArgs ...any)
- func (a *Assertions) NotSamef(expected any, actual any, msg string, args ...any)
- func (a *Assertions) NotSubset(list any, subset any, msgAndArgs ...any)
- func (a *Assertions) NotSubsetf(list any, subset any, msg string, args ...any)
- func (a *Assertions) NotZero(i any, msgAndArgs ...any)
- func (a *Assertions) NotZerof(i any, msg string, args ...any)
- func (a *Assertions) Panics(f assertions.PanicTestFunc, msgAndArgs ...any)
- func (a *Assertions) PanicsWithError(errString string, f assertions.PanicTestFunc, msgAndArgs ...any)
- func (a *Assertions) PanicsWithErrorf(errString string, f assertions.PanicTestFunc, msg string, args ...any)
- func (a *Assertions) PanicsWithValue(expected any, f assertions.PanicTestFunc, msgAndArgs ...any)
- func (a *Assertions) PanicsWithValuef(expected any, f assertions.PanicTestFunc, msg string, args ...any)
- func (a *Assertions) Panicsf(f assertions.PanicTestFunc, msg string, args ...any)
- func (a *Assertions) Positive(e any, msgAndArgs ...any)
- func (a *Assertions) Positivef(e any, msg string, args ...any)
- func (a *Assertions) Regexp(rx any, str any, msgAndArgs ...any)
- func (a *Assertions) Regexpf(rx any, str any, msg string, args ...any)
- func (a *Assertions) Same(expected any, actual any, msgAndArgs ...any)
- func (a *Assertions) Samef(expected any, actual any, msg string, args ...any)
- func (a *Assertions) Subset(list any, subset any, msgAndArgs ...any)
- func (a *Assertions) Subsetf(list any, subset any, msg string, args ...any)
- func (a *Assertions) True(value bool, msgAndArgs ...any)
- func (a *Assertions) Truef(value bool, msg string, args ...any)
- func (a *Assertions) WithinDuration(expected time.Time, actual time.Time, delta time.Duration, msgAndArgs ...any)
- func (a *Assertions) WithinDurationf(expected time.Time, actual time.Time, delta time.Duration, msg string, ...)
- func (a *Assertions) WithinRange(actual time.Time, start time.Time, end time.Time, msgAndArgs ...any)
- func (a *Assertions) WithinRangef(actual time.Time, start time.Time, end time.Time, msg string, args ...any)
- func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...any)
- func (a *Assertions) YAMLEqf(expected string, actual string, msg string, args ...any)
- func (a *Assertions) Zero(i any, msgAndArgs ...any)
- func (a *Assertions) Zerof(i any, msg string, args ...any)
- type BoolAssertionFunc
- type CollectT
- type Comparison
- type ComparisonAssertionFunc
- type ErrorAssertionFunc
- type H
- type PanicAssertionFunc
- type PanicTestFunc
- type T
- type TestingTdeprecated
- type ValueAssertionFunc
Examples ¶
- BoolAssertionFunc
- ComparisonAssertionFunc
- Condition
- Contains
- DirExists
- ElementsMatch
- Empty
- Equal
- EqualError
- EqualExportedValues
- EqualValues
- Error
- ErrorAs
- ErrorAssertionFunc
- ErrorContains
- ErrorIs
- Eventually
- EventuallyWithT
- Exactly
- False
- FileEmpty
- FileExists
- FileNotEmpty
- Greater
- GreaterOrEqual
- HTTPBodyContains
- HTTPBodyNotContains
- HTTPError
- HTTPRedirect
- HTTPStatusCode
- HTTPSuccess
- Implements
- InDelta
- InDeltaMapValues
- InDeltaSlice
- InEpsilon
- InEpsilonSlice
- IsDecreasing
- IsIncreasing
- IsNonDecreasing
- IsNonIncreasing
- IsNotType
- IsType
- JSONEq
- JSONEqBytes
- Len
- Less
- LessOrEqual
- Negative
- Never
- Nil
- NoDirExists
- NoError
- NoFileExists
- NotContains
- NotElementsMatch
- NotEmpty
- NotEqual
- NotEqualValues
- NotErrorAs
- NotErrorIs
- NotImplements
- NotNil
- NotPanics
- NotRegexp
- NotSame
- NotSubset
- NotZero
- PanicAssertionFunc
- Panics
- PanicsWithError
- PanicsWithValue
- Positive
- Regexp
- Same
- Subset
- True
- ValueAssertionFunc
- WithinDuration
- WithinRange
- Zero
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTest is an error instance useful for testing. // // If the code does not care about error specifics, and only needs // to return the error for example, this error should be used to make // the test code more readable. ErrTest = assertions.ErrTest )
Functions ¶
func CallerInfo ¶ added in v2.1.0
func CallerInfo() []string
CallerInfo returns an array of strings containing the file and line number of each stack frame leading from the current test to the assert call that failed.
func Condition ¶
func Condition(t T, comp Comparison, msgAndArgs ...any)
Condition uses a Comparison 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Condition(t, func() bool {
return true
})
fmt.Println("passed")
}
Output: passed
func Conditionf ¶
func Conditionf(t T, comp Comparison, msg string, args ...any)
Conditionf is the same as Condition, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Contains(t, []string{"A", "B"}, "A")
fmt.Println("passed")
}
Output: passed
func Containsf ¶
Containsf is the same as Contains, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"path/filepath"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.DirExists(t, filepath.Join(testDataPath(), "existing_dir"))
fmt.Println("passed")
}
func testDataPath() string {
return filepath.Join("..", "internal", "assertions", "testdata")
}
Output: passed
func DirExistsf ¶
DirExistsf is the same as DirExists, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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, [1, 3, 2, 3], [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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.ElementsMatch(t, []int{1, 3, 2, 3}, []int{1, 3, 3, 2})
fmt.Println("passed")
}
Output: passed
func ElementsMatchf ¶
ElementsMatchf is the same as ElementsMatch, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Empty(t, "")
fmt.Println("passed")
}
Output: passed
func Emptyf ¶
Emptyf is the same as Empty, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Equal(t, 123, 123)
fmt.Println("passed")
}
Output: passed
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.EqualError(t, require.ErrTest, "assert.ErrTest general error for testing")
fmt.Println("passed")
}
Output: passed
func EqualErrorf ¶
EqualErrorf is the same as EqualError, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.EqualExportedValues(t, &dummyStruct{A: "a", b: 1}, &dummyStruct{A: "a", b: 2})
fmt.Println("passed")
}
type dummyStruct struct {
A string
b int
}
Output: passed
func EqualExportedValuesf ¶
EqualExportedValuesf is the same as EqualExportedValues, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func EqualValues ¶
EqualValues asserts that two objects are equal or convertible to the larger type and equal.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.EqualValues(t, uint32(123), int32(123))
fmt.Println("passed")
}
Output: passed
func EqualValuesf ¶
EqualValuesf is the same as EqualValues, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func Equalf ¶
Equalf is the same as Equal, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func Error ¶
Error asserts that a function returned a non-nil error (ie. 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Error(t, require.ErrTest)
fmt.Println("passed")
}
Output: passed
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.ErrorAs(t, fmt.Errorf("wrap: %w", &dummyError{}), new(*dummyError))
fmt.Println("passed")
}
type dummyError struct {
}
func (d *dummyError) Error() string {
return "dummy error"
}
Output: passed
func ErrorAsf ¶
ErrorAsf is the same as ErrorAs, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.ErrorContains(t, require.ErrTest, "general error")
fmt.Println("passed")
}
Output: passed
func ErrorContainsf ¶
ErrorContainsf is the same as ErrorContains, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"io"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.ErrorIs(t, fmt.Errorf("wrap: %w", io.EOF), io.EOF)
fmt.Println("passed")
}
Output: passed
func ErrorIsf ¶
ErrorIsf is the same as ErrorIs, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func Errorf ¶
Errorf is the same as Error, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func Eventually ¶
func Eventually(t T, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...any)
Eventually asserts that given condition will be met in waitFor time, periodically checking target function each tick.
Usage ¶
assertions.Eventually(t, func() bool { return true; }, time.Second, 10*time.Millisecond)
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"time"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Eventually(t, func() bool {
return true
}, 100*time.Millisecond, 20*time.Millisecond)
fmt.Println("passed")
}
Output: passed
func EventuallyWithT ¶
func EventuallyWithT(t T, condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, msgAndArgs ...any)
EventuallyWithT asserts that given condition will be met in waitFor time, periodically checking target function each tick. In contrast to Eventually, it supplies a CollectT to the condition function, so that the condition function can use the CollectT to call 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 there are any). If the condition is not met before waitFor, the collected errors of the last tick are copied to t.
Usage ¶
externalValue := false
go func() {
time.Sleep(8*time.Second)
externalValue = true
}()
assertions.EventuallyWithT(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")
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"time"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.EventuallyWithT(t, func(c *assert.CollectT) {
assert.True(c, true)
}, 100*time.Millisecond, 20*time.Millisecond)
fmt.Println("passed")
}
Output: passed
func EventuallyWithTf ¶
func EventuallyWithTf(t T, condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, msg string, args ...any)
EventuallyWithTf is the same as EventuallyWithT, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func Eventuallyf ¶
func Eventuallyf(t T, condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...any)
Eventuallyf is the same as Eventually, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Exactly(t, int32(123), int32(123))
fmt.Println("passed")
}
Output: passed
func Exactlyf ¶
Exactlyf is the same as Exactly, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
func FailNow ¶
FailNow fails test.
Usage ¶
assertions.FailNow(t, "failed")
Examples ¶
failure: "failed"
Upon failure, the test T is marked as failed and stops execution.
func FailNowf ¶
FailNowf is the same as FailNow, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func Failf ¶
Failf is the same as Fail, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.False(t, 1 == 0)
fmt.Println("passed")
}
Output: passed
func Falsef ¶
Falsef is the same as False, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"path/filepath"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.FileEmpty(t, filepath.Join(testDataPath(), "empty_file"))
fmt.Println("passed")
}
func testDataPath() string {
return filepath.Join("..", "internal", "assertions", "testdata")
}
Output: passed
func FileEmptyf ¶ added in v2.1.0
FileEmptyf is the same as FileEmpty, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"path/filepath"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.FileExists(t, filepath.Join(testDataPath(), "existing_file"))
fmt.Println("passed")
}
func testDataPath() string {
return filepath.Join("..", "internal", "assertions", "testdata")
}
Output: passed
func FileExistsf ¶
FileExistsf is the same as FileExists, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"path/filepath"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.FileNotEmpty(t, filepath.Join(testDataPath(), "existing_file"))
fmt.Println("passed")
}
func testDataPath() string {
return filepath.Join("..", "internal", "assertions", "testdata")
}
Output: passed
func FileNotEmptyf ¶ added in v2.1.0
FileNotEmptyf is the same as FileNotEmpty, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func Greater ¶
Greater asserts that the first element is strictly greater than the second.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Greater(t, 2, 1)
fmt.Println("passed")
}
Output: passed
func GreaterOrEqual ¶
GreaterOrEqual asserts that the first element is greater than or equal to the second.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.GreaterOrEqual(t, 2, 1)
fmt.Println("passed")
}
Output: passed
func GreaterOrEqualf ¶
GreaterOrEqualf is the same as GreaterOrEqual, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func Greaterf ¶
Greaterf is the same as Greater, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func HTTPBody ¶ added in v2.1.0
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)
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 stops execution.
Example ¶
package main
import (
"fmt"
"net/http"
"net/url"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.HTTPBodyContains(t, httpBody, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!")
fmt.Println("passed")
}
func httpBody(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
_, _ = fmt.Fprintf(w, "Hello, %s!", name)
}
Output: passed
func HTTPBodyContainsf ¶
func HTTPBodyContainsf(t T, handler http.HandlerFunc, method string, url string, values url.Values, str any, msg string, args ...any)
HTTPBodyContainsf is the same as HTTPBodyContains, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func HTTPBodyNotContains ¶
func HTTPBodyNotContains(t T, handler http.HandlerFunc, method string, url string, values url.Values, str any, msgAndArgs ...any)
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 stops execution.
Example ¶
package main
import (
"fmt"
"net/http"
"net/url"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.HTTPBodyNotContains(t, httpBody, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, Bob!")
fmt.Println("passed")
}
func httpBody(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
_, _ = fmt.Fprintf(w, "Hello, %s!", name)
}
Output: passed
func HTTPBodyNotContainsf ¶
func HTTPBodyNotContainsf(t T, handler http.HandlerFunc, method string, url string, values url.Values, str any, msg string, args ...any)
HTTPBodyNotContainsf is the same as HTTPBodyNotContains, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func HTTPError ¶
func HTTPError(t T, handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...any)
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 stops execution.
Example ¶
package main
import (
"fmt"
"net/http"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.HTTPError(t, httpError, "GET", "/", nil)
fmt.Println("passed")
}
func httpError(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}
Output: passed
func HTTPErrorf ¶
func HTTPErrorf(t T, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...any)
HTTPErrorf is the same as HTTPError, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func HTTPRedirect ¶
func HTTPRedirect(t T, handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...any)
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 stops execution.
Example ¶
package main
import (
"fmt"
"net/http"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.HTTPRedirect(t, httpRedirect, "GET", "/", nil)
fmt.Println("passed")
}
func httpRedirect(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusTemporaryRedirect)
}
Output: passed
func HTTPRedirectf ¶
func HTTPRedirectf(t T, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...any)
HTTPRedirectf is the same as HTTPRedirect, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func HTTPStatusCode ¶
func HTTPStatusCode(t T, handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msgAndArgs ...any)
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 stops execution.
Example ¶
package main
import (
"fmt"
"net/http"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.HTTPStatusCode(t, httpOK, "GET", "/", nil, http.StatusOK)
fmt.Println("passed")
}
func httpOK(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}
Output: passed
func HTTPStatusCodef ¶
func HTTPStatusCodef(t T, handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msg string, args ...any)
HTTPStatusCodef is the same as HTTPStatusCode, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func HTTPSuccess ¶
func HTTPSuccess(t T, handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...any)
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 stops execution.
Example ¶
package main
import (
"fmt"
"net/http"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.HTTPSuccess(t, httpOK, "GET", "/", nil)
fmt.Println("passed")
}
func httpOK(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}
Output: passed
func HTTPSuccessf ¶
func HTTPSuccessf(t T, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...any)
HTTPSuccessf is the same as HTTPSuccess, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Implements(t, ptr(dummyInterface), new(testing.T))
fmt.Println("passed")
}
//nolint:gochecknoglobals // this is on purpose to share a common pointer when testing
var (
staticVar = "static string"
dummyInterface require.T
)
func ptr[T any](value T) *T {
p := value
return &p
}
Output: passed
func Implementsf ¶
Implementsf is the same as Implements, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func InDelta ¶
InDelta asserts that the two numerals are within delta of each other.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.InDelta(t, 1.0, 1.01, 0.02)
fmt.Println("passed")
}
Output: passed
func InDeltaMapValues ¶
InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.InDeltaMapValues(t, map[string]float64{"a": 1.0}, map[string]float64{"a": 1.01}, 0.02)
fmt.Println("passed")
}
Output: passed
func InDeltaMapValuesf ¶
InDeltaMapValuesf is the same as InDeltaMapValues, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func InDeltaSlice ¶
InDeltaSlice is the same as InDelta, except it compares two slices.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.InDeltaSlice(t, []float64{1.0, 2.0}, []float64{1.01, 2.01}, 0.02)
fmt.Println("passed")
}
Output: passed
func InDeltaSlicef ¶
InDeltaSlicef is the same as InDeltaSlice, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func InDeltaf ¶
InDeltaf is the same as InDelta, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func InEpsilon ¶
InEpsilon asserts that expected and actual have a relative error less than epsilon.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.InEpsilon(t, 100.0, 101.0, 0.02)
fmt.Println("passed")
}
Output: passed
func InEpsilonSlice ¶
InEpsilonSlice is the same as InEpsilon, except it compares each value from two slices.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.InEpsilonSlice(t, []float64{100.0, 200.0}, []float64{101.0, 202.0}, 0.02)
fmt.Println("passed")
}
Output: passed
func InEpsilonSlicef ¶
InEpsilonSlicef is the same as InEpsilonSlice, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func InEpsilonf ¶
InEpsilonf is the same as InEpsilon, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func IsDecreasing ¶
IsDecreasing asserts that the collection is 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.IsDecreasing(t, []int{3, 2, 1})
fmt.Println("passed")
}
Output: passed
func IsDecreasingf ¶
IsDecreasingf is the same as IsDecreasing, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func IsIncreasing ¶
IsIncreasing asserts that the collection is 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.IsIncreasing(t, []int{1, 2, 3})
fmt.Println("passed")
}
Output: passed
func IsIncreasingf ¶
IsIncreasingf is the same as IsIncreasing, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func IsNonDecreasing ¶
IsNonDecreasing asserts that the collection is not 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, 1}
Upon failure, the test T is marked as failed and stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.IsNonDecreasing(t, []int{1, 1, 2})
fmt.Println("passed")
}
Output: passed
func IsNonDecreasingf ¶
IsNonDecreasingf is the same as IsNonDecreasing, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.IsNonIncreasing(t, []int{2, 1, 1})
fmt.Println("passed")
}
Output: passed
func IsNonIncreasingf ¶
IsNonIncreasingf is the same as IsNonIncreasing, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.IsNotType(t, int32(123), int64(456))
fmt.Println("passed")
}
Output: passed
func IsNotTypef ¶
IsNotTypef is the same as IsNotType, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.IsType(t, 123, 456)
fmt.Println("passed")
}
Output: passed
func IsTypef ¶
IsTypef is the same as IsType, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func JSONEq ¶
JSONEq asserts that two JSON strings are equivalent.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.JSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
fmt.Println("passed")
}
Output: passed
func JSONEqBytes ¶ added in v2.0.2
JSONEqBytes asserts that two JSON byte slices are equivalent.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.JSONEqBytes(t, []byte(`{"hello": "world", "foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`))
fmt.Println("passed")
}
Output: passed
func JSONEqBytesf ¶ added in v2.0.2
JSONEqBytesf is the same as JSONEqBytes, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func JSONEqf ¶
JSONEqf is the same as JSONEq, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 or a channel.
See also reflect.Len.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Len(t, []string{"A", "B"}, 2)
fmt.Println("passed")
}
Output: passed
func Lenf ¶
Lenf is the same as Len, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func Less ¶
Less asserts that the first element is strictly less than the second.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Less(t, 1, 2)
fmt.Println("passed")
}
Output: passed
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.LessOrEqual(t, 1, 2)
fmt.Println("passed")
}
Output: passed
func LessOrEqualf ¶
LessOrEqualf is the same as LessOrEqual, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func Lessf ¶
Lessf is the same as Less, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Negative(t, -1)
fmt.Println("passed")
}
Output: passed
func Negativef ¶
Negativef is the same as Negative, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func Never ¶
func Never(t T, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...any)
Never asserts that the given condition doesn't satisfy in waitFor time, periodically checking the target function each tick.
Usage ¶
assertions.Never(t, func() bool { return false; }, time.Second, 10*time.Millisecond)
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"time"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Never(t, func() bool {
return false
}, 100*time.Millisecond, 20*time.Millisecond)
fmt.Println("passed")
}
Output: passed
func Neverf ¶
func Neverf(t T, condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...any)
Neverf is the same as Never, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Nil(t, nil)
fmt.Println("passed")
}
Output: passed
func Nilf ¶
Nilf is the same as Nil, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func NoDirExists ¶
NoDirExists checks whether a directory does not exist in the given path. It fails if the path points to an existing _directory_ only.
Usage ¶
assertions.NoDirExists(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 stops execution.
Example ¶
package main
import (
"fmt"
"path/filepath"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.NoDirExists(t, filepath.Join(testDataPath(), "non_existing_dir"))
fmt.Println("passed")
}
func testDataPath() string {
return filepath.Join("..", "internal", "assertions", "testdata")
}
Output: passed
func NoDirExistsf ¶
NoDirExistsf is the same as NoDirExists, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func NoError ¶
NoError asserts that a function returned a nil error (ie. 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.NoError(t, nil)
fmt.Println("passed")
}
Output: passed
func NoErrorf ¶
NoErrorf is the same as NoError, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func NoFileExists ¶
NoFileExists checks whether a file does not exist in a given path. It fails if the path points to an existing _file_ only.
Usage ¶
assertions.NoFileExists(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 stops execution.
Example ¶
package main
import (
"fmt"
"path/filepath"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.NoFileExists(t, filepath.Join(testDataPath(), "non_existing_file"))
fmt.Println("passed")
}
func testDataPath() string {
return filepath.Join("..", "internal", "assertions", "testdata")
}
Output: passed
func NoFileExistsf ¶
NoFileExistsf is the same as NoFileExists, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.NotContains(t, []string{"A", "B"}, "C")
fmt.Println("passed")
}
Output: passed
func NotContainsf ¶
NotContainsf is the same as NotContains, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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, [1, 1, 2, 3], [1, 1, 2, 3]) -> false assertions.NotElementsMatch(t, [1, 1, 2, 3], [1, 2, 3]) -> true assertions.NotElementsMatch(t, [1, 2, 3], [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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.NotElementsMatch(t, []int{1, 2, 3}, []int{1, 2, 4})
fmt.Println("passed")
}
Output: passed
func NotElementsMatchf ¶
NotElementsMatchf is the same as NotElementsMatch, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.NotEmpty(t, "not empty")
fmt.Println("passed")
}
Output: passed
func NotEmptyf ¶
NotEmptyf is the same as NotEmpty, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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).
Examples ¶
success: 123, 456 failure: 123, 123
Upon failure, the test T is marked as failed and stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.NotEqual(t, 123, 456)
fmt.Println("passed")
}
Output: passed
func NotEqualValues ¶
NotEqualValues asserts that two objects are not equal even when converted to the same type.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.NotEqualValues(t, uint32(123), int32(456))
fmt.Println("passed")
}
Output: passed
func NotEqualValuesf ¶
NotEqualValuesf is the same as NotEqualValues, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func NotEqualf ¶
NotEqualf is the same as NotEqual, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.NotErrorAs(t, require.ErrTest, new(*dummyError))
fmt.Println("passed")
}
type dummyError struct {
}
func (d *dummyError) Error() string {
return "dummy error"
}
Output: passed
func NotErrorAsf ¶
NotErrorAsf is the same as NotErrorAs, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"io"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.NotErrorIs(t, require.ErrTest, io.EOF)
fmt.Println("passed")
}
Output: passed
func NotErrorIsf ¶
NotErrorIsf is the same as NotErrorIs, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.NotImplements(t, (*error)(nil), new(testing.T))
fmt.Println("passed")
}
Output: passed
func NotImplementsf ¶
NotImplementsf is the same as NotImplements, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.NotNil(t, "not nil")
fmt.Println("passed")
}
Output: passed
func NotNilf ¶
NotNilf is the same as NotNil, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func NotPanics ¶
func NotPanics(t T, f assertions.PanicTestFunc, msgAndArgs ...any)
NotPanics asserts that the code inside the specified PanicTestFunc 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.NotPanics(t, func() {
})
fmt.Println("passed")
}
Output: passed
func NotPanicsf ¶
func NotPanicsf(t T, f assertions.PanicTestFunc, msg string, args ...any)
NotPanicsf is the same as NotPanics, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func NotRegexp ¶
NotRegexp asserts that a specified regexp does not match a string.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.NotRegexp(t, "^start", "not starting")
fmt.Println("passed")
}
Output: passed
func NotRegexpf ¶
NotRegexpf is the same as NotRegexp, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func NotSame ¶
NotSame asserts that two pointers do not reference the same object.
Both arguments must be pointer variables. Pointer variable sameness is determined based on the equality of both type and value.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.NotSame(t, &staticVar, ptr("static string"))
fmt.Println("passed")
}
//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: passed
func NotSamef ¶
NotSamef is the same as NotSame, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.NotSubset(t, []int{1, 2, 3}, []int{4, 5})
fmt.Println("passed")
}
Output: passed
func NotSubsetf ¶
NotSubsetf is the same as NotSubset, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.NotZero(t, 1)
fmt.Println("passed")
}
Output: passed
func NotZerof ¶
NotZerof is the same as NotZero, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func ObjectsAreEqual ¶ added in v2.1.0
ObjectsAreEqual determines if two objects are considered equal.
This function does no assertion of any kind.
func ObjectsAreEqualValues ¶ added in v2.1.0
ObjectsAreEqualValues gets whether two objects are equal, or if their values are equal.
func Panics ¶
func Panics(t T, f assertions.PanicTestFunc, msgAndArgs ...any)
Panics asserts that the code inside the specified PanicTestFunc panics.
Usage ¶
assertions.Panics(t, func(){ GoCrazy() })
Examples ¶
success: func() { panic("panicking") }
failure: func() { }
Upon failure, the test T is marked as failed and stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Panics(t, func() {
panic("panicking")
})
fmt.Println("passed")
}
Output: passed
func PanicsWithError ¶
func PanicsWithError(t T, errString string, f assertions.PanicTestFunc, msgAndArgs ...any)
PanicsWithError asserts that the code inside the specified PanicTestFunc 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.PanicsWithError(t, assert.ErrTest.Error(), func() {
panic(assert.ErrTest)
})
fmt.Println("passed")
}
Output: passed
func PanicsWithErrorf ¶
func PanicsWithErrorf(t T, errString string, f assertions.PanicTestFunc, msg string, args ...any)
PanicsWithErrorf is the same as PanicsWithError, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func PanicsWithValue ¶
func PanicsWithValue(t T, expected any, f assertions.PanicTestFunc, msgAndArgs ...any)
PanicsWithValue asserts that the code inside the specified PanicTestFunc 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.PanicsWithValue(t, "panicking", func() {
panic("panicking")
})
fmt.Println("passed")
}
Output: passed
func PanicsWithValuef ¶
func PanicsWithValuef(t T, expected any, f assertions.PanicTestFunc, msg string, args ...any)
PanicsWithValuef is the same as PanicsWithValue, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func Panicsf ¶
func Panicsf(t T, f assertions.PanicTestFunc, msg string, args ...any)
Panicsf is the same as Panics, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Positive(t, 1)
fmt.Println("passed")
}
Output: passed
func Positivef ¶
Positivef is the same as Positive, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func Regexp ¶
Regexp asserts that a specified regexp matches a string.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Regexp(t, "^start", "starting")
fmt.Println("passed")
}
Output: passed
func Regexpf ¶
Regexpf is the same as Regexp, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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.
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Same(t, &staticVar, staticVarPtr)
fmt.Println("passed")
}
//nolint:gochecknoglobals // this is on purpose to share a common pointer when testing
var (
staticVar = "static string"
staticVarPtr = &staticVar
)
Output: passed
func Samef ¶
Samef is the same as Same, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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.
Usage ¶
assertions.Subset(t, [1, 2, 3], [1, 2])
assertions.Subset(t, {"x": 1, "y": 2}, {"x": 1})
assertions.Subset(t, [1, 2, 3], {1: "one", 2: "two"})
assertions.Subset(t, {"x": 1, "y": 2}, ["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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Subset(t, []int{1, 2, 3}, []int{1, 2})
fmt.Println("passed")
}
Output: passed
func Subsetf ¶
Subsetf is the same as Subset, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.True(t, 1 == 1)
fmt.Println("passed")
}
Output: passed
func Truef ¶
Truef is the same as True, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func WithinDuration ¶
func WithinDuration(t T, expected time.Time, actual time.Time, delta time.Duration, msgAndArgs ...any)
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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"time"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.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.Println("passed")
}
Output: passed
func WithinDurationf ¶
func WithinDurationf(t T, expected time.Time, actual time.Time, delta time.Duration, msg string, args ...any)
WithinDurationf is the same as WithinDuration, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"time"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.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.Println("passed")
}
Output: passed
func WithinRangef ¶
WithinRangef is the same as WithinRange, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func YAMLEq ¶
YAMLEq asserts that the first documents in the two YAML strings are equivalent.
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: "key: value", "key: value" should panic without the yaml feature enabled.
Upon failure, the test T is marked as failed and stops execution.
func YAMLEqf ¶
YAMLEqf is the same as YAMLEq, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops 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 stops execution.
Example ¶
package main
import (
"fmt"
"testing"
"github.com/go-openapi/testify/v2/require"
)
func main() {
t := new(testing.T)
require.Zero(t, 0)
fmt.Println("passed")
}
Output: passed
Types ¶
type Assertions ¶
type Assertions struct {
// contains filtered or unexported fields
}
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 stops execution.
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 Comparison, msgAndArgs ...any)
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 stops execution.
func (*Assertions) Conditionf ¶
func (a *Assertions) Conditionf(comp Comparison, msg string, args ...any)
Conditionf is the same as Assertions.Condition, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Contains ¶
func (a *Assertions) Contains(s any, contains any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) Containsf ¶
func (a *Assertions) Containsf(s any, contains any, msg string, args ...any)
Containsf is the same as Assertions.Contains, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) DirExists ¶
func (a *Assertions) DirExists(path string, msgAndArgs ...any)
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 stops execution.
func (*Assertions) DirExistsf ¶
func (a *Assertions) DirExistsf(path string, msg string, args ...any)
DirExistsf is the same as Assertions.DirExists, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) ElementsMatch ¶
func (a *Assertions) ElementsMatch(listA any, listB any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) ElementsMatchf ¶
func (a *Assertions) ElementsMatchf(listA any, listB any, msg string, args ...any)
ElementsMatchf is the same as Assertions.ElementsMatch, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Empty ¶
func (a *Assertions) Empty(object any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) Emptyf ¶
func (a *Assertions) Emptyf(object any, msg string, args ...any)
Emptyf is the same as Assertions.Empty, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Equal ¶
func (a *Assertions) Equal(expected any, actual any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) EqualError ¶
func (a *Assertions) EqualError(theError error, errString string, msgAndArgs ...any)
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 stops execution.
func (*Assertions) EqualErrorf ¶
func (a *Assertions) EqualErrorf(theError error, errString string, msg string, args ...any)
EqualErrorf is the same as Assertions.EqualError, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) EqualExportedValues ¶
func (a *Assertions) EqualExportedValues(expected any, actual any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) EqualExportedValuesf ¶
func (a *Assertions) EqualExportedValuesf(expected any, actual any, msg string, args ...any)
EqualExportedValuesf is the same as Assertions.EqualExportedValues, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) EqualValues ¶
func (a *Assertions) EqualValues(expected any, actual any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) EqualValuesf ¶
func (a *Assertions) EqualValuesf(expected any, actual any, msg string, args ...any)
EqualValuesf is the same as Assertions.EqualValues, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Equalf ¶
func (a *Assertions) Equalf(expected any, actual any, msg string, args ...any)
Equalf is the same as Assertions.Equal, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Error ¶
func (a *Assertions) Error(err error, msgAndArgs ...any)
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 stops execution.
func (*Assertions) ErrorAs ¶
func (a *Assertions) ErrorAs(err error, target any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) ErrorAsf ¶
func (a *Assertions) ErrorAsf(err error, target any, msg string, args ...any)
ErrorAsf is the same as Assertions.ErrorAs, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) ErrorContains ¶
func (a *Assertions) ErrorContains(theError error, contains string, msgAndArgs ...any)
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 stops execution.
func (*Assertions) ErrorContainsf ¶
func (a *Assertions) ErrorContainsf(theError error, contains string, msg string, args ...any)
ErrorContainsf is the same as Assertions.ErrorContains, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) ErrorIs ¶
func (a *Assertions) ErrorIs(err error, target error, msgAndArgs ...any)
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 stops execution.
func (*Assertions) ErrorIsf ¶
func (a *Assertions) ErrorIsf(err error, target error, msg string, args ...any)
ErrorIsf is the same as Assertions.ErrorIs, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Errorf ¶
func (a *Assertions) Errorf(err error, msg string, args ...any)
Errorf is the same as Assertions.Error, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Eventually ¶
func (a *Assertions) Eventually(condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...any)
Eventually is the same as Eventually, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) EventuallyWithT ¶
func (a *Assertions) EventuallyWithT(condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, msgAndArgs ...any)
EventuallyWithT is the same as EventuallyWithT, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) EventuallyWithTf ¶
func (a *Assertions) EventuallyWithTf(condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, msg string, args ...any)
EventuallyWithTf is the same as Assertions.EventuallyWithT, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Eventuallyf ¶
func (a *Assertions) Eventuallyf(condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...any)
Eventuallyf is the same as Assertions.Eventually, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Exactly ¶
func (a *Assertions) Exactly(expected any, actual any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) Exactlyf ¶
func (a *Assertions) Exactlyf(expected any, actual any, msg string, args ...any)
Exactlyf is the same as Assertions.Exactly, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Fail ¶
func (a *Assertions) Fail(failureMessage string, msgAndArgs ...any)
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 stops execution.
func (*Assertions) FailNow ¶
func (a *Assertions) FailNow(failureMessage string, msgAndArgs ...any)
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 stops execution.
func (*Assertions) FailNowf ¶
func (a *Assertions) FailNowf(failureMessage string, msg string, args ...any)
FailNowf is the same as Assertions.FailNow, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Failf ¶
func (a *Assertions) Failf(failureMessage string, msg string, args ...any)
Failf is the same as Assertions.Fail, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) False ¶
func (a *Assertions) False(value bool, msgAndArgs ...any)
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 stops execution.
func (*Assertions) Falsef ¶
func (a *Assertions) Falsef(value bool, msg string, args ...any)
Falsef is the same as Assertions.False, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) FileEmpty ¶ added in v2.1.0
func (a *Assertions) FileEmpty(path string, msgAndArgs ...any)
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 stops execution.
func (*Assertions) FileEmptyf ¶ added in v2.1.0
func (a *Assertions) FileEmptyf(path string, msg string, args ...any)
FileEmptyf is the same as Assertions.FileEmpty, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) FileExists ¶
func (a *Assertions) FileExists(path string, msgAndArgs ...any)
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 stops execution.
func (*Assertions) FileExistsf ¶
func (a *Assertions) FileExistsf(path string, msg string, args ...any)
FileExistsf is the same as Assertions.FileExists, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) FileNotEmpty ¶ added in v2.1.0
func (a *Assertions) FileNotEmpty(path string, msgAndArgs ...any)
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 stops execution.
func (*Assertions) FileNotEmptyf ¶ added in v2.1.0
func (a *Assertions) FileNotEmptyf(path string, msg string, args ...any)
FileNotEmptyf is the same as Assertions.FileNotEmpty, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Greater ¶
func (a *Assertions) Greater(e1 any, e2 any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) GreaterOrEqual ¶
func (a *Assertions) GreaterOrEqual(e1 any, e2 any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) GreaterOrEqualf ¶
func (a *Assertions) GreaterOrEqualf(e1 any, e2 any, msg string, args ...any)
GreaterOrEqualf is the same as Assertions.GreaterOrEqual, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Greaterf ¶
func (a *Assertions) Greaterf(e1 any, e2 any, msg string, args ...any)
Greaterf is the same as Assertions.Greater, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) HTTPBodyContains ¶
func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method string, url string, values url.Values, str any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) HTTPBodyContainsf ¶
func (a *Assertions) HTTPBodyContainsf(handler http.HandlerFunc, method string, url string, values url.Values, str any, msg string, args ...any)
HTTPBodyContainsf is the same as Assertions.HTTPBodyContains, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) HTTPBodyNotContains ¶
func (a *Assertions) HTTPBodyNotContains(handler http.HandlerFunc, method string, url string, values url.Values, str any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) HTTPBodyNotContainsf ¶
func (a *Assertions) HTTPBodyNotContainsf(handler http.HandlerFunc, method string, url string, values url.Values, str any, msg string, args ...any)
HTTPBodyNotContainsf is the same as Assertions.HTTPBodyNotContains, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) HTTPError ¶
func (a *Assertions) HTTPError(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...any)
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 stops execution.
func (*Assertions) HTTPErrorf ¶
func (a *Assertions) HTTPErrorf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...any)
HTTPErrorf is the same as Assertions.HTTPError, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) HTTPRedirect ¶
func (a *Assertions) HTTPRedirect(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...any)
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 stops execution.
func (*Assertions) HTTPRedirectf ¶
func (a *Assertions) HTTPRedirectf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...any)
HTTPRedirectf is the same as Assertions.HTTPRedirect, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) HTTPStatusCode ¶
func (a *Assertions) HTTPStatusCode(handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msgAndArgs ...any)
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 stops execution.
func (*Assertions) HTTPStatusCodef ¶
func (a *Assertions) HTTPStatusCodef(handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msg string, args ...any)
HTTPStatusCodef is the same as Assertions.HTTPStatusCode, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) HTTPSuccess ¶
func (a *Assertions) HTTPSuccess(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...any)
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 stops execution.
func (*Assertions) HTTPSuccessf ¶
func (a *Assertions) HTTPSuccessf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...any)
HTTPSuccessf is the same as Assertions.HTTPSuccess, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Implements ¶
func (a *Assertions) Implements(interfaceObject any, object any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) Implementsf ¶
func (a *Assertions) Implementsf(interfaceObject any, object any, msg string, args ...any)
Implementsf is the same as Assertions.Implements, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) InDelta ¶
func (a *Assertions) InDelta(expected any, actual any, delta float64, msgAndArgs ...any)
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 stops execution.
func (*Assertions) InDeltaMapValues ¶
func (a *Assertions) InDeltaMapValues(expected any, actual any, delta float64, msgAndArgs ...any)
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 stops execution.
func (*Assertions) InDeltaMapValuesf ¶
func (a *Assertions) InDeltaMapValuesf(expected any, actual any, delta float64, msg string, args ...any)
InDeltaMapValuesf is the same as Assertions.InDeltaMapValues, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) InDeltaSlice ¶
func (a *Assertions) InDeltaSlice(expected any, actual any, delta float64, msgAndArgs ...any)
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 stops execution.
func (*Assertions) InDeltaSlicef ¶
func (a *Assertions) InDeltaSlicef(expected any, actual any, delta float64, msg string, args ...any)
InDeltaSlicef is the same as Assertions.InDeltaSlice, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) InDeltaf ¶
InDeltaf is the same as Assertions.InDelta, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) InEpsilon ¶
func (a *Assertions) InEpsilon(expected any, actual any, epsilon float64, msgAndArgs ...any)
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 stops execution.
func (*Assertions) InEpsilonSlice ¶
func (a *Assertions) InEpsilonSlice(expected any, actual any, epsilon float64, msgAndArgs ...any)
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 stops execution.
func (*Assertions) InEpsilonSlicef ¶
func (a *Assertions) InEpsilonSlicef(expected any, actual any, epsilon float64, msg string, args ...any)
InEpsilonSlicef is the same as Assertions.InEpsilonSlice, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) InEpsilonf ¶
InEpsilonf is the same as Assertions.InEpsilon, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) IsDecreasing ¶
func (a *Assertions) IsDecreasing(object any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) IsDecreasingf ¶
func (a *Assertions) IsDecreasingf(object any, msg string, args ...any)
IsDecreasingf is the same as Assertions.IsDecreasing, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) IsIncreasing ¶
func (a *Assertions) IsIncreasing(object any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) IsIncreasingf ¶
func (a *Assertions) IsIncreasingf(object any, msg string, args ...any)
IsIncreasingf is the same as Assertions.IsIncreasing, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) IsNonDecreasing ¶
func (a *Assertions) IsNonDecreasing(object any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) IsNonDecreasingf ¶
func (a *Assertions) IsNonDecreasingf(object any, msg string, args ...any)
IsNonDecreasingf is the same as Assertions.IsNonDecreasing, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) IsNonIncreasing ¶
func (a *Assertions) IsNonIncreasing(object any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) IsNonIncreasingf ¶
func (a *Assertions) IsNonIncreasingf(object any, msg string, args ...any)
IsNonIncreasingf is the same as Assertions.IsNonIncreasing, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) IsNotType ¶
func (a *Assertions) IsNotType(theType any, object any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) IsNotTypef ¶
func (a *Assertions) IsNotTypef(theType any, object any, msg string, args ...any)
IsNotTypef is the same as Assertions.IsNotType, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) IsType ¶
func (a *Assertions) IsType(expectedType any, object any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) IsTypef ¶
func (a *Assertions) IsTypef(expectedType any, object any, msg string, args ...any)
IsTypef is the same as Assertions.IsType, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) JSONEq ¶
func (a *Assertions) JSONEq(expected string, actual string, msgAndArgs ...any)
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 stops execution.
func (*Assertions) JSONEqBytes ¶ added in v2.0.2
func (a *Assertions) JSONEqBytes(expected []byte, actual []byte, msgAndArgs ...any)
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 stops execution.
func (*Assertions) JSONEqBytesf ¶ added in v2.0.2
func (a *Assertions) JSONEqBytesf(expected []byte, actual []byte, msg string, args ...any)
JSONEqBytesf is the same as Assertions.JSONEqBytes, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) JSONEqf ¶
func (a *Assertions) JSONEqf(expected string, actual string, msg string, args ...any)
JSONEqf is the same as Assertions.JSONEq, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Len ¶
func (a *Assertions) Len(object any, length int, msgAndArgs ...any)
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 stops execution.
func (*Assertions) Lenf ¶
func (a *Assertions) Lenf(object any, length int, msg string, args ...any)
Lenf is the same as Assertions.Len, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Less ¶
func (a *Assertions) Less(e1 any, e2 any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) LessOrEqual ¶
func (a *Assertions) LessOrEqual(e1 any, e2 any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) LessOrEqualf ¶
func (a *Assertions) LessOrEqualf(e1 any, e2 any, msg string, args ...any)
LessOrEqualf is the same as Assertions.LessOrEqual, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Lessf ¶
func (a *Assertions) Lessf(e1 any, e2 any, msg string, args ...any)
Lessf is the same as Assertions.Less, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Negative ¶
func (a *Assertions) Negative(e any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) Negativef ¶
func (a *Assertions) Negativef(e any, msg string, args ...any)
Negativef is the same as Assertions.Negative, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Never ¶
func (a *Assertions) Never(condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...any)
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 stops execution.
func (*Assertions) Neverf ¶
func (a *Assertions) Neverf(condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...any)
Neverf is the same as Assertions.Never, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Nil ¶
func (a *Assertions) Nil(object any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) Nilf ¶
func (a *Assertions) Nilf(object any, msg string, args ...any)
Nilf is the same as Assertions.Nil, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NoDirExists ¶
func (a *Assertions) NoDirExists(path string, msgAndArgs ...any)
NoDirExists is the same as NoDirExists, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NoDirExistsf ¶
func (a *Assertions) NoDirExistsf(path string, msg string, args ...any)
NoDirExistsf is the same as Assertions.NoDirExists, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NoError ¶
func (a *Assertions) NoError(err error, msgAndArgs ...any)
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 stops execution.
func (*Assertions) NoErrorf ¶
func (a *Assertions) NoErrorf(err error, msg string, args ...any)
NoErrorf is the same as Assertions.NoError, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NoFileExists ¶
func (a *Assertions) NoFileExists(path string, msgAndArgs ...any)
NoFileExists is the same as NoFileExists, as a method rather than a package-level function.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NoFileExistsf ¶
func (a *Assertions) NoFileExistsf(path string, msg string, args ...any)
NoFileExistsf is the same as Assertions.NoFileExists, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NotContains ¶
func (a *Assertions) NotContains(s any, contains any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) NotContainsf ¶
func (a *Assertions) NotContainsf(s any, contains any, msg string, args ...any)
NotContainsf is the same as Assertions.NotContains, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NotElementsMatch ¶
func (a *Assertions) NotElementsMatch(listA any, listB any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) NotElementsMatchf ¶
func (a *Assertions) NotElementsMatchf(listA any, listB any, msg string, args ...any)
NotElementsMatchf is the same as Assertions.NotElementsMatch, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NotEmpty ¶
func (a *Assertions) NotEmpty(object any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) NotEmptyf ¶
func (a *Assertions) NotEmptyf(object any, msg string, args ...any)
NotEmptyf is the same as Assertions.NotEmpty, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NotEqual ¶
func (a *Assertions) NotEqual(expected any, actual any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) NotEqualValues ¶
func (a *Assertions) NotEqualValues(expected any, actual any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) NotEqualValuesf ¶
func (a *Assertions) NotEqualValuesf(expected any, actual any, msg string, args ...any)
NotEqualValuesf is the same as Assertions.NotEqualValues, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NotEqualf ¶
func (a *Assertions) NotEqualf(expected any, actual any, msg string, args ...any)
NotEqualf is the same as Assertions.NotEqual, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NotErrorAs ¶
func (a *Assertions) NotErrorAs(err error, target any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) NotErrorAsf ¶
func (a *Assertions) NotErrorAsf(err error, target any, msg string, args ...any)
NotErrorAsf is the same as Assertions.NotErrorAs, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NotErrorIs ¶
func (a *Assertions) NotErrorIs(err error, target error, msgAndArgs ...any)
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 stops execution.
func (*Assertions) NotErrorIsf ¶
func (a *Assertions) NotErrorIsf(err error, target error, msg string, args ...any)
NotErrorIsf is the same as Assertions.NotErrorIs, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NotImplements ¶
func (a *Assertions) NotImplements(interfaceObject any, object any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) NotImplementsf ¶
func (a *Assertions) NotImplementsf(interfaceObject any, object any, msg string, args ...any)
NotImplementsf is the same as Assertions.NotImplements, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NotNil ¶
func (a *Assertions) NotNil(object any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) NotNilf ¶
func (a *Assertions) NotNilf(object any, msg string, args ...any)
NotNilf is the same as Assertions.NotNil, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NotPanics ¶
func (a *Assertions) NotPanics(f assertions.PanicTestFunc, msgAndArgs ...any)
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 stops execution.
func (*Assertions) NotPanicsf ¶
func (a *Assertions) NotPanicsf(f assertions.PanicTestFunc, msg string, args ...any)
NotPanicsf is the same as Assertions.NotPanics, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NotRegexp ¶
func (a *Assertions) NotRegexp(rx any, str any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) NotRegexpf ¶
func (a *Assertions) NotRegexpf(rx any, str any, msg string, args ...any)
NotRegexpf is the same as Assertions.NotRegexp, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NotSame ¶
func (a *Assertions) NotSame(expected any, actual any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) NotSamef ¶
func (a *Assertions) NotSamef(expected any, actual any, msg string, args ...any)
NotSamef is the same as Assertions.NotSame, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NotSubset ¶
func (a *Assertions) NotSubset(list any, subset any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) NotSubsetf ¶
func (a *Assertions) NotSubsetf(list any, subset any, msg string, args ...any)
NotSubsetf is the same as Assertions.NotSubset, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) NotZero ¶
func (a *Assertions) NotZero(i any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) NotZerof ¶
func (a *Assertions) NotZerof(i any, msg string, args ...any)
NotZerof is the same as Assertions.NotZero, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Panics ¶
func (a *Assertions) Panics(f assertions.PanicTestFunc, msgAndArgs ...any)
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 stops execution.
func (*Assertions) PanicsWithError ¶
func (a *Assertions) PanicsWithError(errString string, f assertions.PanicTestFunc, msgAndArgs ...any)
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 stops execution.
func (*Assertions) PanicsWithErrorf ¶
func (a *Assertions) PanicsWithErrorf(errString string, f assertions.PanicTestFunc, msg string, args ...any)
PanicsWithErrorf is the same as Assertions.PanicsWithError, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) PanicsWithValue ¶
func (a *Assertions) PanicsWithValue(expected any, f assertions.PanicTestFunc, msgAndArgs ...any)
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 stops execution.
func (*Assertions) PanicsWithValuef ¶
func (a *Assertions) PanicsWithValuef(expected any, f assertions.PanicTestFunc, msg string, args ...any)
PanicsWithValuef is the same as Assertions.PanicsWithValue, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Panicsf ¶
func (a *Assertions) Panicsf(f assertions.PanicTestFunc, msg string, args ...any)
Panicsf is the same as Assertions.Panics, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Positive ¶
func (a *Assertions) Positive(e any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) Positivef ¶
func (a *Assertions) Positivef(e any, msg string, args ...any)
Positivef is the same as Assertions.Positive, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Regexp ¶
func (a *Assertions) Regexp(rx any, str any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) Regexpf ¶
func (a *Assertions) Regexpf(rx any, str any, msg string, args ...any)
Regexpf is the same as Assertions.Regexp, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Same ¶
func (a *Assertions) Same(expected any, actual any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) Samef ¶
func (a *Assertions) Samef(expected any, actual any, msg string, args ...any)
Samef is the same as Assertions.Same, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Subset ¶
func (a *Assertions) Subset(list any, subset any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) Subsetf ¶
func (a *Assertions) Subsetf(list any, subset any, msg string, args ...any)
Subsetf is the same as Assertions.Subset, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) True ¶
func (a *Assertions) True(value bool, msgAndArgs ...any)
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 stops execution.
func (*Assertions) Truef ¶
func (a *Assertions) Truef(value bool, msg string, args ...any)
Truef is the same as Assertions.True, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) WithinDuration ¶
func (a *Assertions) WithinDuration(expected time.Time, actual time.Time, delta time.Duration, msgAndArgs ...any)
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 stops execution.
func (*Assertions) WithinDurationf ¶
func (a *Assertions) WithinDurationf(expected time.Time, actual time.Time, delta time.Duration, msg string, args ...any)
WithinDurationf is the same as Assertions.WithinDuration, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) WithinRange ¶
func (a *Assertions) WithinRange(actual time.Time, start time.Time, end time.Time, msgAndArgs ...any)
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 stops execution.
func (*Assertions) WithinRangef ¶
func (a *Assertions) WithinRangef(actual time.Time, start time.Time, end time.Time, msg string, args ...any)
WithinRangef is the same as Assertions.WithinRange, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) YAMLEq ¶
func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...any)
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 stops execution.
func (*Assertions) YAMLEqf ¶
func (a *Assertions) YAMLEqf(expected string, actual string, msg string, args ...any)
YAMLEqf is the same as Assertions.YAMLEq, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
func (*Assertions) Zero ¶
func (a *Assertions) Zero(i any, msgAndArgs ...any)
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 stops execution.
func (*Assertions) Zerof ¶
func (a *Assertions) Zerof(i any, msg string, args ...any)
Zerof is the same as Assertions.Zero, but accepts a format msg string to format arguments like fmt.Printf.
Upon failure, the test T is marked as failed and stops execution.
type 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/require"
)
func main() {
t := new(testing.T) // normally provided by test
isOkay := func(x int) bool {
return x >= 42
}
for tt := range boolAssertionCases() {
tt.requirement(t, isOkay(tt.arg))
}
fmt.Printf("passed: %t", !t.Failed())
}
type boolAssertionCase struct {
name string
arg int
requirement require.BoolAssertionFunc
}
func boolAssertionCases() iter.Seq[boolAssertionCase] {
return slices.Values([]boolAssertionCase{
{"-1 is bad", -1, require.False},
{"42 is good", 42, require.True},
{"41 is bad", 41, require.False},
{"45 is cool", 45, require.True},
})
}
Output: passed: true
type CollectT ¶ added in v2.1.0
type CollectT = assertions.CollectT
CollectT implements the T interface and collects all errors.
type Comparison ¶ added in v2.1.0
type Comparison = assertions.Comparison
Comparison is a custom function that returns true on success and false on failure.
type 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/require"
)
func main() {
t := new(testing.T) // normally provided by test
adder := func(x, y int) int {
return x + y
}
for tt := range comparisonFuncCases() {
tt.requirement(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
requirement require.ComparisonAssertionFunc
}
func comparisonFuncCases() iter.Seq[comparisonFuncCase] {
return slices.Values([]comparisonFuncCase{
{"2+2=4", args{2, 2}, 4, require.Equal},
{"2+2!=5", args{2, 2}, 5, require.NotEqual},
{"2+3==5", args{2, 3}, 5, require.Exactly},
})
}
Output: passed: true
type 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/require"
)
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.requirement(t, dumbParseNum(tt.arg, &x))
}
fmt.Printf("passed: %t", !t.Failed())
}
type errorAssertionCase struct {
name string
arg string
requirement require.ErrorAssertionFunc
}
func errorAssertionCases() iter.Seq[errorAssertionCase] {
return slices.Values([]errorAssertionCase{
{"1.2 is number", "1.2", require.NoError},
{"1.2.3 not number", "1.2.3", require.Error},
{"true is not number", "true", require.Error},
{"3 is number", "3", require.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.
type PanicAssertionFunc ¶ added in v2.1.0
type PanicAssertionFunc func(t T, f assertions.PanicTestFunc, msgAndArgs ...any)
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/require"
)
func main() {
t := new(testing.T) // normally provided by test
for tt := range panicAssertionCases() {
tt.requirement(t, tt.panicFn)
}
fmt.Printf("passed: %t", !t.Failed())
}
type panicAssertionCase struct {
name string
panicFn require.PanicTestFunc
requirement require.PanicAssertionFunc
}
func panicAssertionCases() iter.Seq[panicAssertionCase] {
return slices.Values([]panicAssertionCase{
{"with panic", func() { panic(nil) }, require.Panics},
{"without panic", func() {}, require.NotPanics},
})
}
Output: passed: true
type PanicTestFunc ¶ added in v2.1.0
type PanicTestFunc = assertions.PanicTestFunc
PanicTestFunc defines a func that should be passed to the assert.Panics and assert.NotPanics methods, and represents a simple func that takes no arguments, and returns nothing.
type T ¶ added in v2.1.0
type T interface {
assertions.T
FailNow()
}
T is an interface wrapper around testing.T.
type 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/require"
)
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.requirement(t, dumbParse(tt.arg))
}
fmt.Printf("passed: %t", !t.Failed())
}
type valueAssertionCase struct {
name string
arg string
requirement require.ValueAssertionFunc
}
func valueAssertionCases() iter.Seq[valueAssertionCase] {
return slices.Values([]valueAssertionCase{
{"true is not nil", "true", require.NotNil},
{"empty string is nil", "", require.Nil},
{"zero is not nil", "0", require.NotNil},
{"zero is zero", "0", require.Zero},
{"false is zero", "false", require.Zero},
})
}
Output: passed: true