testutils

package
v0.0.0-...-b7aacc1 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package testutils provides general purpose utility functions for unit/integration testing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FailOnTimeout

func FailOnTimeout(t *testing.T, done <-chan struct{}, timeout time.Duration)

FailOnTimeout blocks until `done` is closed or until specified timeout has elapsed. In the latter case it calls require.FailNow(t, "test timed out").

func SetEnv

func SetEnv(t *testing.T, key string, val string)

SetEnv safely sets an OS env var to the specified value and resets it to the original value upon test closure

func SetFileContents

func SetFileContents(t *testing.T, filepath string, contents string)

SetFileContents safely sets the contents of the specified filepath to the specified value, then resets it to the original value upon test closure

func SetFileContentsAndPermissions

func SetFileContentsAndPermissions(t *testing.T, path string, contents string, perm fs.FileMode)

SetFileContentsAndPermissions safely sets the contents of the specified filepath to the specified value and FileMode, then resets it to the original value upon test closure

Types

type ErrorCache

type ErrorCache struct {
	Error error
}

ErrorCache is a struct that can be used to get at the error that is emitted by test assertions when passing it instead ot *testing.T

func (*ErrorCache) Errorf

func (ec *ErrorCache) Errorf(format string, args ...interface{})

Errorf records the given formatted string as an erro

type Func

type Func func(t *testing.T)

Func wraps a regular testing function so it can be used as a pointer function receiver

func (Func) Repeat

func (f Func) Repeat(n int) Func

Repeat executes the testing function n times

func (Func) Run

func (f Func) Run(t *testing.T)

Run is equivalent to calling f(t), it just provides an interface that reads better

type GivenStatement

type GivenStatement struct {
	// contains filtered or unexported fields
}

GivenStatement is used to set up unit test preconditions

func Given

func Given(description string, setup func()) GivenStatement

Given starts the test with the first precondition

func (GivenStatement) Branch

func (g GivenStatement) Branch(runners ...Runner) Runner

Branch allows test branching by adding multiple sub-statements after a Given

func (GivenStatement) Given

func (g GivenStatement) Given(description string, setup func()) GivenStatement

Given adds an additional precondition

func (GivenStatement) Given2

Given2 allows the usage of a previously defined Given statement

func (GivenStatement) When

func (g GivenStatement) When(description string, setup func()) WhenStatement

When adds a trigger to the test path

func (GivenStatement) When2

When2 allows the usage of a previously defined When statement

type Runner

type Runner interface {
	Run(t *testing.T, repeats ...int) bool
}

Runner combines ThenStatement and ThenStatements, this should not be implemented outside of this package!

type TestCases

type TestCases[T any] []T

TestCases define alternative test setups that should all be tested

func AsTestCases

func AsTestCases[T any](cases ...T) TestCases[T]

AsTestCases is defined for convenience of casting a slice to TestCases

func (TestCases[T]) ForEach

func (tc TestCases[T]) ForEach(f func(t *testing.T, testCase T)) Func

ForEach defines the test that should be run on each test case

func (TestCases[T]) Map

func (tc TestCases[T]) Map(f func(testCase T) Runner) Runner

type ThenStatement

type ThenStatement struct {
	// contains filtered or unexported fields
}

ThenStatement is used to define test assertions

func Then

func Then(description string, setup func(t *testing.T)) ThenStatement

Then is an independent outcome check that can be used to start a statement in a Branch

func (ThenStatement) Run

func (then ThenStatement) Run(t *testing.T, repeats ...int) bool

Run executes all defined test paths. Optionally, each path is repeated a given number of times

func (ThenStatement) Then

func (then ThenStatement) Then(description string, execution func(t *testing.T)) ThenStatement

Then adds an outcome check to the test path

func (ThenStatement) Then2

func (then ThenStatement) Then2(then2 ThenStatement) ThenStatement

Then2 allows the use of a previously defined Then statement

type ThenStatements

type ThenStatements []ThenStatement

ThenStatements is used as an alias for multiple ThenStatement

func (ThenStatements) Run

func (thens ThenStatements) Run(t *testing.T, repeats ...int) bool

Run executes all defined test paths. Optionally, each path is repeated a given number of times

type WhenStatement

type WhenStatement struct {
	// contains filtered or unexported fields
}

WhenStatement is used to define conditions under test

func When

func When(description string, setup func()) WhenStatement

When is an independent trigger that can be used to start a statement in a Branch

func (WhenStatement) Branch

func (w WhenStatement) Branch(runners ...Runner) Runner

Branch allows test branching by adding multiple sub-statements after a When

func (WhenStatement) Then

func (w WhenStatement) Then(description string, execution func(t *testing.T)) ThenStatement

Then adds an outcome check to the test path

func (WhenStatement) Then2

Then2 allows the use of a previously defined Then statement

func (WhenStatement) When

func (w WhenStatement) When(description string, setup func()) WhenStatement

When adds a trigger to the test path

func (WhenStatement) When2

When2 allows the usage of a previously defined When statement

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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