stdtest

package
v1.41.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTestParallelWithSetEnv = stdlib.Error{
	Code:      "parallel_with_setenv",
	Message:   "test cannot be parallel and also modify environment variables",
	Namespace: stdlib.ErrorNamespaceDefault,
}

ErrTestParallelWithSetEnv is returned when attempting to create a parallel test that overrides process environment variables.

Functions

func WithTestContext added in v1.8.0

func WithTestContext(ctx context.Context) stdlib.Option[*TestConfig]

WithTestContext sets the config ctx.

func WithTestEnv added in v1.8.0

func WithTestEnv(env map[string]string) stdlib.Option[*TestConfig]

WithTestEnv sets the config env.

func WithTestLogf

func WithTestLogf(log Logf) stdlib.Option[*TestConfig]

WithTestLogf sets the config logf.

func WithTestMaxCount

func WithTestMaxCount(maxCount int) stdlib.Option[*TestConfig]

WithTestMaxCount sets the config property testing max count.

func WithTestMaxCountScale

func WithTestMaxCountScale(maxCountScale float64) stdlib.Option[*TestConfig]

WithTestMaxCountScale sets the config property testing max count scale.

func WithTestParallel

func WithTestParallel(parallel bool) stdlib.Option[*TestConfig]

WithTestParallel sets the config parallel.

func WithTestPrecondition

func WithTestPrecondition(preconditions ...TestPrecondition) stdlib.Option[*TestConfig]

WithTestPrecondition sets the config preconditions.

func WithTestRand

func WithTestRand(r *rand.Rand) stdlib.Option[*TestConfig]

WithTestRand sets the config rand number generator.

func WithTestSkip added in v1.41.0

func WithTestSkip(skip string) stdlib.Option[*TestConfig]

WithTestSkip sets the config skip reason.

func WithTestTimeout added in v1.8.0

func WithTestTimeout(to time.Duration) stdlib.Option[*TestConfig]

WithTestTimeout sets the config timeout.

Types

type Assert

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

Assert implements helpers for common assertion patterns.

func (*Assert) Equal

func (a *Assert) Equal(got, want any) bool

Equal fails the test if 'got' is not equal to 'want' using reflect.DeepEqual.

func (*Assert) EqualComparer added in v1.16.0

func (a *Assert) EqualComparer(got, want any, cmp Comparer) bool

EqualComparer fails the test if the comparer is not true for the given values.

func (*Assert) EqualError added in v1.4.1

func (a *Assert) EqualError(got, want error) bool

EqualError fails the test if 'got' is not equal to 'want' for errors.

func (*Assert) EqualFloat added in v1.40.0

func (a *Assert) EqualFloat(got, want, epsilon float64) bool

EqualFloat fails the test if 'got' is not equal to 'want' for float64 values.

func (*Assert) EqualPointer added in v1.4.1

func (a *Assert) EqualPointer(got, want any) bool

EqualPointer fails the test if 'got' is not equal to 'want' for pointers.

func (*Assert) EqualType added in v1.5.0

func (a *Assert) EqualType(got, want any) bool

EqualType fails the test if 'got' is not equal to 'want' for concrete types.

func (*Assert) False

func (a *Assert) False(condition bool, format string, args ...any) bool

False fails the test if the condition is true.

func (*Assert) Match

func (a *Assert) Match(got, wantPattern string) bool

Match fails the test if 'got' does not match 'want' pattern.

func (*Assert) NotEqual added in v1.9.0

func (a *Assert) NotEqual(got, want any) bool

NotEqual fails the test if 'got' is equal to 'want' using reflect.DeepEqual.

func (*Assert) NotOK

func (a *Assert) NotOK(err error) bool

NotOK fails the test if err is nil.

func (*Assert) OK

func (a *Assert) OK(err error) bool

OK fails the test if err is not nil.

func (*Assert) Panic

func (a *Assert) Panic(got func()) bool

Panic fails the test if the 'got' function does not panic.

func (*Assert) True

func (a *Assert) True(condition bool, format string, args ...any) bool

True fails the test if the condition is false.

type Asserter

type Asserter interface {
	True(condition bool, format string, args ...any) bool
	False(condition bool, format string, args ...any) bool
	OK(err error) bool
	NotOK(err error) bool
	Match(got, want string) bool
	NotEqual(got, want any) bool
	Equal(got, want any) bool
	EqualPointer(got, want any) bool
	EqualError(got, want error) bool
	EqualType(got, want any) bool
	EqualComparer(got, want any, cmp Comparer) bool
	EqualFloat(got, want, epsilon float64) bool
	Panic(got func()) bool
}

Asserter defines common test assertions.

type Check

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

Check implements property testing via the "testing/quick" package.

func (*Check) Check

func (c *Check) Check(fn any) bool

Check fails the test if the check function returns false.

func (*Check) CheckEqual

func (c *Check) CheckEqual(fn1, fn2 any) bool

CheckEqual fails the test if the check function returns false.

type Checker

type Checker interface {
	Check(fn any) bool
	CheckEqual(fn1, fn2 any) bool
}

