Documentation ΒΆ
Overview ΒΆ
Package testza is a full-featured testing framework for Go. It integrates with the default test runner, so you can use it with the standard `go test` tool. Testza contains easy to use methods, like assertions, output capturing, mocking, and much more.
Index ΒΆ
- func AssertCompletesIn(t testRunner, duration time.Duration, f func(), msg ...interface{})
- func AssertContains(t testRunner, object, element interface{}, msg ...interface{})
- func AssertDecreasing(t testRunner, object interface{}, msg ...interface{})
- func AssertDirEmpty(t testRunner, dir string, msg ...interface{})
- func AssertDirExist(t testRunner, dir string, msg ...interface{})
- func AssertDirNotEmpty(t testRunner, dir string, msg ...interface{})
- func AssertEqual(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
- func AssertEqualValues(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
- func AssertErrorIs(t testRunner, err, target error, msg ...interface{})
- func AssertFalse(t testRunner, value interface{}, msg ...interface{})
- func AssertFileExists(t testRunner, file string, msg ...interface{})
- func AssertGreater(t testRunner, object1, object2 interface{}, msg ...interface{})
- func AssertImplements(t testRunner, interfaceObject, object interface{}, msg ...interface{})
- func AssertIncreasing(t testRunner, object interface{}, msg ...interface{})
- func AssertKindOf(t testRunner, expectedKind reflect.Kind, object interface{}, ...)
- func AssertLen(t testRunner, object interface{}, length int, msg ...interface{})
- func AssertLess(t testRunner, object1, object2 interface{}, msg ...interface{})
- func AssertNil(t testRunner, object interface{}, msg ...interface{})
- func AssertNoDirExists(t testRunner, dir string, msg ...interface{})
- func AssertNoError(t testRunner, err error, msg ...interface{})
- func AssertNoFileExists(t testRunner, file string, msg ...interface{})
- func AssertNoSubset(t testRunner, list interface{}, subset interface{}, msg ...interface{})
- func AssertNotCompletesIn(t testRunner, duration time.Duration, f func(), msg ...interface{})
- func AssertNotContains(t testRunner, object, element interface{}, msg ...interface{})
- func AssertNotEqual(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
- func AssertNotEqualValues(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
- func AssertNotErrorIs(t testRunner, err, target error, msg ...interface{})
- func AssertNotImplements(t testRunner, interfaceObject, object interface{}, msg ...interface{})
- func AssertNotKindOf(t testRunner, kind reflect.Kind, object interface{}, msg ...interface{})
- func AssertNotNil(t testRunner, object interface{}, msg ...interface{})
- func AssertNotNumeric(t testRunner, object interface{}, msg ...interface{})
- func AssertNotPanics(t testRunner, f func(), msg ...interface{})
- func AssertNotRegexp(t testRunner, regex interface{}, txt interface{}, msg ...interface{})
- func AssertNotSameElements(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
- func AssertNotZero(t testRunner, value interface{}, msg ...interface{})
- func AssertNumeric(t testRunner, object interface{}, msg ...interface{})
- func AssertPanics(t testRunner, f func(), msg ...interface{})
- func AssertRegexp(t testRunner, regex interface{}, txt interface{}, msg ...interface{})
- func AssertSameElements(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
- func AssertSubset(t testRunner, list interface{}, subset interface{}, msg ...interface{})
- func AssertTestFails(t testRunner, test func(t TestingPackageWithFailFunctions), msg ...interface{})
- func AssertTrue(t testRunner, value interface{}, msg ...interface{})
- func AssertZero(t testRunner, value interface{}, msg ...interface{})
- 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 FuzzInputBoolFull() []bool
- func FuzzInputBoolModify(inputSlice []bool, modifier func(index int, value bool) bool) (floats []bool)
- func FuzzInputBoolRunTests(t testRunner, testSet []bool, testFunc func(t *testing.T, index int, f bool))
- func FuzzInputFloat64Full() (floats []float64)
- func FuzzInputFloat64GenerateRandomNegative(count int, min float64) (floats []float64)
- func FuzzInputFloat64GenerateRandomPositive(count int, max float64) (floats []float64)
- func FuzzInputFloat64GenerateRandomRange(count int, min, max float64) (floats []float64)
- func FuzzInputFloat64Modify(inputSlice []float64, modifier func(index int, value float64) float64) (floats []float64)
- func FuzzInputFloat64RunTests(t testRunner, testSet []float64, ...)
- func FuzzInputIntFull() (ints []int)
- func FuzzInputIntGenerateRandomNegative(count, min int) (ints []int)
- func FuzzInputIntGenerateRandomPositive(count, max int) (ints []int)
- func FuzzInputIntGenerateRandomRange(count, min, max int) (ints []int)
- func FuzzInputIntModify(inputSlice []int, modifier func(index int, value int) int) (ints []int)
- func FuzzInputIntRunTests(t testRunner, testSet []int, testFunc func(t *testing.T, index int, i int))
- func FuzzInputStringEmailAddresses() []string
- func FuzzInputStringEmpty() []string
- func FuzzInputStringFull() (ret []string)
- func FuzzInputStringGenerateRandom(count, length int) (result []string)
- func FuzzInputStringHtmlTags() []string
- func FuzzInputStringLimit(testSet []string, max int) []string
- func FuzzInputStringLong() (testSet []string)
- func FuzzInputStringModify(inputSlice []string, modifier func(index int, value string) string) (ret []string)
- func FuzzInputStringNumeric() []string
- func FuzzInputStringRunTests(t testRunner, testSet []string, ...)
- func FuzzInputStringUsernames() []string
- 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 interface{}) error
- func SnapshotCreateOrValidate(t testRunner, name string, object interface{}, msg ...interface{}) error
- func SnapshotValidate(t testRunner, name string, actual interface{}, msg ...interface{}) error
- type TestingPackageWithFailFunctions
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func AssertCompletesIn ΒΆ added in v0.1.0
AssertCompletesIn 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:
testza.AssertCompletesIn(t, 2 * time.Second, func() { // some code that should take less than 2 seconds... }) // => PASS
func AssertContains ΒΆ added in v0.1.0
func AssertContains(t testRunner, object, element interface{}, msg ...interface{})
AssertContains 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:
testza.AssertContains(t, []int{1,2,3}, 2) testza.AssertContains(t, []string{"Hello", "World"}, "World") testza.AssertContains(t, "Hello, World!", "World")
func AssertDecreasing ΒΆ added in v0.2.8
func AssertDecreasing(t testRunner, object interface{}, msg ...interface{})
AssertDecreasing 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:
testza.AssertDecreasing(t, []int{1000, 137, 2, 1}) testza.AssertDecreasing(t, []float32{13.5, 7, 0.1, -10.3})
func AssertDirEmpty ΒΆ added in v0.2.10
func AssertDirEmpty(t testRunner, dir string, msg ...interface{})
AssertDirEmpty 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:
testza.AssertDirEmpty(t, "FolderName")
func AssertDirExist ΒΆ added in v0.2.11
func AssertDirExist(t testRunner, dir string, msg ...interface{})
AssertDirExist 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:
testza.AssertDirExist(t, "FolderName")
func AssertDirNotEmpty ΒΆ added in v0.2.10
func AssertDirNotEmpty(t testRunner, dir string, msg ...interface{})
AssertDirNotEmpty 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:
testza.AssertDirNotEmpty(t, "FolderName")
func AssertEqual ΒΆ added in v0.1.0
func AssertEqual(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
AssertEqual asserts that two objects are equal.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
testza.AssertEqual(t, "Hello, World!", "Hello, World!") testza.AssertEqual(t, true, true)
func AssertEqualValues ΒΆ added in v0.1.0
func AssertEqualValues(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
AssertEqualValues 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:
testza.AssertEqualValues(t, []string{"Hello", "World"}, []string{"Hello", "World"}) testza.AssertEqualValues(t, []int{1,2}, []int{1,2}) testza.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", } testza.AssertEqualValues(t, person1, person2)
func AssertErrorIs ΒΆ added in v0.2.7
func AssertErrorIs(t testRunner, err, target error, msg ...interface{})
AssertErrorIs 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) testza.AssertErrorIs(t, testErrWrapped ,testErr)
func AssertFalse ΒΆ added in v0.1.0
func AssertFalse(t testRunner, value interface{}, msg ...interface{})
AssertFalse asserts that an expression or object resolves to false.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
testza.AssertFalse(t, false) testza.AssertFalse(t, 1 == 2) testza.AssertFalse(t, 2 != 2) testza.AssertFalse(t, 1 > 5 && 4 < 0)
func AssertFileExists ΒΆ added in v0.2.10
func AssertFileExists(t testRunner, file string, msg ...interface{})
AssertFileExists asserts that a file exists.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
testza.AssertFileExists(t, "./test.txt") testza.AssertFileExists(t, "./config.yaml", "the config file is missing")
func AssertGreater ΒΆ added in v0.1.0
func AssertGreater(t testRunner, object1, object2 interface{}, msg ...interface{})
AssertGreater 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:
testza.AssertGreater(t, 5, 1) testza.AssertGreater(t, 10, -10)
func AssertImplements ΒΆ added in v0.1.0
func AssertImplements(t testRunner, interfaceObject, object interface{}, msg ...interface{})
AssertImplements asserts that an objects implements an interface.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
testza.AssertImplements(t, (*YourInterface)(nil), new(YourObject)) testza.AssertImplements(t, (*fmt.Stringer)(nil), new(types.Const)) => pass
func AssertIncreasing ΒΆ added in v0.2.8
func AssertIncreasing(t testRunner, object interface{}, msg ...interface{})
AssertIncreasing 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:
testza.AssertIncreasing(t, []int{1, 2, 137, 1000}) testza.AssertIncreasing(t, []float32{-10.3, 0.1, 7, 13.5})
func AssertKindOf ΒΆ added in v0.1.0
AssertKindOf 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:
testza.AssertKindOf(t, reflect.Slice, []int{1,2,3}) testza.AssertKindOf(t, reflect.Slice, []string{"Hello", "World"}) testza.AssertKindOf(t, reflect.Int, 1337) testza.AssertKindOf(t, reflect.Bool, true) testza.AssertKindOf(t, reflect.Map, map[string]bool{})
func AssertLen ΒΆ added in v0.2.8
func AssertLen(t testRunner, object interface{}, length int, msg ...interface{})
AssertLen 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:
testza.AssertLen(t, "abc", 3) testza.AssertLen(t, "Assert", 6) testza.AssertLen(t, []int{1, 2, 1337, 25}, 4) testza.AssertLen(t, map[string]int{"asd": 1, "test": 1337}, 2)
func AssertLess ΒΆ added in v0.1.0
func AssertLess(t testRunner, object1, object2 interface{}, msg ...interface{})
AssertLess 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:
testza.AssertLess(t, 1, 5) testza.AssertLess(t, -10, 10)
func AssertNil ΒΆ added in v0.1.0
func AssertNil(t testRunner, object interface{}, msg ...interface{})
AssertNil asserts that an object is nil.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
testza.AssertNil(t, nil)
func AssertNoDirExists ΒΆ added in v0.2.11
func AssertNoDirExists(t testRunner, dir string, msg ...interface{})
AssertNoDirExists 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:
testza.AssertNoDirExists(t, "FolderName")
func AssertNoError ΒΆ added in v0.1.0
func AssertNoError(t testRunner, err error, msg ...interface{})
AssertNoError asserts that an error is nil.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
err := nil testza.AssertNoError(t, err)
func AssertNoFileExists ΒΆ added in v0.2.10
func AssertNoFileExists(t testRunner, file string, msg ...interface{})
func AssertNoSubset ΒΆ added in v0.2.14
func AssertNoSubset(t testRunner, list interface{}, subset interface{}, msg ...interface{})
AssertNoSubset 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:
testza.AssertNoSubset(t, []int{1, 2, 3}, []int{1, 7}) testza.AssertNoSubset(t, []string{"Hello", "World", "Test"}, []string{"Test", "John"})
func AssertNotCompletesIn ΒΆ added in v0.1.0
AssertNotCompletesIn 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:
testza.AssertNotCompletesIn(t, 2 * time.Second, func() { // some code that should take more than 2 seconds... time.Sleep(3 * time.Second) }) // => PASS
func AssertNotContains ΒΆ added in v0.1.0
func AssertNotContains(t testRunner, object, element interface{}, msg ...interface{})
AssertNotContains 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:
testza.AssertNotContains(t, []string{"Hello", "World"}, "Spaceship") testza.AssertNotContains(t, "Hello, World!", "Spaceship")
func AssertNotEqual ΒΆ added in v0.1.0
func AssertNotEqual(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
AssertNotEqual asserts that two objects are not equal.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
testza.AssertNotEqual(t, true, false) testza.AssertNotEqual(t, "Hello", "World")
func AssertNotEqualValues ΒΆ added in v0.1.0
func AssertNotEqualValues(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
AssertNotEqualValues asserts that two objects do not have equal values.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
testza.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 } testza.AssertNotEqualValues(t, person1, person2)
func AssertNotErrorIs ΒΆ added in v0.2.7
func AssertNotErrorIs(t testRunner, err, target error, msg ...interface{})
AssertNotErrorIs
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) testza.AssertNotErrorIs(t, testErrWrapped, test2Err)
func AssertNotImplements ΒΆ added in v0.1.0
func AssertNotImplements(t testRunner, interfaceObject, object interface{}, msg ...interface{})
AssertNotImplements asserts that an object does not implement an interface.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
testza.AssertNotImplements(t, (*YourInterface)(nil), new(YourObject)) testza.AssertNotImplements(t, (*fmt.Stringer)(nil), new(types.Const)) => fail, because types.Const does implement fmt.Stringer.
func AssertNotKindOf ΒΆ added in v0.1.0
AssertNotKindOf 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:
testza.AssertNotKindOf(t, reflect.Slice, "Hello, World") testza.AssertNotKindOf(t, reflect.Slice, true) testza.AssertNotKindOf(t, reflect.Int, 13.37) testza.AssertNotKindOf(t, reflect.Bool, map[string]bool{}) testza.AssertNotKindOf(t, reflect.Map, false)
func AssertNotNil ΒΆ added in v0.1.0
func AssertNotNil(t testRunner, object interface{}, msg ...interface{})
AssertNotNil asserts that an object is not nil.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
testza.AssertNotNil(t, true) testza.AssertNotNil(t, "Hello, World!") testza.AssertNotNil(t, 0)
func AssertNotNumeric ΒΆ added in v0.1.0
func AssertNotNumeric(t testRunner, object interface{}, msg ...interface{})
AssertNotNumeric 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:
testza.AssertNotNumeric(t, true) testza.AssertNotNumeric(t, "123")
func AssertNotPanics ΒΆ added in v0.2.0
func AssertNotPanics(t testRunner, f func(), msg ...interface{})
AssertNotPanics asserts that a function does not panic.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
testza.AssertNotPanics(t, func() { // some code that does not call a panic... }) // => PASS
func AssertNotRegexp ΒΆ added in v0.2.8
func AssertNotRegexp(t testRunner, regex interface{}, txt interface{}, msg ...interface{})
AssertNotRegexp 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:
testza.AssertNotRegexp(t, "ab.*", "Hello, World!")
func AssertNotSameElements ΒΆ added in v0.2.13
func AssertNotSameElements(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
AssertNotSameElements 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:
testza.AssertNotSameElements(t, []string{"Hello", "World"}, []string{"Hello", "World", "World"}) testza.AssertNotSameElements(t, []int{1,2}, []int{1,2,3}) type A struct { a string } testza.AssertNotSameElements(t, []*A{{a: "A"}, {a: "B"}, {a: "C"}}, []*A{{a: "A"}, {a: "B"}, {a: "C"}, {a: "D"}})
func AssertNotZero ΒΆ added in v0.1.0
func AssertNotZero(t testRunner, value interface{}, msg ...interface{})
AssertNotZero 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:
testza.AssertNotZero(t, 1337) testza.AssertNotZero(t, true) testza.AssertNotZero(t, "Hello, World")
func AssertNumeric ΒΆ added in v0.1.0
func AssertNumeric(t testRunner, object interface{}, msg ...interface{})
AssertNumeric 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:
testza.AssertNumeric(t, 123) testza.AssertNumeric(t, 1.23) testza.AssertNumeric(t, uint(123))
func AssertPanics ΒΆ added in v0.2.0
func AssertPanics(t testRunner, f func(), msg ...interface{})
AssertPanics asserts that a function panics.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
testza.AssertPanics(t, func() { // ... panic("some panic") }) // => PASS
func AssertRegexp ΒΆ added in v0.2.8
func AssertRegexp(t testRunner, regex interface{}, txt interface{}, msg ...interface{})
AssertRegexp asserts that a string matches a given regexp.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
testza.AssertRegexp(t, "^a.*c$", "abc")
func AssertSameElements ΒΆ added in v0.2.13
func AssertSameElements(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
AssertSameElements 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:
testza.AssertSameElements(t, []string{"Hello", "World"}, []string{"Hello", "World"}) testza.AssertSameElements(t, []int{1,2,3}, []int{1,2,3}) testza.AssertSameElements(t, []int{1,2}, []int{2,1}) type A struct { a string } testza.AssertSameElements(t, []*A{{a: "A"}, {a: "B"}, {a: "C"}}, []*A{{a: "A"}, {a: "B"}, {a: "C"}})
func AssertSubset ΒΆ added in v0.2.14
func AssertSubset(t testRunner, list interface{}, subset interface{}, msg ...interface{})
AssertSubset 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:
testza.AssertSubset(t, []int{1, 2, 3}, []int{1, 2}) testza.AssertSubset(t, []string{"Hello", "World", "Test"}, []string{"Test", "World"})
func AssertTestFails ΒΆ added in v0.2.0
func AssertTestFails(t testRunner, test func(t TestingPackageWithFailFunctions), msg ...interface{})
AssertTestFails 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:
testza.AssertTestFails(t, func(t testza.TestingPackageWithFailFunctions) { testza.AssertTrue(t, false) }) // => Pass testza.AssertTestFails(t, func(t testza.TestingPackageWithFailFunctions) { // ... t.Fail() // Or any other failing method. }) // => Pass
func AssertTrue ΒΆ added in v0.1.0
func AssertTrue(t testRunner, value interface{}, msg ...interface{})
AssertTrue asserts that an expression or object resolves to true.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
testza.AssertTrue(t, true) testza.AssertTrue(t, 1 == 1) testza.AssertTrue(t, 2 != 3) testza.AssertTrue(t, 1 > 0 && 4 < 5)
func AssertZero ΒΆ added in v0.1.0
func AssertZero(t testRunner, value interface{}, msg ...interface{})
AssertZero asserts that the value is the zero value for it's type.
When using a custom message, the same formatting as with fmt.Sprintf() is used.
Example:
testza.AssertZero(t, 0) testza.AssertZero(t, false) testza.AssertZero(t, "")
func CaptureStderr ΒΆ added in v0.1.0
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 := testza.CaptureStderr(func(w io.Writer) error { _, err := fmt.Fprint(os.Stderr, "Hello, World!") testza.AssertNoError(t, err) return nil }) testza.AssertNoError(t, err) testza.AssertEqual(t, "Hello, World!", stderr)
func CaptureStdout ΒΆ added in v0.1.0
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 := testza.CaptureStdout(func(w io.Writer) error { fmt.Println("Hello, World!") return nil }) testza.AssertNoError(t, err) testza.AssertEqual(t, "Hello, World!", stdout)
func CaptureStdoutAndStderr ΒΆ added in v0.2.6
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 := testza.CaptureStdoutAndStderr(func(stdoutWriter, stderrWriter io.Writer) error { fmt.Fprint(os.Stdout, "Hello") fmt.Fprint(os.Stderr, "World") return nil }) testza.AssertNoError(t, err) testza.AssertEqual(t, "Hello", stdout) testza.AssertEqual(t, "World", stderr)
func FuzzInputBoolFull ΒΆ added in v0.3.0
func FuzzInputBoolFull() []bool
FuzzInputBoolFull returns true and false in a boolean slice.
func FuzzInputBoolModify ΒΆ added in v0.3.0
func FuzzInputBoolModify(inputSlice []bool, modifier func(index int, value bool) bool) (floats []bool)
FuzzInputBoolModify returns a modified version of a test set.
Example:
testset := testza.FuzzInputBoolModify(testza.FuzzInputBoolFull(), func(index int, value bool) bool { return !value })
func FuzzInputBoolRunTests ΒΆ added in v0.3.0
func FuzzInputBoolRunTests(t testRunner, testSet []bool, testFunc func(t *testing.T, index int, f bool))
FuzzInputBoolRunTests runs a test for every value in a testset. 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 hunderts of cases easily.
Example:
testza.FuzzInputBoolRunTests(t, testza.FuzzInputBoolFull(), func(t *testing.T, index int, b bool) { // Test logic // err := YourFunction(b) // testza.AssertNoError(t, err) // ... })
func FuzzInputFloat64Full ΒΆ added in v0.3.0
func FuzzInputFloat64Full() (floats []float64)
FuzzInputFloat64Full returns a combination of every float64 testset and some random float64s (positive and negative).
func FuzzInputFloat64GenerateRandomNegative ΒΆ added in v0.3.0
FuzzInputFloat64GenerateRandomNegative 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 FuzzInputFloat64GenerateRandomPositive ΒΆ added in v0.3.0
FuzzInputFloat64GenerateRandomPositive 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 FuzzInputFloat64GenerateRandomRange ΒΆ added in v0.3.0
FuzzInputFloat64GenerateRandomRange 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 FuzzInputFloat64Modify ΒΆ added in v0.3.0
func FuzzInputFloat64Modify(inputSlice []float64, modifier func(index int, value float64) float64) (floats []float64)
FuzzInputFloat64Modify returns a modified version of a test set.
Example:
testset := testza.FuzzInputFloat64Modify(testza.FuzzInputFloat64Full(), func(index int, value float64) float64 { return value * 2 })
func FuzzInputFloat64RunTests ΒΆ added in v0.3.0
func FuzzInputFloat64RunTests(t testRunner, testSet []float64, testFunc func(t *testing.T, index int, f float64))
FuzzInputFloat64RunTests runs a test for every value in a testset. 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 hunderts of cases easily.
Example:
testza.FuzzInputFloat64RunTests(t, testza.FuzzInputFloat64Full(), func(t *testing.T, index int, f float64) { // Test logic // err := YourFunction(f) // testza.AssertNoError(t, err) // ... })
func FuzzInputIntFull ΒΆ added in v0.3.0
func FuzzInputIntFull() (ints []int)
FuzzInputIntFull returns a combination of every integer testset and some random integers (positive and negative).
func FuzzInputIntGenerateRandomNegative ΒΆ added in v0.3.0
FuzzInputIntGenerateRandomNegative 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 FuzzInputIntGenerateRandomPositive ΒΆ added in v0.3.0
FuzzInputIntGenerateRandomPositive 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 FuzzInputIntGenerateRandomRange ΒΆ added in v0.3.0
FuzzInputIntGenerateRandomRange generates random integers with a range of min to max.
func FuzzInputIntModify ΒΆ added in v0.3.0
FuzzInputIntModify returns a modified version of a test set.
Example:
testset := testza.FuzzInputIntModify(testza.FuzzInputIntFull(), func(index int, value int) int { return value * 2 })
func FuzzInputIntRunTests ΒΆ added in v0.3.0
func FuzzInputIntRunTests(t testRunner, testSet []int, testFunc func(t *testing.T, index int, i int))
FuzzInputIntRunTests runs a test for every value in a testset. 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 hunderts of cases easily.
Example:
testza.FuzzInputIntRunTests(t, testza.FuzzInputIntFull(), func(t *testing.T, index int, i int) { // Test logic // err := YourFunction(i) // testza.AssertNoError(t, err) // ... })
func FuzzInputStringEmailAddresses ΒΆ added in v0.3.0
func FuzzInputStringEmailAddresses() []string
FuzzInputStringEmailAddresses 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 FuzzInputStringEmpty ΒΆ added in v0.3.0
func FuzzInputStringEmpty() []string
FuzzInputStringEmpty returns a test set with a single empty string.
func FuzzInputStringFull ΒΆ added in v0.3.0
func FuzzInputStringFull() (ret []string)
FuzzInputStringFull 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 FuzzInputStringGenerateRandom ΒΆ added in v0.3.0
FuzzInputStringGenerateRandom returns random strings in a test set.
func FuzzInputStringHtmlTags ΒΆ added in v0.3.0
func FuzzInputStringHtmlTags() []string
FuzzInputStringHtmlTags returns a test set with different html tags.
Example:
- <script>
- <script>alert('XSS')</script>
- <a href="https://github.com/MarvinJWendt/testza">link</a>
func FuzzInputStringLimit ΒΆ added in v0.3.0
FuzzInputStringLimit limits a test set in size.
func FuzzInputStringLong ΒΆ added in v0.3.0
func FuzzInputStringLong() (testSet []string)
FuzzInputStringLong 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 FuzzInputStringModify ΒΆ added in v0.3.0
func FuzzInputStringModify(inputSlice []string, modifier func(index int, value string) string) (ret []string)
FuzzInputStringModify returns a modified version of a test set.
Example:
testset := testza.FuzzInputStringModify(testza.FuzzInputStringFull(), func(index int, value string) string { return value + " some suffix" })
func FuzzInputStringNumeric ΒΆ added in v0.3.0
func FuzzInputStringNumeric() []string
FuzzInputStringNumeric returns a test set with strings that are numeric. The highest number in here is "9223372036854775807", which is equal to the maxmim int64.
func FuzzInputStringRunTests ΒΆ added in v0.3.0
func FuzzInputStringRunTests(t testRunner, testSet []string, testFunc func(t *testing.T, index int, str string))
FuzzInputStringRunTests runs a test for every value in a testset. 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 hunderts of cases easily.
Example:
testza.FuzzInputStringRunTests(t, testza.FuzzInputStringFull(), func(t *testing.T, index int, str string) { // Test logic // err := YourFunction(str) // testza.AssertNoError(t, err) // ... })
func FuzzInputStringUsernames ΒΆ added in v0.3.0
func FuzzInputStringUsernames() []string
FuzzInputStringUsernames returns a test set with usernames.
func SetColorsEnabled ΒΆ added in v0.2.4
func SetColorsEnabled(enabled bool)
SetColorsEnabled controls if testza should print colored output. You should use this in the init() method of the package, which contains your tests.
Example:
init() { testza.SetColorsEnabled(false) // Disable colored output testza.SetColorsEnabled(true) // Enable colored output }
func SetDiffContextLines ΒΆ added in v0.3.2
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.
Example:
init() { testza.SetDiffContextLines(-1) // Show all diff lines testza.SetDiffContextLines(3) // Show 3 lines around every changed line }
func SetLineNumbersEnabled ΒΆ added in v0.2.4
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.
Example:
init() { testza.SetLineNumbersEnabled(false) // Disable line numbers testza.SetLineNumbersEnabled(true) // Enable line numbers }
func SetRandomSeed ΒΆ added in v0.2.11
func SetRandomSeed(seed int64)
SetRandomSeed sets the seed for the random generator used in testza. 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.
Example:
init() { testza.SetRandomSeed(1337) // Set the seed to 1337 testza.SetRandomSeed(time.Now().UnixNano()) // Set the seed back to the current time (default | non-deterministic) }
func SetShowStartupMessage ΒΆ added in v0.2.15
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.
Example:
init() { testza.SetShowStartupMessage(false) // Disable the startup message testza.SetShowStartupMessage(true) // Enable the startup message }
func SnapshotCreate ΒΆ added in v0.2.2
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:
testza.SnapshotCreate(t.Name(), objectToBeSnapshotted)
func SnapshotCreateOrValidate ΒΆ added in v0.2.2
func SnapshotCreateOrValidate(t testRunner, name string, object interface{}, msg ...interface{}) error
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:
testza.SnapshotCreateOrValidate(t, t.Name(), object) testza.SnapshotCreateOrValidate(t, t.Name(), object, "Optional Message")
func SnapshotValidate ΒΆ added in v0.2.2
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:
testza.SnapshotValidate(t, t.Name(), objectToBeValidated) testza.SnapshotValidate(t, t.Name(), objectToBeValidated, "Optional message")
Types ΒΆ
type TestingPackageWithFailFunctions ΒΆ added in v0.2.0
type TestingPackageWithFailFunctions interface { Error(args ...interface{}) Errorf(format string, args ...interface{}) Fail() FailNow() Fatal(args ...interface{}) Fatalf(format string, args ...interface{}) }
TestingPackageWithFailFunctions contains every function that fails a test in testing.T.