Documentation
ΒΆ
Index ΒΆ
- func CaptureStderr(capture func(w io.Writer) error) (string, error)
- func CaptureStdout(capture func(w io.Writer) error) (string, error)
- func CaptureStdoutAndStderr(capture func(stdoutWriter, stderrWriter io.Writer) error) (stdout, stderr string, err error)
- func CompletesIn(t testRunner, duration time.Duration, f func(), msg ...any)
- func Contains(t testRunner, object, element any, msg ...any)
- func Decreasing(t testRunner, object any, msg ...any)
- func DirEmpty(t testRunner, dir string, msg ...any)
- func DirExists(t testRunner, dir string, msg ...any)
- func DirNotEmpty(t testRunner, dir string, msg ...any)
- func Equal(t testRunner, expected any, actual any, msg ...any)
- func EqualValues(t testRunner, expected any, actual any, msg ...any)
- func Error(t testRunner, err error, msg ...any)
- func ErrorIs(t testRunner, err, target error, msg ...any)
- func FailNow(t testRunner, msg ...any)
- func False(t testRunner, value any, msg ...any)
- func FileExists(t testRunner, file string, msg ...any)
- func FuzzBoolFull() []bool
- func FuzzFloat64Full() (floats []float64)
- func FuzzFloat64GenerateRandomNegative(count int, min float64) (floats []float64)
- func FuzzFloat64GenerateRandomPositive(count int, max float64) (floats []float64)
- func FuzzFloat64GenerateRandomRange(count int, min, max float64) []float64
- func FuzzIntFull() (ints []int)
- func FuzzIntGenerateRandomNegative(count, min int) (ints []int)
- func FuzzIntGenerateRandomPositive(count, max int) (ints []int)
- func FuzzIntGenerateRandomRange(count, min, max int) (ints []int)
- func FuzzStringEmailAddresses() []string
- func FuzzStringEmpty() []string
- func FuzzStringFull() (ret []string)
- func FuzzStringGenerateRandom(count, length int) (result []string)
- func FuzzStringHtmlTags() []string
- func FuzzStringLong() (testSet []string)
- func FuzzStringNumeric() []string
- func FuzzStringUsernames() []string
- func FuzzUtilDistinctSet[setType comparable](testSet []setType) []setType
- func FuzzUtilLimitSet[setType any](testSet []setType, max int) []setType
- func FuzzUtilMergeSets[setType any](sets ...[]setType) (merged []setType)
- func FuzzUtilModifySet[setType any](inputSet []setType, modifier func(index int, value setType) setType) (floats []setType)
- func FuzzUtilRunTests[setType any](t testRunner, testSet []setType, ...)
- func GetColorsEnabled() bool
- func GetDiffContextLines() int
- func GetLineNumbersEnabled() bool
- func GetRandomSeed() int64
- func GetShowStartupMessage() bool
- func Greater[T constraints.Ordered](t testRunner, object1, object2 T, msg ...any)
- func GreaterOrEqual[T constraints.Ordered](t testRunner, object1, object2 T, msg ...interface{})
- func Implements(t testRunner, interfaceObject, object any, msg ...any)
- func InRange[T number](t testRunner, value T, min T, max T, msg ...any)
- func Increasing(t testRunner, object any, msg ...any)
- func KindOf(t testRunner, expectedKind reflect.Kind, object any, msg ...any)
- func Len(t testRunner, object any, length int, msg ...any)
- func Less[T constraints.Ordered](t testRunner, object1, object2 T, msg ...any)
- func LessOrEqual[T constraints.Ordered](t testRunner, v1, v2 T, msg ...interface{})
- func Nil(t testRunner, object any, msg ...any)
- func NoDirExists(t testRunner, dir string, msg ...any)
- func NoError(t testRunner, err error, msg ...any)
- func NoFileExists(t testRunner, file string, msg ...any)
- func NoSubset[T comparable](t testRunner, list []T, subset []T, msg ...any)
- func NotCompletesIn(t testRunner, duration time.Duration, f func(), msg ...any)
- func NotContains(t testRunner, object, element any, msg ...any)
- func NotEqual(t testRunner, expected any, actual any, msg ...any)
- func NotEqualValues(t testRunner, expected any, actual any, msg ...any)
- func NotErrorIs(t testRunner, err, target error, msg ...any)
- func NotImplements(t testRunner, interfaceObject, object any, msg ...any)
- func NotInRange[T number](t testRunner, value T, min T, max T, msg ...any)
- func NotKindOf(t testRunner, kind reflect.Kind, object any, msg ...any)
- func NotNil(t testRunner, object any, msg ...any)
- func NotNumeric(t testRunner, object any, msg ...any)
- func NotPanics(t testRunner, f func(), msg ...any)
- func NotRegexp(t testRunner, regex any, txt any, msg ...any)
- func NotSameElements[T comparable](t testRunner, expected []T, actual []T, msg ...any)
- func NotUnique[elementType comparable](t testRunner, list []elementType, msg ...any)
- func NotZero(t testRunner, value any, msg ...any)
- func Numeric(t testRunner, object any, msg ...any)
- func Panics(t testRunner, f func(), msg ...any)
- func Regexp(t testRunner, regex any, txt any, msg ...any)
- func SameElements(t testRunner, expected any, actual any, msg ...any)
- func SetColorsEnabled(enabled bool)
- func SetDiffContextLines(lines int)
- func SetLineNumbersEnabled(enabled bool)
- func SetRandomSeed(seed int64)
- func SetShowStartupMessage(show bool)
- func SnapshotCreate(name string, snapshotObject any) error
- func SnapshotCreateOrValidate(t testRunner, name string, object any, msg ...any) error
- func SnapshotValidate(t testRunner, name string, actual any, msg ...any) error
- func Subset[T comparable](t testRunner, list []T, subset []T, msg ...any)
- func TestFails(t testRunner, test func(t TestingPackageWithFailFunctions), msg ...any)
- func True(t testRunner, value any, msg ...any)
- func Unique[T comparable](t testRunner, list []T, msg ...any)
- func Zero(t testRunner, value any, msg ...any)
- type TestingPackageWithFailFunctions
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func CaptureStderr ΒΆ
CaptureStderr captures everything written to stderr from a specific function. You can use this method in tests, to validate that your functions writes a string to the terminal.
Example:
stderr, err := assert.CaptureStderr(func(w io.Writer) error {
_, err := fmt.Fprint(os.Stderr, "Hello, World!")
assert.AssertNoError(t, err)
return nil
})
assert.AssertNoError(t, err)
assert.AssertEqual(t, "Hello, World!", stderr)
func CaptureStdout ΒΆ
CaptureStdout captures everything written to stdout from a specific function. You can use this method in tests, to validate that your functions writes a string to the terminal.
Example:
stdout, err := assert.CaptureStdout(func(w io.Writer) error {
fmt.Println("Hello, World!")
return nil
})
assert.AssertNoError(t, err)
assert.AssertEqual(t, "Hello, World!", stdout)
func CaptureStdoutAndStderr ΒΆ
func CaptureStdoutAndStderr(capture func(stdoutWriter, stderrWriter io.Writer) error) (stdout, stderr string, err error)
CaptureStdoutAndStderr captures everything written to stdout and stderr from a specific function. You can use this method in tests, to validate that your functions writes a string to the terminal.
Example:
stdout, stderr, err := assert.CaptureStdoutAndStderr(func(stdoutWriter, stderrWriter io.Writer) error {
fmt.Fprint(os.Stdout, "Hello")
fmt.Fprint(os.Stderr, "World")
return nil
})
assert.AssertNoError(t, err)
assert.AssertEqual(t, "Hello", stdout)
assert.AssertEqual(t, "World", stderr)
func CompletesIn ΒΆ
CompletesIn asserts that a function completes in a given time. Use this function to test that functions do not take too long to complete.
NOTE: Every system takes a different amount of time to complete a function. Do not set the duration too low, if you want consistent results.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertCompletesIn(t, 2 * time.Second, func() {
// some code that should take less than 2 seconds...
}) // => PASS
func Contains ΒΆ
Contains asserts that a string/list/array/slice/map contains the specified element.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertContains(t, []int{1,2,3}, 2)
assert.AssertContains(t, []string{"Hello", "World"}, "World")
assert.AssertContains(t, "Hello, World!", "World")
func Decreasing ΒΆ
Decreasing asserts that the values in a slice are decreasing. the test fails if the values are not in a slice or if the values are not comparable.
Valid input kinds are: []int, []int8, []int16, []int32, []int64, []uint, []uint8, []uint16, []uint32, []uint64, []float32, []float64.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertDecreasing(t, []int{1000, 137, 2, 1})
assert.AssertDecreasing(t, []float32{13.5, 7, 0.1, -10.3})
func DirEmpty ΒΆ
DirEmpty asserts that a directory is empty. The test will pass when the directory is empty or does not exist.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertDirEmpty(t, "FolderName")
func DirExists ΒΆ
DirExists asserts that a directory exists. The test will pass when the directory exists, and it's visible to the current user. The test will fail, if the path points to a file.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertDirExists(t, "FolderName")
func DirNotEmpty ΒΆ
DirNotEmpty asserts that a directory is not empty The test will pass when the directory is not empty and will fail if the directory does not exist.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertDirNotEmpty(t, "FolderName")
func Equal ΒΆ
Equal asserts that two objects are equal.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertEqual(t, "Hello, World!", "Hello, World!") assert.AssertEqual(t, true, true)
func EqualValues ΒΆ
EqualValues asserts that two objects have equal values. The order of the values is also validated.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertEqualValues(t, []string{"Hello", "World"}, []string{"Hello", "World"})
assert.AssertEqualValues(t, []int{1,2}, []int{1,2})
assert.AssertEqualValues(t, []int{1,2}, []int{2,1}) // FAILS (wrong order)
Comparing struct values:
person1 := Person{
Name: "Marvin Wendt",
Age: 20,
Gender: "male",
}
person2 := Person{
Name: "Marvin Wendt",
Age: 20,
Gender: "male",
}
assert.AssertEqualValues(t, person1, person2)
func Error ΒΆ added in v0.0.2
Error asserts that an error is not nil.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
err := errors.New("hello world")
assert.AssertError(t, err)
func ErrorIs ΒΆ
ErrorIs asserts that target is inside the error chain of err.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
var testErr = errors.New("hello world")
var testErrWrapped = fmt.Errorf("test err: %w", testErr)
assert.AssertErrorIs(t, testErrWrapped ,testErr)
func False ΒΆ
False asserts that an expression or object resolves to false.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertFalse(t, false) assert.AssertFalse(t, 1 == 2) assert.AssertFalse(t, 2 != 2) assert.AssertFalse(t, 1 > 5 && 4 < 0)
func FileExists ΒΆ
FileExists asserts that a file exists.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertFileExists(t, "./test.txt") assert.AssertFileExists(t, "./config.yaml", "the config file is missing")
func FuzzBoolFull ΒΆ
func FuzzBoolFull() []bool
FuzzBoolFull returns true and false in a boolean slice.
func FuzzFloat64Full ΒΆ
func FuzzFloat64Full() (floats []float64)
FuzzFloat64Full returns a combination of every float64 testset and some random float64s (positive and negative).
func FuzzFloat64GenerateRandomNegative ΒΆ
FuzzFloat64GenerateRandomNegative generates random negative integers with a minimum of min. If the minimum is positive, it will be converted to a negative number. If it is set to 0, there is no limit.
func FuzzFloat64GenerateRandomPositive ΒΆ
FuzzFloat64GenerateRandomPositive generates random positive integers with a maximum of max. If the maximum is 0, or below, the maximum will be set to math.MaxInt64.
func FuzzFloat64GenerateRandomRange ΒΆ
FuzzFloat64GenerateRandomRange generates random positive integers with a maximum of max. If the maximum is 0, or below, the maximum will be set to math.MaxInt64.
func FuzzIntFull ΒΆ
func FuzzIntFull() (ints []int)
FuzzIntFull returns a combination of every integer testset and some random integers (positive and negative).
func FuzzIntGenerateRandomNegative ΒΆ
FuzzIntGenerateRandomNegative generates random negative integers with a minimum of min. If the minimum is 0, or above, the maximum will be set to math.MinInt64.
func FuzzIntGenerateRandomPositive ΒΆ
FuzzIntGenerateRandomPositive generates random positive integers with a maximum of max. If the maximum is 0, or below, the maximum will be set to math.MaxInt64.
func FuzzIntGenerateRandomRange ΒΆ
FuzzIntGenerateRandomRange generates random integers with a range of min to max.
func FuzzStringEmailAddresses ΒΆ
func FuzzStringEmailAddresses() []string
FuzzStringEmailAddresses returns a test set with valid email addresses. The addresses may look like they are invalid, but they are all conform to RFC 2822 and could be used. You can use this test set to test your email validation process.
func FuzzStringEmpty ΒΆ
func FuzzStringEmpty() []string
FuzzStringEmpty returns a test set with a single empty string.
func FuzzStringFull ΒΆ
func FuzzStringFull() (ret []string)
FuzzStringFull contains all string test sets plus ten generated random strings. This test set is huge and should only be used if you want to make sure that no string, at all, can crash a process.
func FuzzStringGenerateRandom ΒΆ
FuzzStringGenerateRandom returns random strings in a test set.
func FuzzStringHtmlTags ΒΆ
func FuzzStringHtmlTags() []string
FuzzStringHtmlTags returns a test set with different html tags.
Example:
- <script>
- <script>alert('XSS')</script>
- <a href="https://github.com/MarvinJWendt/testza">link</a>
func FuzzStringLong ΒΆ
func FuzzStringLong() (testSet []string)
FuzzStringLong returns a test set with long random strings. Returns: [0]: Random string (length: 25) [1]: Random string (length: 50) [2]: Random string (length: 100) [3]: Random string (length: 1,000) [4]: Random string (length: 100,000)
func FuzzStringNumeric ΒΆ
func FuzzStringNumeric() []string
FuzzStringNumeric returns a test set with strings that are numeric. The highest number in here is "9223372036854775807", which is equal to the maxmim int64.
func FuzzStringUsernames ΒΆ
func FuzzStringUsernames() []string
FuzzStringUsernames returns a test set with usernames.
func FuzzUtilDistinctSet ΒΆ
func FuzzUtilDistinctSet[setType comparable](testSet []setType) []setType
FuzzUtilDistinctSet returns a set with removed duplicates.
Example:
uniqueSet := assert.FuzzUtilDistinctSet([]string{"A", "C", "A", "B", "A", "B", "C"})
// uniqueSet => []string{"A", "C", "B"}
func FuzzUtilLimitSet ΒΆ
FuzzUtilLimitSet returns a random sample of a test set with a maximal size.
Example:
limitedSet := assert.FuzzUtilLimitSet(assert.FuzzStringFull(), 10)
func FuzzUtilMergeSets ΒΆ
func FuzzUtilMergeSets[setType any](sets ...[]setType) (merged []setType)
FuzzUtilMergeSets merges multiple test sets into one. All test sets must have the same type.
Example:
mergedSet := assert.FuzzUtilMergeSets(assert.FuzzIntGenerateRandomNegative(3, 0), assert.FuzzIntGenerateRandomPositive(2, 0))
func FuzzUtilModifySet ΒΆ
func FuzzUtilModifySet[setType any](inputSet []setType, modifier func(index int, value setType) setType) (floats []setType)
FuzzUtilModifySet returns a modified version of a test set.
Example:
modifiedSet := assert.FuzzUtilModifySet(assert.FuzzIntFull(), func(i int, value int) int {
return i * 2 // double every value in the test set
})
func FuzzUtilRunTests ΒΆ
func FuzzUtilRunTests[setType any](t testRunner, testSet []setType, testFunc func(t *testing.T, index int, f setType))
FuzzUtilRunTests runs a test for every value in a test set. You can use the value as input parameter for your functions, to sanity test against many different cases. This ensures that your functions have a correct error handling and enables you to test against hundreds of cases easily.
Example:
assert.FuzzUtilRunTests(t, assert.FuzzStringEmailAddresses(), func(t *testing.T, index int, emailAddress string) {
// Test logic
// err := YourFunction(emailAddress)
// assert.AssertNoError(t, err)
// ...
})
func GetColorsEnabled ΒΆ
func GetColorsEnabled() bool
GetColorsEnabled returns current value of ColorsEnabled setting. ColorsEnabled controls if assert should print colored output.
func GetDiffContextLines ΒΆ
func GetDiffContextLines() int
GetDiffContextLines returns current value of DiffContextLines setting. DiffContextLines setting controls how many lines are shown around a changed diff line. If set to -1 it will show full diff.
func GetLineNumbersEnabled ΒΆ
func GetLineNumbersEnabled() bool
GetLineNumbersEnabled returns current value of LineNumbersEnabled setting. LineNumbersEnabled controls if line numbers should be printed in failing tests.
func GetRandomSeed ΒΆ
func GetRandomSeed() int64
GetRandomSeed returns current value of the random seed setting.
func GetShowStartupMessage ΒΆ
func GetShowStartupMessage() bool
GetShowStartupMessage returns current value of showStartupMessage setting. showStartupMessage setting controls if the startup message should be printed.
func Greater ΒΆ
func Greater[T constraints.Ordered](t testRunner, object1, object2 T, msg ...any)
Greater asserts that the first object is greater than the second.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertGreater(t, 5, 1) assert.AssertGreater(t, 10, -10)
func GreaterOrEqual ΒΆ
func GreaterOrEqual[T constraints.Ordered](t testRunner, object1, object2 T, msg ...interface{})
GreaterOrEqual asserts that the first object is greater than or equal to the second.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertGreaterOrEqual(t, 5, 1) assert.AssertGreaterOrEqual(t, 10, -10)
assert.AssertGreaterOrEqual(t, 10, 10)
func Implements ΒΆ
Implements asserts that an objects implements an interface.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertImplements(t, (*YourInterface)(nil), new(YourObject)) assert.AssertImplements(t, (*fmt.Stringer)(nil), new(types.Const)) => pass
func InRange ΒΆ
func InRange[T number](t testRunner, value T, min T, max T, msg ...any)
InRange asserts that the value is in the range.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertInRange(t, 5, 1, 10)
func Increasing ΒΆ
Increasing asserts that the values in a slice are increasing. the test fails if the values are not in a slice or if the values are not comparable.
Valid input kinds are: []int, []int8, []int16, []int32, []int64, []uint, []uint8, []uint16, []uint32, []uint64, []float32, []float64.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertIncreasing(t, []int{1, 2, 137, 1000})
assert.AssertIncreasing(t, []float32{-10.3, 0.1, 7, 13.5})
func KindOf ΒΆ
KindOf asserts that the object is a type of kind exptectedKind.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertKindOf(t, reflect.Slice, []int{1,2,3})
assert.AssertKindOf(t, reflect.Slice, []string{"Hello", "World"})
assert.AssertKindOf(t, reflect.Int, 1337)
assert.AssertKindOf(t, reflect.Bool, true)
assert.AssertKindOf(t, reflect.Map, map[string]bool{})
func Len ΒΆ
Len asserts that the length of an object is equal to the given length.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertLen(t, "abc", 3)
assert.AssertLen(t, "Assert", 6)
assert.AssertLen(t, []int{1, 2, 1337, 25}, 4)
assert.AssertLen(t, map[string]int{"asd": 1, "test": 1337}, 2)
func Less ΒΆ
func Less[T constraints.Ordered](t testRunner, object1, object2 T, msg ...any)
Less asserts that the first object is less than the second.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertLess(t, 1, 5) assert.AssertLess(t, -10, 10)
func LessOrEqual ΒΆ
func LessOrEqual[T constraints.Ordered](t testRunner, v1, v2 T, msg ...interface{})
LessOrEqual asserts that the first object is less than or equal to the second.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertLessOrEqual(t, 1, 5) assert.AssertLessOrEqual(t, -10, 10) assert.AssertLessOrEqual(t, 1, 1)
func Nil ΒΆ
Nil asserts that an object is nil.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNil(t, nil)
func NoDirExists ΒΆ
NoDirExists asserts that a directory does not exists. The test will pass, if the path points to a file, as a directory with the same name, cannot exist.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNoDirExists(t, "FolderName")
func NoError ΒΆ
NoError asserts that an error is nil.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
err := nil assert.AssertNoError(t, err)
func NoFileExists ΒΆ
func NoSubset ΒΆ
func NoSubset[T comparable](t testRunner, list []T, subset []T, msg ...any)
NoSubset asserts that the second parameter is not a subset of the list. The order is irrelevant.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNoSubset(t, []int{1, 2, 3}, []int{1, 7})
assert.AssertNoSubset(t, []string{"Hello", "World", "Test"}, []string{"Test", "John"})
func NotCompletesIn ΒΆ
NotCompletesIn asserts that a function does not complete in a given time. Use this function to test that functions do not complete to quickly. For example if your database connection completes in under a millisecond, there might be something wrong.
NOTE: Every system takes a different amount of time to complete a function. Do not set the duration too high, if you want consistent results.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNotCompletesIn(t, 2 * time.Second, func() {
// some code that should take more than 2 seconds...
time.Sleep(3 * time.Second)
}) // => PASS
func NotContains ΒΆ
NotContains asserts that a string/list/array/slice/map does not contain the specified element.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNotContains(t, []string{"Hello", "World"}, "Spaceship")
assert.AssertNotContains(t, "Hello, World!", "Spaceship")
func NotEqual ΒΆ
NotEqual asserts that two objects are not equal.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNotEqual(t, true, false) assert.AssertNotEqual(t, "Hello", "World")
func NotEqualValues ΒΆ
NotEqualValues asserts that two objects do not have equal values.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNotEqualValues(t, []int{1,2}, []int{3,4})
Comparing struct values:
person1 := Person{
Name: "Marvin Wendt",
Age: 20,
Gender: "male",
}
person2 := Person{
Name: "Marvin Wendt",
Age: 20,
Gender: "female", // <-- CHANGED
}
assert.AssertNotEqualValues(t, person1, person2)
func NotErrorIs ΒΆ
NotErrorIs
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
var testErr = errors.New("hello world")
var test2Err = errors.New("hello world 2")
var testErrWrapped = fmt.Errorf("test err: %w", testErr)
assert.AssertNotErrorIs(t, testErrWrapped, test2Err)
func NotImplements ΒΆ
NotImplements asserts that an object does not implement an interface.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNotImplements(t, (*YourInterface)(nil), new(YourObject)) assert.AssertNotImplements(t, (*fmt.Stringer)(nil), new(types.Const)) => fail, because types.Const does implement fmt.Stringer.
func NotInRange ΒΆ
func NotInRange[T number](t testRunner, value T, min T, max T, msg ...any)
NotInRange asserts that the value is not in the range.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNotInRange(t, 5, 1, 10)
func NotKindOf ΒΆ
NotKindOf asserts that the object is not a type of kind `kind`.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNotKindOf(t, reflect.Slice, "Hello, World")
assert.AssertNotKindOf(t, reflect.Slice, true)
assert.AssertNotKindOf(t, reflect.Int, 13.37)
assert.AssertNotKindOf(t, reflect.Bool, map[string]bool{})
assert.AssertNotKindOf(t, reflect.Map, false)
func NotNil ΒΆ
NotNil asserts that an object is not nil.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNotNil(t, true) assert.AssertNotNil(t, "Hello, World!") assert.AssertNotNil(t, 0)
func NotNumeric ΒΆ
NotNumeric checks if the object is not a numeric type. Numeric types are: Int, Int8, Int16, Int32, Int64, Float32, Float64, Uint, Uint8, Uint16, Uint32, Uint64, Complex64 and Complex128.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNotNumeric(t, true) assert.AssertNotNumeric(t, "123")
func NotPanics ΒΆ
func NotPanics(t testRunner, f func(), msg ...any)
NotPanics asserts that a function does not panic.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNotPanics(t, func() {
// some code that does not call a panic...
}) // => PASS
func NotRegexp ΒΆ
NotRegexp asserts that a string does not match a given regexp.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNotRegexp(t, "ab.*", "Hello, World!")
func NotSameElements ΒΆ
func NotSameElements[T comparable](t testRunner, expected []T, actual []T, msg ...any)
NotSameElements asserts that two slices contains same elements (including pointers). The order is irrelevant.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNotSameElements(t, []string{"Hello", "World"}, []string{"Hello", "World", "World"})
assert.AssertNotSameElements(t, []int{1,2}, []int{1,2,3})
type A struct {
a string
}
assert.AssertNotSameElements(t, []*A{{a: "A"}, {a: "B"}, {a: "C"}}, []*A{{a: "A"}, {a: "B"}, {a: "C"}, {a: "D"}})
func NotUnique ΒΆ
func NotUnique[elementType comparable](t testRunner, list []elementType, msg ...any)
NotUnique asserts that the elements in a list are not unique.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNotUnique(t, []int{1, 2, 3, 3})
func NotZero ΒΆ
NotZero asserts that the value is not the zero value for it's type.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNotZero(t, 1337) assert.AssertNotZero(t, true) assert.AssertNotZero(t, "Hello, World")
func Numeric ΒΆ
Numeric asserts that the object is a numeric type. Numeric types are: Int, Int8, Int16, Int32, Int64, Float32, Float64, Uint, Uint8, Uint16, Uint32, Uint64, Complex64 and Complex128.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertNumeric(t, 123) assert.AssertNumeric(t, 1.23) assert.AssertNumeric(t, uint(123))
func Panics ΒΆ
func Panics(t testRunner, f func(), msg ...any)
Panics asserts that a function panics.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertPanics(t, func() {
// ...
panic("some panic")
}) // => PASS
func Regexp ΒΆ
Regexp asserts that a string matches a given regexp.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertRegexp(t, "^a.*c$", "abc")
func SameElements ΒΆ
SameElements asserts that two slices contains same elements (including pointers). The order is irrelevant.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertSameElements(t, []string{"Hello", "World"}, []string{"Hello", "World"})
assert.AssertSameElements(t, []int{1,2,3}, []int{1,2,3})
assert.AssertSameElements(t, []int{1,2}, []int{2,1})
type A struct {
a string
}
assert.AssertSameElements(t, []*A{{a: "A"}, {a: "B"}, {a: "C"}}, []*A{{a: "A"}, {a: "B"}, {a: "C"}})
func SetColorsEnabled ΒΆ
func SetColorsEnabled(enabled bool)
SetColorsEnabled controls if assert should print colored output. You should use this in the init() method of the package, which contains your tests.
> This setting can also be set by the command line flag --assert.disable-color.
Example:
init() {
assert.SetColorsEnabled(false) // Disable colored output
assert.SetColorsEnabled(true) // Enable colored output
}
func SetDiffContextLines ΒΆ
func SetDiffContextLines(lines int)
SetDiffContextLines controls how many lines are shown around a changed diff line. If set to -1 it will show full diff. You should use this in the init() method of the package, which contains your tests.
> This setting can also be set by the command line flag --assert.diff-context-lines.
Example:
init() {
assert.SetDiffContextLines(-1) // Show all diff lines
assert.SetDiffContextLines(3) // Show 3 lines around every changed line
}
func SetLineNumbersEnabled ΒΆ
func SetLineNumbersEnabled(enabled bool)
SetLineNumbersEnabled controls if line numbers should be printed in failing tests. You should use this in the init() method of the package, which contains your tests.
> This setting can also be set by the command line flag --assert.disable-line-numbers.
Example:
init() {
assert.SetLineNumbersEnabled(false) // Disable line numbers
assert.SetLineNumbersEnabled(true) // Enable line numbers
}
func SetRandomSeed ΒΆ
func SetRandomSeed(seed int64)
SetRandomSeed sets the seed for the random generator used in assert. Using the same seed will result in the same random sequences each time and guarantee a reproducible test run. Use this setting, if you want a 100% deterministic test. You should use this in the init() method of the package, which contains your tests.
> This setting can also be set by the command line flag --assert.seed.
Example:
init() {
assert.SetRandomSeed(1337) // Set the seed to 1337
assert.SetRandomSeed(time.Now().UnixNano()) // Set the seed back to the current time (default | non-deterministic)
}
func SetShowStartupMessage ΒΆ
func SetShowStartupMessage(show bool)
SetShowStartupMessage controls if the startup message should be printed. You should use this in the init() method of the package, which contains your tests.
> This setting can also be set by the command line flag --assert.disable-startup-message.
Example:
init() {
assert.SetShowStartupMessage(false) // Disable the startup message
assert.SetShowStartupMessage(true) // Enable the startup message
}
func SnapshotCreate ΒΆ
SnapshotCreate creates a snapshot of an object, which can be validated in future test runs. Using this function directly will override previous snapshots with the same name. You most likely want to use SnapshotCreateOrValidate.
NOTICE: \r\n will be replaced with \n to make the files consistent between operating systems.
Example:
assert.SnapshotCreate(t.Name(), objectToBeSnapshotted)
func SnapshotCreateOrValidate ΒΆ
SnapshotCreateOrValidate creates a snapshot of an object which can be used in future test runs. It is good practice to name your snapshots the same as the test they are created in. You can do that automatically by using t.Name() as the second parameter, if you are using the inbuilt test system of Go. If a snapshot already exists, the function will not create a new one, but validate the exisiting one. To re-create a snapshot, you can delete the according file in /testdata/snapshots/.
NOTICE: \r\n will be replaced with \n to make the files consistent between operating systems.
Example:
assert.SnapshotCreateOrValidate(t, t.Name(), object) assert.SnapshotCreateOrValidate(t, t.Name(), object, "Optional Message")
func SnapshotValidate ΒΆ
SnapshotValidate validates an already exisiting snapshot of an object. You most likely want to use SnapshotCreateOrValidate.
NOTICE: \r\n will be replaced with \n to make the files consistent between operating systems.
Example:
assert.SnapshotValidate(t, t.Name(), objectToBeValidated) assert.SnapshotValidate(t, t.Name(), objectToBeValidated, "Optional message")
func Subset ΒΆ
func Subset[T comparable](t testRunner, list []T, subset []T, msg ...any)
Subset asserts that the second parameter is a subset of the list. The order is irrelevant.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertSubset(t, []int{1, 2, 3}, []int{1, 2})
assert.AssertSubset(t, []string{"Hello", "World", "Test"}, []string{"Test", "World"})
func TestFails ΒΆ
func TestFails(t testRunner, test func(t TestingPackageWithFailFunctions), msg ...any)
TestFails asserts that a unit test fails. A unit test fails if one of the following methods is called in the test function: Error, Errorf, Fail, FailNow, Fatal, Fatalf
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertTestFails(t, func(t assert.TestingPackageWithFailFunctions) {
assert.AssertTrue(t, false)
}) // => Pass
assert.AssertTestFails(t, func(t assert.TestingPackageWithFailFunctions) {
// ...
t.Fail() // Or any other failing method.
}) // => Pass
func True ΒΆ
True asserts that an expression or object resolves to true.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertTrue(t, true) assert.AssertTrue(t, 1 == 1) assert.AssertTrue(t, 2 != 3) assert.AssertTrue(t, 1 > 0 && 4 < 5)
func Unique ΒΆ
func Unique[T comparable](t testRunner, list []T, msg ...any)
Unique asserts that the list contains only unique elements. The order is irrelevant.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
assert.AssertUnique(t, []int{1, 2, 3})
assert.AssertUnique(t, []string{"Hello", "World", "!"})
Types ΒΆ
type TestingPackageWithFailFunctions ΒΆ
type TestingPackageWithFailFunctions interface {
Error(args ...any)
Errorf(format string, args ...any)
Fail()
FailNow()
Fatal(args ...any)
Fatalf(format string, args ...any)
}
TestingPackageWithFailFunctions contains every function that fails a test in testing.T.