Testza is like pizza for Go - you could life without it, but why should you?
Get The Module | Documentation | Contributing | Code of Conduct
# Execute this command inside your project
go get github.com/MarvinJWendt/testza
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.
Testza is structured a bit differently than you might be used to in Go, but we think that it makes writing tests as easy and efficient as possible.
After all, writing tests should be very simple and should not require you to study a whole framework.
That's why we made testza to integrate perfectly with your IDE.
You don't even have to lookup the documentation, as testza is self-explanatory.
Testza is very IDE friendly and was made to integrate with your IDE to increase your productivity.
β Testza package
| β Container for all testza modules
| | β The module you want to use (Press Ctrl+Space to see a list of all modules)
testza.Use.XXXXXXX
// --- Some Examples ---
// - Some assertions -
testza.Use.Assert.True(t, true) // -> Pass
testza.Use.Assert.NoError(t, err) // -> Pass
testza.Use.Assert.Equal(t, object, object) // -> Pass
// ...
// - Testing console output -
// Test the output of your CLI tool easily!
terminalOutput, _ := testza.Use.Capture.Stdout(func(w io.Writer) error {fmt.Println("Hello"); return nil})
testza.Use.Assert.Equal(t, terminalOutput, "Hello\n") // -> Pass
// - Mocking -
// Testing a function that accepts email addresses as a parameter:
// Testset of many different email addresses
emailAddresses := testza.Use.Mock.Strings.EmailAddresses()
// Run a test for every string in the test set
testza.Use.Mock.Strings.RunTests(t, emailAddresses, func(t *testing.T, index int, str string) {
user, domain, err := internal.ParseEmailAddress(str) // Use your function
testza.Use.Assert.NoError(t, err) // Assert that your function does not return an error
testza.Use.Assert.NotZero(t, user) // Assert that the user is returned
testza.Use.Assert.NotZero(t, domain) // Assert that the domain is returned
})
// - Aliases -
// You can use aliases to shorten the usage of testza for modules that you use often!
var assert = testza.Use.Assert
var stringTests = testza.Use.Mock.Strings
// ...
// And that's just a few examples of what you can do with Testza!
Module | Methods |
---|---|
Assert | |
Capture | |
Mock.Inputs.Strings |
Click to expand |
Mock.Inputs.Bools | |
Mock.Inputs.Floats64 |
Click to expand |
Mock.Inputs.Ints |
Click to expand |
func CompletesIn(t testRunner, duration time.Duration, f func(), msg ...interface{})
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.
func Contains(t testRunner, object, element interface{}, msg ...interface{})
func Equal(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
Equal asserts that two objects are equal.
func EqualValues(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
EqualValues asserts that two objects have equal values.
func False(t testRunner, value interface{}, msg ...interface{})
False asserts that an expression or object resolves to false.
func Greater(t testRunner, object1, object2 interface{}, msg ...interface{})
Greater asserts that the first object is greater than the second.
func Implements(t testRunner, interfaceObject, object interface{}, msg ...interface{})
Implements checks if an objects implements an interface.
testza.Use.Assert.Implements(t, (*YourInterface)(nil), new(YourObject))
testza.Use.Assert.Implements(t, (*fmt.Stringer)(nil), new(types.Const)) => pass
func KindOf(t testRunner, expectedKind reflect.Kind, object interface{}, msg ...interface{})
KindOf asserts that the object is a type of kind exptectedKind.
func Less(t testRunner, object1, object2 interface{}, msg ...interface{})
Less asserts that the first object is less than the second.
func Nil(t testRunner, object interface{}, msg ...interface{})
Nil asserts that an object is nil.
func NoError(t testRunner, err interface{}, msg ...interface{})
NoError asserts that an error is nil.
func NotCompletesIn(t testRunner, duration time.Duration, f func(), msg ...interface{})
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.
func NotContains(t testRunner, object, element interface{}, msg ...interface{})
func NotEqual(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
NotEqual asserts that two objects are not equal.
func NotEqualValues(t testRunner, expected interface{}, actual interface{}, msg ...interface{})
NotEqualValues asserts that two objects do not have equal values.
func NotImplements(t testRunner, interfaceObject, object interface{}, msg ...interface{})
NotImplements checks if an object does not implement an interface.
testza.Use.Assert.NotImplements(t, (*YourInterface)(nil), new(YourObject))
testza.Use.Assert.NotImplements(t, (*fmt.Stringer)(nil), new(types.Const)) => fail, because types.Const does implement fmt.Stringer.
func NotKindOf(t testRunner, kind reflect.Kind, object interface{}, msg ...interface{})
NotKindOf asserts that the object is not a type of kind kind
.
func NotNil(t testRunner, object interface{}, msg ...interface{})
NotNil assertsthat an object is not nil.
func NotNumeric(t testRunner, object interface{}, msg ...interface{})
Number 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.
func NotPanic(t testRunner, f func(), msg ...interface{})
NotPanic asserts that a function does not panic.
func NotZero(t testRunner, value interface{}, msg ...interface{})
NotZero asserts that the value is not the zero value for it's type.
testza.Use.Assert.NotZero(t, 1337)
testza.Use.Assert.NotZero(t, true)
testza.Use.Assert.NotZero(t, "Hello, World")
func Numeric(t testRunner, object interface{}, msg ...interface{})
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.
func Panic(t testRunner, f func(), msg ...interface{})
Panic asserts that a function panics.
func True(t testRunner, value interface{}, msg ...interface{})
True asserts that an expression or object resolves to true.
func Zero(t testRunner, value interface{}, msg ...interface{})
Zero asserts that the value is the zero value for it's type.
testza.Use.Assert.Zero(t, 0)
testza.Use.Assert.Zero(t, false)
testza.Use.Assert.Zero(t, "")
func Stderr(capture func(w io.Writer) error) (string, error)
Stderr 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 Stdout(capture func(w io.Writer) error) (string, error)
Stdout 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 Full() []bool
Full returns true and false in a boolean slice.
func Modify(inputSlice []bool, f func(index int, value bool) bool) (floats []bool)
Modify returns a modified version of a test set.
func RunTests(t testRunner, testSet []bool, testFunc func(t *testing.T, index int, f bool))
RunTests 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 Full() (floats []float64)
func GenerateRandomNegative(count int, min float64) (floats []float64)
GenerateRandomNegative 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 GenerateRandomPositive(count int, max float64) (floats []float64)
GenerateRandomPositive 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 GenerateRandomRange(count int, min, max float64) (floats []float64)
GenerateRandomRange 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 Modify(inputSlice []float64, f func(index int, value float64) float64) (floats []float64)
Modify returns a modified version of a test set.
func RunTests(t testRunner, testSet []float64, testFunc func(t *testing.T, index int, f float64))
RunTests 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 Full() (ints []int)
Full returns a combination of every integer testset and some random integers (positive and negative).
func GenerateRandomNegative(count, min int) (ints []int)
GenerateRandomNegative 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 GenerateRandomPositive(count, max int) (ints []int)
GenerateRandomPositive 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 GenerateRandomRange(count, min, max int) (ints []int)
GenerateRandomRange generates random integers with a range of min to max.
func Modify(inputSlice []int, f func(index int, value int) int) (ints []int)
Modify returns a modified version of a test set.
func RunTests(t testRunner, testSet []int, testFunc func(t *testing.T, index int, i int))
RunTests 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 EmailAddresses() []string
EmailAddresses returns a test set with valid email addresses.
func Empty() []string
Empty returns a test set with a single empty string.
func Full() (ret []string)
Full contains all string test sets plus ten generated random strings.
func GenerateRandom(count, length int) (result []string)
GenerateRandom returns random StringsHelper in a test set.
func HtmlTags() []string
HtmlTags returns a test set with html tags.
func Limit(testSet []string, max int) []string
Limit limits a test set in size.
func Long() (testSet []string)
Long 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 Modify(inputSlice []string, f func(index int, value string) string) (ret []string)
Modify returns a modified version of a test set.
func Numeric() []string
Numeric returns a test set with strings that are numeric. The highest number in here is "9223372036854775807", which is equal to the maxmim int64.
func RunTests(t testRunner, testSet []string, testFunc func(t *testing.T, index int, str string))
RunTests 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.
Made with β€οΈ by @MarvinJWendt and contributors! | MarvinJWendt.com