Checker defines common quick tests.

type Comparer added in v1.16.0

type Comparer func(got, want any) bool

Comparer is a function that compares two values of any type and returns true if they are "equal".

type Logf

type Logf func(format string, args ...any)

Logf is the func called when a boolean condition is not met as part of an assertion. It's responsible for generating output to a user indicating the test failure and either allowing the test to continue or immediately stop execution.

type Table

type Table[TGot any, TWant any] map[string]Testcase[TGot, TWant]

Table represents a collection of "table tests" in the form of named test cases.

func (Table[TGot, TWant]) Run added in v1.5.1

func (table Table[TGot, TWant]) Run(
	tb testing.TB,
	testFn TestFunc[TGot, TWant],
	options ...stdlib.Option[*TestConfig],
)

Run will execute all table tests using the given test function.

type Test

type Test struct {
	// TB is the golang 'testing' implementation for (T, B, F) tests.
	testing.TB
	// Asserter handles common test assertions.
	Asserter
	// Checker handles property tests.
	Checker
	// Config stores configuration specific to an individual test.
	Config *TestConfig
}

Test merges the stdlib 'testing.TB' with our test helpers into a single implementation.

By using this 'Test' instead of the `testing` structs (T, B, F) we can automatically handle many common assertion patterns and preconditions for different test types (functional, fuzz, or integration).

func BenchmarkTest

func BenchmarkTest(t *testing.B, options ...stdlib.Option[*TestConfig]) *Test

BenchmarkTest creates a new *Benchmark configured for running only "benchmark" tests when the BENCHMARK_TEST environment variable is set.

func FuzzTest

func FuzzTest(t *testing.F, options ...stdlib.Option[*TestConfig]) *Test

FuzzTest creates a new *Benchmark configured for running only "fuzz" tests when the FUZZ_TEST environment variable is set.

func IntegrationTest

func IntegrationTest(t *testing.T, options ...stdlib.Option[*TestConfig]) *Test

IntegrationTest creates a new *Test configured for running only "integration" tests when the INTEGRATION_TEST environment variable is set.

func NewTest added in v1.25.0

func NewTest(t testing.TB, options ...stdlib.Option[*TestConfig]) *Test

NewTest creates a new *Test with the given options.

func PropertyTest

func PropertyTest(t testing.TB, options ...stdlib.Option[*TestConfig]) *Test

PropertyTest creates a new *Test configured for running only "property" tests.

func UnitTest

func UnitTest(t testing.TB, options ...stdlib.Option[*TestConfig]) *Test

UnitTest creates a new *Test configured for running only "unit" tests.

func (*Test) Fuzz added in v1.20.0

func (t *Test) Fuzz(fn any) bool

Fuzz runs the given function as a fuzztest of the current test.

func (*Test) Sub

func (t *Test) Sub(
	name string,
	fn func(subtest *Test),
	options ...stdlib.Option[*TestConfig],
) bool

Sub runs the given function as a subtest of the current test.

type TestConfig

type TestConfig struct {
	// Context is context for an individual test.
	Context context.Context
	// Env contains key/value pairs to be set with 'os.SetEnv' for the scope of the test.
	Env map[string]string
	// Logf is called when condition of test assertion is not met.
	Logf Logf
	// Parallel enables/disables Parallel test execution.
	Parallel bool
	// Preconditions are called before test execution to determine if test should
	// be skipped and the reason for it.
	Preconditions []TestPrecondition
	// QuickConfig modifies of how quick test (property tests) are executed.
	QuickConfig *quick.Config
	// Skip test for this reason.
	Skip string
	// Timeout is timeout duration for test execution.
	Timeout time.Duration
}

TestConfig defines config options for test.

func NewTestConfig

func NewTestConfig(options ...stdlib.Option[*TestConfig]) (*TestConfig, error)

NewTestConfig creates a new *TestConfig for the given functional opts and sane defaults.

type TestFunc

type TestFunc[TGot any, TWant any] func(t *Test, tc Testcase[TGot, TWant])

TestFunc is a function that executes a single test for the given testcase.

Note: The benefit of passing in an isolated function for the test is that we can avoid issues with closures when t.Run(...) is called in parallel with the testcase.

type TestPrecondition

type TestPrecondition func() (bool, string)

TestPrecondition is a func called prior to test execution to determine if the test should be skipped and the reason for it.

func TestPreconditionEnvVarSet added in v1.11.0

func TestPreconditionEnvVarSet(name string) TestPrecondition

TestPreconditionEnvVarSet returns true if the given environment variable name is present.

type Testcase

type Testcase[TGot any, TWant any] struct {
	// Got stores inputs for the testcase.
	Got TGot
	// Want stores the expectations for the testcase.
	Want TWant
	// WantErr stores the optional error expectation for the testcase.
	WantErr error
	// WantPanic store expectation for the test case to cause a panic.
	WantPanic bool
	// Options are custom options for test execution specific to this case.
	Options []stdlib.Option[*TestConfig]
}

Testcase encapsulates the inputs and expectations of a single testcase.

Jump to

Keyboard shortcuts

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