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 AssertEqual(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
- func AssertEqualValues(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
- func AssertFalse(t testRunner, value interface{}, msg ...interface{})
- func AssertGreater(t testRunner, object1, object2 interface{}, msg ...interface{})
- func AssertImplements(t testRunner, interfaceObject, object interface{}, msg ...interface{})
- func AssertKindOf(t testRunner, expectedKind reflect.Kind, object interface{}, ...)
- func AssertLess(t testRunner, object1, object2 interface{}, msg ...interface{})
- func AssertNil(t testRunner, object interface{}, msg ...interface{})
- func AssertNoError(t testRunner, err 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 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 AssertNotZero(t testRunner, value interface{}, msg ...interface{})
- func AssertNumeric(t testRunner, object interface{}, msg ...interface{})
- func AssertPanics(t testRunner, f func(), 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 MockInputBoolFull() []bool
- func MockInputBoolModify(inputSlice []bool, f func(index int, value bool) bool) (floats []bool)
- func MockInputBoolRunTests(t testRunner, testSet []bool, testFunc func(t *testing.T, index int, f bool))
- func MockInputFloat64Full() (floats []float64)
- func MockInputFloat64GenerateRandomNegative(count int, min float64) (floats []float64)
- func MockInputFloat64GenerateRandomPositive(count int, max float64) (floats []float64)
- func MockInputFloat64GenerateRandomRange(count int, min, max float64) (floats []float64)
- func MockInputFloat64Modify(inputSlice []float64, f func(index int, value float64) float64) (floats []float64)
- func MockInputFloat64RunTests(t testRunner, testSet []float64, ...)
- func MockInputIntFull() (ints []int)
- func MockInputIntGenerateRandomNegative(count, min int) (ints []int)
- func MockInputIntGenerateRandomPositive(count, max int) (ints []int)
- func MockInputIntGenerateRandomRange(count, min, max int) (ints []int)
- func MockInputIntModify(inputSlice []int, f func(index int, value int) int) (ints []int)
- func MockInputIntRunTests(t testRunner, testSet []int, testFunc func(t *testing.T, index int, i int))
- func MockInputStringEmailAddresses() []string
- func MockInputStringEmpty() []string
- func MockInputStringFull() (ret []string)
- func MockInputStringGenerateRandom(count, length int) (result []string)
- func MockInputStringHtmlTags() []string
- func MockInputStringLimit(testSet []string, max int) []string
- func MockInputStringLong() (testSet []string)
- func MockInputStringModify(inputSlice []string, f func(index int, value string) string) (ret []string)
- func MockInputStringNumeric() []string
- func MockInputStringRunTests(t testRunner, testSet []string, ...)
- func MockInputStringUsernames() []string
- 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.
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.
Example:
testza.AssertContains(t, []int{1,2,3}, 2) testza.AssertContains(t, []string{"Hello", "World"}, "World") testza.AssertContains(t, "Hello, World!", "World")
func AssertEqual ¶ added in v0.1.0
func AssertEqual(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
AssertEqual asserts that two objects are equal.
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.
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 AssertFalse ¶ added in v0.1.0
func AssertFalse(t testRunner, value interface{}, msg ...interface{})
AssertFalse asserts that an expression or object resolves to false.
Example:
testza.AssertFalse(t, false) testza.AssertFalse(t, 1 == 2) testza.AssertFalse(t, 2 != 2) testza.AssertFalse(t, 1 > 5 && 4 < 0)
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.
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.
Example:
testza.AssertImplements(t, (*YourInterface)(nil), new(YourObject)) testza.AssertImplements(t, (*fmt.Stringer)(nil), new(types.Const)) => pass
func AssertKindOf ¶ added in v0.1.0
AssertKindOf asserts that the object is a type of kind exptectedKind.
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.
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.
Example:
testza.AssertNil(t, nil)
func AssertNoError ¶ added in v0.1.0
func AssertNoError(t testRunner, err interface{}, msg ...interface{})
AssertNoError asserts that an error is nil.
Example:
err := nil testza.AssertNoError(t, err)
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.
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.
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.
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.
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 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.
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`.
func AssertNotNil ¶ added in v0.1.0
func AssertNotNil(t testRunner, object interface{}, msg ...interface{})
AssertNotNil asserts that an object is not nil.
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.
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.
Example:
testza.AssertNotPanics(t, func() { // some code that does not call a panic... }) // => PASS
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.
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.
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.
Example:
testza.AssertPanics(t, func() { // ... panic("some panic") }) // => PASS
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
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.
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.
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.
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.
func MockInputBoolFull ¶ added in v0.1.0
func MockInputBoolFull() []bool
MockInputBoolFull returns true and false in a boolean slice.
func MockInputBoolModify ¶ added in v0.1.0
MockInputBoolModify returns a modified version of a test set.
func MockInputBoolRunTests ¶ added in v0.1.0
func MockInputBoolRunTests(t testRunner, testSet []bool, testFunc func(t *testing.T, index int, f bool))
MockInputBoolRunTests 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.
func MockInputFloat64Full ¶ added in v0.1.0
func MockInputFloat64Full() (floats []float64)
MockInputFloat64Full returns a combination of every float64 testset and some random float64s (positive and negative).
func MockInputFloat64GenerateRandomNegative ¶ added in v0.1.0
MockInputFloat64GenerateRandomNegative 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 MockInputFloat64GenerateRandomPositive ¶ added in v0.1.0
MockInputFloat64GenerateRandomPositive 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 MockInputFloat64GenerateRandomRange ¶ added in v0.1.0
MockInputFloat64GenerateRandomRange 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 MockInputFloat64Modify ¶ added in v0.1.0
func MockInputFloat64Modify(inputSlice []float64, f func(index int, value float64) float64) (floats []float64)
MockInputFloat64Modify returns a modified version of a test set.
func MockInputFloat64RunTests ¶ added in v0.1.0
func MockInputFloat64RunTests(t testRunner, testSet []float64, testFunc func(t *testing.T, index int, f float64))
MockInputFloat64RunTests 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.
func MockInputIntFull ¶ added in v0.1.0
func MockInputIntFull() (ints []int)
MockInputIntFull returns a combination of every integer testset and some random integers (positive and negative).
func MockInputIntGenerateRandomNegative ¶ added in v0.1.0
MockInputIntGenerateRandomNegative 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 MockInputIntGenerateRandomPositive ¶ added in v0.1.0
MockInputIntGenerateRandomPositive 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 MockInputIntGenerateRandomRange ¶ added in v0.1.0
MockInputIntGenerateRandomRange generates random integers with a range of min to max.
func MockInputIntModify ¶ added in v0.1.0
MockInputIntModify returns a modified version of a test set.
func MockInputIntRunTests ¶ added in v0.1.0
func MockInputIntRunTests(t testRunner, testSet []int, testFunc func(t *testing.T, index int, i int))
MockInputIntRunTests 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.
func MockInputStringEmailAddresses ¶ added in v0.1.0
func MockInputStringEmailAddresses() []string
MockInputStringEmailAddresses returns a test set with valid email addresses.
func MockInputStringEmpty ¶ added in v0.1.0
func MockInputStringEmpty() []string
MockInputStringEmpty returns a test set with a single empty string.
func MockInputStringFull ¶ added in v0.1.0
func MockInputStringFull() (ret []string)
MockInputStringFull contains all string test sets plus ten generated random strings.
func MockInputStringGenerateRandom ¶ added in v0.1.0
MockInputStringGenerateRandom returns random StringsHelper in a test set.
func MockInputStringHtmlTags ¶ added in v0.1.0
func MockInputStringHtmlTags() []string
MockInputStringHtmlTags returns a test set with html tags.
func MockInputStringLimit ¶ added in v0.1.0
MockInputStringLimit limits a test set in size.
func MockInputStringLong ¶ added in v0.1.0
func MockInputStringLong() (testSet []string)
MockInputStringLong returns a test set with long random strings. Returns: - Random string (length: 25) - Random string (length: 50) - Random string (length: 100) - Random string (length: 1,000) - Random string (length: 100,000)
func MockInputStringModify ¶ added in v0.1.0
func MockInputStringModify(inputSlice []string, f func(index int, value string) string) (ret []string)
MockInputStringModify returns a modified version of a test set.
func MockInputStringNumeric ¶ added in v0.1.0
func MockInputStringNumeric() []string
MockInputStringNumeric returns a test set with strings that are numeric. The highest number in here is "9223372036854775807", which is equal to the maxmim int64.
func MockInputStringRunTests ¶ added in v0.1.0
func MockInputStringRunTests(t testRunner, testSet []string, testFunc func(t *testing.T, index int, str string))
MockInputStringRunTests 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.
func MockInputStringUsernames ¶ added in v0.1.0
func MockInputStringUsernames() []string
MockInputStringUsernames returns a test set with usernames.
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.