Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // PanicExpected indicates that the function being tested is expected to panic. PanicExpected = errors.New("panic expected") // ErrAny indicates that the function being tested is expected to return an (unspecified) error. ErrAny = errors.New("any error expected") // CustomTest indicates that you want to perform your own test inside the Run1, RunErr or Run2 function. // You will be expected to signify when a test failed yourself i.e. using t.Errorf(...). CustomTest = errors.New("custom test") )
Functions ¶
Types ¶
type Comparator ¶
type Comparator func(x, y interface{}) bool
Comparator returns true if x and y are equal (as determined by the Comparator function). x and y are the returned and expected value respectively from a function being tested. The default Comparator uses reflect.DeepEqual. However, the github.com/google/go-cmp package may also be used. See: https://github.com/google/go-cmp.
type ErrContains ¶
type ErrContains struct{ Substr string }
ErrContains is used to check if the observed error contains a particular substring. It uses strings.Contains().
Example:
testCases := []struct { in bool ExpErr error }{ {false, ErrContains{"database error"}}, }
func (ErrContains) Error ¶
func (e ErrContains) Error() string
func (ErrContains) Is ¶
func (e ErrContains) Is(target error) bool
type Is ¶
type Is struct{ Err error }
Is indicates that errors.Is() be used to test if an expected error matches the observed error. When not set, a simple equality check is performed using reflect.DeepEqual.
type Not ¶
type Not struct{ Val interface{} }
Not means the expected value/error is expected to be not equal.
Example:
testCases := []struct { in bool ExpErr error }{ {false, Not{ErrContains{"database error"}}}, }
type TestConfig ¶
type TestConfig struct { // C sets the function used to check if the observed return value (non error) matches // the expected return value. By default, reflect.DeepEqual is used. However, the github.com/google/go-cmp // package may also be used. See: https://github.com/google/go-cmp. C Comparator // Fatal marks the test as having failed and stops its execution. // // See: https://golang.org/pkg/testing/#T.FailNow Fatal bool // contains filtered or unexported fields }
TestConfig ...
See ret1_test.go file for usage example.
func NewTestConfig ¶
func NewTestConfig(t *testing.T) *TestConfig
NewTestConfig creates a new TestConfig.
func (TestConfig) Run1 ¶
func (tcfg TestConfig) Run1(name string, tc interface{}, f func(t *testing.T) interface{})
Run1 is used when testing a function that returns a single (non-error) value. tc is a struct that is expected to have a field named: ExpOut. It can be of the relevant type, but if you want to test for panics, then it must be interface{} so you can use PanicExpected (which is an error type).
See ret1_test.go file for usage example.
NOTE: Be wary of how Go interprets in-place constants. If you are expecting a float64, then don't type 1. Instead type 1.0. See: https://blog.golang.org/constants.
func (TestConfig) Run2 ¶
func (tcfg TestConfig) Run2(name string, tc interface{}, f func(t *testing.T) (interface{}, error))
Run2 is used when testing a function that returns a value and an error. tc is a struct that is expected to have 2 fields named: ExpOut and ExpErr (of type error). ExpOut can be of the relevant type, but if you want to test for panics, then it must be interface{} so you can use PanicExpected (which is an error type).
See ret2_test.go file for usage example.
NOTE: Be wary of how Go interprets in-place constants. If you are expecting a float64, then don't type 1. Instead type 1.0. See: https://blog.golang.org/constants.
func (TestConfig) RunErr ¶
func (tcfg TestConfig) RunErr(name string, tc interface{}, f func(t *testing.T) error)
RunErr is used when testing a function that returns a single error value. tc is a struct that is expected to have a field named: ExpErr of type error.
See ret1_test.go file for usage example.