testing

package module
v1.0.0 Latest Latest
Warning

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

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

README

testing-go

A testing framework for Go unit testing.

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

Error ...

func (ErrContains) Is

func (e ErrContains) Is(target error) bool

Is ...

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

Error ...

func (Is) Is

func (e Is) Is(target error) bool

Is ...

func (Is) Unwrap

func (e Is) Unwrap() error

Unwrap ...

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

Error ...

func (Not) Is

func (e Not) Is(target error) bool

Is ...

func (Not) Unwrap

func (e Not) Unwrap() error

Unwrap ...

type TestConfig

type TestConfig struct {
	C     Comparator
	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.

See ret1_test.go file for usage example.

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.

See ret2_test.go file for usage example.

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.

See ret1_test.go file.

Jump to

Keyboard shortcuts

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