testing

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2021 License: MIT Imports: 6 Imported by: 0

README

testing-go

Features

  1. Supports testing functions that return 1 or 2 values
  2. Convenience & Simplicity

Until I write better documentation, see ret1_test.go and ret2_test.go for examples of usage.

Installation

go get -u github.com/rocketlaunchr/testing-go

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func Errorf

func Errorf(format string, a ...interface{}) error

Errorf convenience function.

See: https://pkg.go.dev/fmt#Errorf

func Sprintf

func Sprintf(format string, a ...interface{}) string

Sprintf convenience function.

See: https://pkg.go.dev/fmt#Sprintf

func Str

func Str(i int) string

Str converts an int to a string.

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.

See: https://pkg.go.dev/errors#Is

func (Is) Error

func (Is) Error() string

func (Is) Is

func (e Is) Is(target error) bool

func (Is) Unwrap

func (e Is) Unwrap() error

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

func (Not) Error

func (Not) Error() string

func (Not) Is

func (e Not) Is(target error) bool

func (Not) Unwrap

func (e Not) Unwrap() 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL