8000 GitHub - pschwartz/testza: Full-featured test framework for Go! Assertions, mocking, input testing, output capturing, and much more! πŸ•
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Full-featured test framework for Go! Assertions, mocking, input testing, output capturing, and much more! πŸ•

License

Notifications You must be signed in to change notification settings

pschwartz/testza

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

testza πŸ•

Testza is like pizza for Go - you could life without it, but why should you?

Latest Release Tests Coverage Unit test count Issues License: MIT


Get The Module | Documentation | Contributing | Code of Conduct


Screenshot of an example test message

Installation

# Execute this command inside your project
go get github.com/MarvinJWendt/testza

Description

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.

Getting Started

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!

Documentation

Module Methods
Assert
Click to expand
Capture
Click to expand
Mock.Inputs.Strings
Click to expand
Mock.Inputs.Bools
Click to expand
Mock.Inputs.Floats64
Click to expand
Mock.Inputs.Ints
Click to expand

Assert

testza.Use.Assert.CompletesIn

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.

testza.Use.Assert.Contains

func Contains(t testRunner, object, element interface{}, msg ...interface{})

testza.Use.Assert.Equal

func Equal(t testRunner, expected interface{}, actual interface{}, msg ...interface{})

Equal asserts that two objects are equal.

testza.Use.Assert.EqualValues

func EqualValues(t testRunner, expected interface{}, actual interface{}, msg ...interface{})

EqualValues asserts that two objects have equal values.

testza.Use.Assert.False

func False(t testRunner, value interface{}, msg ...interface{})

False asserts that an expression or object resolves to false.

testza.Use.Assert.Greater

func Greater(t testRunner, object1, object2 interface{}, msg ...interface{})

Greater asserts that the first object is greater than the second.

testza.Use.Assert.Implements

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

testza.Use.Assert.KindOf

func KindOf(t testRunner, expectedKind reflect.Kind, object interface{}, msg ...interface{})

KindOf asserts that the object is a type of kind exptectedKind.

testza.Use.Assert.Less

func Less(t testRunner, object1, object2 interface{}, msg ...interface{})

Less asserts that the first object is less than the second.

testza.Use.Assert.Nil

func Nil(t testRunner, object interface{}, msg ...interface{})

Nil asserts that an object is nil.

testza.Use.Assert.NoError

func NoError(t testRunner, err interface{}, msg ...interface{})

NoError asserts that an error is nil.

testza.Use.Assert.NotCompletesIn

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.

testza.Use.Assert.NotContains

func NotContains(t testRunner, object, element interface{}, msg ...interface{})

testza.Use.Assert.NotEqual

func NotEqual(t testRunner, expected interface{}, actual interface{}, msg ...interface{})

NotEqual asserts that two objects are not equal.

testza.Use.Assert.NotEqualValues

func NotEqualValues(t testRunner, expected interface{}, actual interface{}, msg ...interface{})

NotEqualValues asserts that two objects do not have equal values.

testza.Use.Assert.NotImplements

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.

testza.Use.Assert.NotKindOf

func NotKindOf(t testRunner, kind reflect.Kind, object interface{}, msg ...interface{})

NotKindOf asserts that the object is not a type of kind kind.

testza.Use.Assert.NotNil

func NotNil(t testRunner, object interface{}, msg ...interface{})

NotNil assertsthat an object is not nil.

testza.Use.Assert.NotNumeric

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.

testza.Use.Assert.NotPanic

func NotPanic(t testRunner, f func(), msg ...interface{})

NotPanic asserts that a function does not panic.

testza.Use.Assert.NotZero

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")

testza.Use.Assert.Numeric

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.

testza.Use.Assert.Panic

func Panic(t testRunner, f func(), msg ...interface{})

Panic asserts that a function panics.

testza.Use.Assert.True

func True(t testRunner, value interface{}, msg ...interface{})

True asserts that an expression or object resolves to true.

testza.Use.Assert.Zero

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, "")

Capture

testza.Use.Capture.Stderr

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.

testza.Use.Capture.Stdout

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.

Mock.Inputs.Bools

testza.Use.Mock.Inputs.Bools.Full

func Full() []bool

Full returns true and false in a boolean slice.

testza.Use.Mock.Inputs.Bools.Modify

func Modify(inputSlice []bool, f func(index int, value bool) bool) (floats []bool)

Modify returns a modified version of a test set.

testza.Use.Mock.Inputs.Bools.RunTests

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.

Mock.Inputs.Floats64

testza.Use.Mock.Inputs.Floats64.Full

func Full() (floats []float64)

testza.Use.Mock.Inputs.Floats64.GenerateRandomNegative

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.

testza.Use.Mock.Inputs.Floats64.GenerateRandomPositive

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.

testza.Use.Mock.Inputs.Floats64.GenerateRandomRange

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.

testza.Use.Mock.Inputs.Floats64.Modify

func Modify(inputSlice []float64, f func(index int, value float64) float64) (floats []float64)

Modify returns a modified version of a test set.

testza.Use.Mock.Inputs.Floats64.RunTests

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.

Mock.Inputs.Ints

testza.Use.Mock.Inputs.Ints.Full

func Full() (ints []int)

Full returns a combination of every integer testset and some random integers (positive and negative).

testza.Use.Mock.Inputs.Ints.GenerateRandomNegative

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.

testza.Use.Mock.Inputs.Ints.GenerateRandomPositive

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.

testza.Use.Mock.Inputs.Ints.GenerateRandomRange

func GenerateRandomRange(count, min, max int) (ints []int)

GenerateRandomRange generates random integers with a range of min to max.

testza.Use.Mock.Inputs.Ints.Modify

func Modify(inputSlice []int, f func(index int, value int) int) (ints []int)

Modify returns a modified version of a test set.

testza.Use.Mock.Inputs.Ints.RunTests

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.

Mock.Inputs.Strings

testza.Use.Mock.Inputs.Inputs.Strings.EmailAddresses

func EmailAddresses() []string

EmailAddresses returns a test set with valid email addresses.

testza.Use.Mock.Inputs.Inputs.Strings.Empty

func Empty() []string

Empty returns a test set with a single empty string.

testza.Use.Mock.Inputs.Inputs.Strings.Full

func Full() (ret []string)

Full contains all string test sets plus ten generated random strings.

testza.Use.Mock.Inputs.Inputs.Strings.GenerateRandom

func GenerateRandom(count, length int) (result []string)

GenerateRandom returns random StringsHelper in a test set.

testza.Use.Mock.Inputs.Inputs.Strings.HtmlTags

func HtmlTags() []string

HtmlTags returns a test set with html tags.

testza.Use.Mock.Inputs.Inputs.Strings.Limit

func Limit(testSet []string, max int) []string

Limit limits a test set in size.

testza.Use.Mock.Inputs.Inputs.Strings.Long

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)

testza.Use.Mock.Inputs.Inputs.Strings.Modify

func Modify(inputSlice []string, f func(index int, value string) string) (ret []string)

Modify returns a modified version of a test set.

testza.Use.Mock.Inputs.Inputs.Strings.Numeric

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.

testza.Use.Mock.Inputs.Inputs.Strings.RunTests

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

About

Full-featured test framework for Go! Assertions, mocking, input testing, output capturing, and much more! πŸ•

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%
0