Documentation
¶
Overview ¶
Package test contains the main collection of functions and types for setting up the basic isolated test environment. It is part of the public interface and starting to get stable, however, we are still experimenting to optimize the interface and the user experience.
Index ¶
- Constants
- Variables
- func Cast[T any](arg any) T
- func ConsumedCall[T any](creator func(*gomock.Controller) *T, method, caller, ecaller string, ...) func(Test, *mock.Mocks) mock.SetupFunc
- func DeepCopy(t Test, p DeepCopyParams)
- func DeepCopyTestCases(seed int64, size, length int, args ...any) map[string]DeepCopyParams
- func EqCall(x any) gomock.Matcher
- func EqError(x any) gomock.Matcher
- func Error(args ...any) mock.SetupFunc
- func Errorf(format string, args ...any) mock.SetupFunc
- func Fail() mock.SetupFunc
- func FailNow() mock.SetupFunc
- func Fatal(args ...any) mock.SetupFunc
- func Fatalf(format string, args ...any) mock.SetupFunc
- func First[T any](arg T, _ ...any) T
- func Log(args ...any) mock.SetupFunc
- func Logf(format string, args ...any) mock.SetupFunc
- func Main(main func()) func(t Test, param MainParams)
- func MissingCalls(setups ...mock.SetupFunc) func(Test, *mock.Mocks) mock.SetupFunc
- func Must[T any](arg T, err error) T
- func NewErrInvalidType(value any) error
- func Panic(arg any) mock.SetupFunc
- func Ptr[T any](v T) *T
- func Recover(t Test, expect any)
- func Run(expect Expect, test Func) func(*testing.T)
- func RunSeq(expect Expect, test Func) func(*testing.T)
- func UnexpectedCall[T any](creator func(*gomock.Controller) *T, method, caller string, args ...any) func(Test, *mock.Mocks) mock.SetupFunc
- type CleanupFunc
- type Cleanuper
- type Context
- func (t *Context) Cleanup(cleanup func())
- func (t *Context) Deadline() (time.Time, bool)
- func (t *Context) Done()
- func (t *Context) Error(args ...any)
- func (t *Context) Errorf(format string, args ...any)
- func (t *Context) Expect(expect Expect) *Context
- func (t *Context) Fail()
- func (t *Context) FailNow()
- func (t *Context) Failed() bool
- func (t *Context) Fatal(args ...any)
- func (t *Context) Fatalf(format string, args ...any)
- func (t *Context) Helper()
- func (t *Context) Log(args ...any)
- func (t *Context) Logf(format string, args ...any)
- func (t *Context) Name() string
- func (t *Context) Panic(arg any)
- func (t *Context) Parallel()
- func (t *Context) Reporter(reporter Reporter)
- func (t *Context) Run(test Func) Test
- func (t *Context) Setenv(key, value string)
- func (t *Context) Skip(args ...any)
- func (t *Context) SkipNow()
- func (t *Context) Skipf(format string, args ...any)
- func (t *Context) Skipped() bool
- func (t *Context) StopEarly(time time.Duration) *Context
- func (t *Context) TempDir() string
- func (t *Context) Timeout(timeout time.Duration) *Context
- func (t *Context) WaitGroup(wg sync.WaitGroup)
- type DeepCopyParams
- type Expect
- type Factory
- type FilterFunc
- type Func
- type MainParams
- type Panicer
- type ParamFunc
- type Recorder
- func (r *Recorder) Error(args ...any) *gomock.Call
- func (r *Recorder) Errorf(format string, args ...any) *gomock.Call
- func (r *Recorder) Fail() *gomock.Call
- func (r *Recorder) FailNow() *gomock.Call
- func (r *Recorder) Fatal(args ...any) *gomock.Call
- func (r *Recorder) Fatalf(format string, args ...any) *gomock.Call
- func (r *Recorder) Log(args ...any) *gomock.Call
- func (r *Recorder) Logf(format string, args ...any) *gomock.Call
- func (r *Recorder) Panic(arg any) *gomock.Call
- type Reporter
- type SetupFunc
- type Test
- type Validator
- func (v *Validator) EXPECT() *Recorder
- func (v *Validator) Error(args ...any)
- func (v *Validator) Errorf(format string, args ...any)
- func (v *Validator) Fail()
- func (v *Validator) FailNow()
- func (v *Validator) Fatal(args ...any)
- func (v *Validator) Fatalf(format string, args ...any)
- func (v *Validator) Log(args ...any)
- func (v *Validator) Logf(format string, args ...any)
- func (v *Validator) Panic(arg any)
Constants ¶
const GoTestingRunVar = "GO_TESTING_RUN"
GoTestingRunVar is the environment variable used to signal the new process to execute the `main` method instead of spawning a new test process.
Variables ¶
var ErrInvalidType = errors.New("invalid type")
ErrInvalidType is an error for invalid types.
Functions ¶
func Cast ¶ added in v0.0.35
Cast is a convenience function to cast the given argument to the specified type or panic if the cast fails. The method allows to write concise test setup code granting meaningful type checks.
func ConsumedCall ¶
func DeepCopy ¶ added in v0.0.36
func DeepCopy(t Test, p DeepCopyParams)
DeepCopy provides a test function that tests a the `DeepCopy*` functions generated by `k8s.io/code-generator/cmd/deepcopy-gen` as part of the type system.
The test function verifies that the copied value is equal to the original value but not the same reference. For simplicity, the function only requires a template to generate random non-zero test values that ensure covering all code paths. If this fails, you can vary the random seed, as well as the limits on the slice and map sizes (`size`) and string lengths (`len`).
The following code shows a quick example of how to use this function in a tests:
```go
var deepCopyTestCases = DeepCopyTestCases(42, 3, 10,
&MyStruct{}, (*MyStruct)(nil), ...)
func TestDeepCopy(t *testing.T) {
test.Map(t, deepCopyTestCases).Run(test.DeepCopy)
}
``` *Note:* the test cases can also be generated inside the `TestDeepCopy` function.
func DeepCopyTestCases ¶ added in v0.0.39
func DeepCopyTestCases( seed int64, size, length int, args ...any, ) map[string]DeepCopyParams
DeepCopyTestCases creates the test cases from a list of types to be tested either given as value or as nil pointer. For each type two test cases are created:
* A non-nil random value, and * A nil pointer of the type.
The types must implement either the `DeepCopy` or the `DeepCopyObject` method generated by `k8s.io/code-generator/cmd/deepcopy-gen` to succeed.
func EqCall ¶
EqCall creates a new call matcher that allows to match calls by translating them to the string containing the core information instead of using the standard matcher using reflect.DeepEquals that fails for the contained actions.
func EqError ¶
EqError creates a new error matcher that allows to match either the error or alternatively the string describing the error.
func First ¶ added in v0.0.30
First is a convenience function to return the first argument and ignore all others arguments. The method allows to write concise test setup code.
func Main ¶ added in v0.0.29
func Main(main func()) func(t Test, param MainParams)
Main creates a test function that runs the given `main`-method in a separate test process to protect the test execution from `os.Exit` calls while allowing to capture and check the exit code against the expectation. The following example demonstrates how to use this method to test a `main`-method:
mainTestCases := map[string]test.MainParam{
"with args": {
Args: []string{"mock", "arg1", "arg2"},
Env: []string{"VAR=value"},
ExitCode: 0,
},
}
func Main(t *testing.T) {
test.Map(t, mainTestCases).Run(test.Main(main))
}
If the test process is expected to run longer than the default test timeout, a context with timeout can be provided to interrupt the test process in time. This e.g. can be done as follows using `test.First` to ignore the cancelFunc:
Ctx: test.First(context.WithTimeout(context.Background(), time.Second))
func MissingCalls ¶
MissingCalls creates an expectation for all missing calls.
func Must ¶ added in v0.0.30
Must is a convenience method returning the value of the first argument and that panics on any error in the second argument using the provided error. The method allows to write concise test setup code.
func NewErrInvalidType ¶ added in v0.0.22
NewErrInvalidType creates a new invalid type error.
func Panic ¶
Panic creates a validation method call setup for a panic. It allows to match the panic object, which usually is an error and alternatively the error string representing the error, since runtime errors may be irreproducible.
func Ptr ¶ added in v0.0.43
func Ptr[T any](v T) *T
Ptr is a convenience function to obtain the pointer to the given value. This is particularly useful to create pointers to literal values in test setup code, e.g., `test.Ptr(42)` or `test.Ptr("value")`.
func Recover ¶ added in v0.0.44
Recover is a convenience method to be used in deferred calls to verify that a panic occurred with the expected panic response. The function creates a test failure if no panic occurred or the panic response does not match the expected value.
func Run ¶
Run creates an isolated (by default) parallel test context running the given test function with given expectation. If the expectation is not met, a test failure is created in the parent test context.
func RunSeq ¶
RunSeq creates an isolated, test context for the given test function with given expectation. If the expectation is not met, a test failure is created in the parent test context.
func UnexpectedCall ¶
func UnexpectedCall[T any]( creator func(*gomock.Controller) *T, method, caller string, args ...any, ) func(Test, *mock.Mocks) mock.SetupFunc
UnexpectedCall creates expectation for unexpected calls. We only support one unexpected call since the test execution stops in this case.
Types ¶
type CleanupFunc ¶ added in v0.0.28
type CleanupFunc func()
CleanupFunc defines the common test cleanup function signature.
type Cleanuper ¶
type Cleanuper interface {
Cleanup(cleanup func())
}
Cleanuper defines an interface to add a custom method that is called after the test execution to cleanup the test environment.
type Context ¶ added in v0.0.22
type Context struct {
sync.Synchronizer
// contains filtered or unexported fields
}
Context is a test isolation environment based on the `Test` abstraction. It can be used as a drop in replacement for `testing.T` in various libraries to check for expected test failures.
func New ¶
New creates a new minimal isolated test context based on the given test context with. The parent test context is used to delegate methods calls to the parent context to propagate test results.
func (*Context) Cleanup ¶ added in v0.0.22
func (t *Context) Cleanup(cleanup func())
Cleanup is a function called to setup test cleanup after execution. This method is allowing `gomock` to register its `finish` method that reports the missing mock calls.
func (*Context) Deadline ¶ added in v0.0.22
Deadline delegates request to the parent context. It returns the deadline of the test and a flag indicating whether the deadline is set.
func (*Context) Done ¶ added in v0.0.29
func (t *Context) Done()
Done decrements the wait group of the test by one.
func (*Context) Error ¶ added in v0.0.22
Error handles failure messages where the test is supposed to continue. On an expected success, the failure is also delegated to the parent test context. Else it delegates the request to the test reporter if available.
func (*Context) Errorf ¶ added in v0.0.22
Errorf handles failure messages where the test is supposed to continue. On an expected success, the failure is also delegated to the parent test context. Else it delegates the request to the test reporter if available.
func (*Context) Fail ¶ added in v0.0.22
func (t *Context) Fail()
Fail handles a failure message that immediate aborts of the test execution. On an expected success, the failure handling is also delegated to the parent test context. Else it delegates the request to the test reporter if available.
func (*Context) FailNow ¶ added in v0.0.22
func (t *Context) FailNow()
FailNow handles fatal failure notifications without log output that aborts test execution immediately. On an expected success, it the failure handling is also delegated to the parent test context. Else it delegates the request to the test reporter if available.
func (*Context) Fatal ¶ added in v0.0.22
Fatal handles a fatal failure message that immediate aborts of the test execution. On an expected success, the failure handling is also delegated to the parent test context. Else it delegates the request to the test reporter if available.
func (*Context) Fatalf ¶ added in v0.0.22
Fatalf handles a fatal failure message that immediate aborts of the test execution. On an expected success, the failure handling is also delegated to the parent test context. Else it delegates the request to the test reporter if available.
func (*Context) Helper ¶ added in v0.0.22
func (t *Context) Helper()
Helper delegates request to the parent test context.
func (*Context) Log ¶ added in v0.0.22
Log delegates request to the parent context. It provides a logging function for the test.
func (*Context) Logf ¶ added in v0.0.22
Logf delegates request to the parent context. It provides a logging function for the test.
func (*Context) Panic ¶ added in v0.0.22
Panic handles failure notifications of panics that also abort the test execution immediately.
func (*Context) Parallel ¶ added in v0.0.22
func (t *Context) Parallel()
Parallel robustly delegates request to the parent context. It can be called multiple times, since it is swallowing the panic that is raised when calling `t.Parallel()` multiple times.
func (*Context) Reporter ¶ added in v0.0.22
Reporter sets up a test failure reporter. This can be used to validate the reported failures in a test environment.
func (*Context) Run ¶ added in v0.0.22
Run executes the test function in a safe detached environment and check the failure state after the test function has finished. If the test result is not according to expectation, a failure is created in the parent test context.
func (*Context) Setenv ¶ added in v0.0.22
Setenv delegates request to the parent context, if it is of type `*testing.T`. Else it is swallowing the request silently.
func (*Context) Skip ¶ added in v0.0.22
Skip delegates request to the parent context. It is a helper method to skip the test.
func (*Context) SkipNow ¶ added in v0.0.22
func (t *Context) SkipNow()
SkipNow delegates request to the parent context. It is a helper method to skip the test immediately.
func (*Context) Skipf ¶ added in v0.0.22
Skipf delegates request to the parent context. It is a helper method to skip the test with a formatted message.
func (*Context) Skipped ¶ added in v0.0.22
Skipped delegates request to the parent context. It reports whether the test has been skipped.
func (*Context) StopEarly ¶ added in v0.0.22
StopEarly stops the test by the given duration ahead of the individual or global test deadline, to ensure that a cleanup function has sufficient time to finish before a global deadline exceeds. The method is not able to extend the test deadline. A negative or zero duration is ignored.
Warning: calling this method multiple times will also reduce the deadline step by step.
func (*Context) TempDir ¶ added in v0.0.22
TempDir delegates the request to the parent test context.
func (*Context) Timeout ¶ added in v0.0.22
Timeout sets up an individual timeout for the test. This does not affect the global test timeout or a pending parent timeout that may abort the test, if the given duration is exceeding the timeout. A negative or zero duration is ignored and will not change the timeout.
type DeepCopyParams ¶ added in v0.0.36
type DeepCopyParams struct {
// Value is a template value used to generate random non-zero test values
// for testing the `DeepCopy*` functions.
Value any
}
DeepCopyParams provides test parameters for testing `DeepCopy*` functions generated by `k8s.io/code-generator/cmd/deepcopy-gen`, that unfortunately are part of the type system and thus should be unit tested for coverage.
type Expect ¶
type Expect bool
Expect the expectation whether a test will succeed or fail.
const ( // Success used to express that a test is supposed to succeed. Success Expect = true // Failure used to express that a test is supposed to fail. Failure Expect = false // Parallel is the global flag to switch test runs to be executed in // parallel instead of sequentially. Parallel = true )
Constants to express test expectations.
type Factory ¶ added in v0.0.41
type Factory[P any] interface { // Adds a generic filter function that allows to filter test cases based on // the name and the parameter set. Filter(filter FilterFunc[P]) Factory[P] // Timeout sets up a timeout for the test cases executed by the test runner. // Setting a timeout is useful to prevent the test execution from waiting // too long in case of deadlocks. The timeout is not affecting the global // test timeout that may only abort a test earlier. If the given duration is // zero or negative, the timeout is ignored. Timeout(timeout time.Duration) Factory[P] // StopEarly stops the test by the given duration ahead of an individual or // global test deadline. This is useful to ensure that resources can be // cleaned up before the global deadline is exceeded. StopEarly(time time.Duration) Factory[P] // Run runs all test parameter sets in parallel. If the test parameter sets // are provided as a map, the test case name is used as the test name. If // the test parameter sets are provided as a slice, the test case name is // created by appending the index to the test name. If the test parameter // sets are provided as a single parameter set, the test case name is used // as the test name. The test case name is normalized before being used. Run(call ParamFunc[P]) Factory[P] // RunSeq runs the test parameter sets in a sequence. If the test parameter // sets are provided as a map, the test case name is used as the test name. // If the test parameter sets are provided as a slice, the test case name is // created by appending the index to the test name. If the test parameter // sets are provided as a single parameter set, the test case name is used // as the test name. The test case name is normalized before being used. RunSeq(call ParamFunc[P]) Factory[P] // Cleanup register a function to be called to cleanup after all tests have // finished to remove the shared resources. Cleanup(call CleanupFunc) }
Factory is a generic test factory interface.
func Any ¶ added in v0.0.22
Any creates a new parallel test runner with given parameter set(s). The set can be a single test parameter set, a slice of test parameter sets, or a map of named test parameter sets. The test runner is looking into the parameter set to determine a suitable test case name, e.g. by using a `name` parameter.
func Map ¶
Map creates a new parallel test runner with given test parameter sets provided as a test case name to parameter sets mapping.
type FilterFunc ¶ added in v0.0.41
FilterFunc defines the common test filter function signature.
func And ¶ added in v0.0.41
func And[P any](filters ...FilterFunc[P]) FilterFunc[P]
And combines the given filter functions with a logical `and`.
func Arch ¶ added in v0.0.41
func Arch[P any](arch string) FilterFunc[P]
Arch creates a filter function that matches the given architecture.
func Not ¶ added in v0.0.41
func Not[P any](filter FilterFunc[P]) FilterFunc[P]
Not negates the given filter function.
func OS ¶ added in v0.0.41
func OS[P any](os string) FilterFunc[P]
OS creates a filter function that matches the given operating system.
func Or ¶ added in v0.0.41
func Or[P any](filters ...FilterFunc[P]) FilterFunc[P]
Or combines the given filter functions with a logical `or`.
func Pattern ¶ added in v0.0.41
func Pattern[P any](pattern string) FilterFunc[P]
Pattern creates a filter function that matches the test case name against the given pattern. The pattern is adjusted to replace spaces with dashes before being compiled into a regular expression to account for the test name normalization.
type MainParams ¶ added in v0.0.8
type MainParams struct {
// Ctx is the context to control the test process, e.g. for deadlines and
// cancellations. If not set, a background context is used with unlimited
// duration.
Ctx context.Context //nolint:containedctx // only in test parameters.
// Args are the command line arguments to provide to the test process. The
// first argument is usually the program name, typically `os.Args[0]`. If
// not provided, the `main`-method is called with no arguments at all.
Args []string
// Env are the additional environment variables that are provided to the
// spawned test process. The existing environment variables are inherited
// and an additional `GO_TESTING_TEST` variable is set to select the test
// case.
Env []string
// ExitCode is the expected exit code when running the test process. If
// the exit code is not `0`, the error must be of type `*exec.ExitError`.
ExitCode int
// Error is the expected error when running the test process. This is only
// used in edge cases when the test error is not of type `*exec.ExitError`.
Error error
}
MainParams provides the test parameters for testing a `main`-method.
type Panicer ¶ added in v0.0.45
type Panicer interface {
// Embeds the basic test reporter interface.
Reporter
// Panic reports a panic.
Panic(arg any)
}
Panicer is a test reporter that supports in addition panic reporting.
type ParamFunc ¶ added in v0.0.28
ParamFunc defines the common parameterized test function signature.
type Recorder ¶
type Recorder struct {
// contains filtered or unexported fields
}
Recorder a test failure validator recorder.
type Reporter ¶
type Reporter interface {
// Log provides a logging function for the test.
Log(args ...any)
// Logf provides a logging function for the test.
Logf(format string, args ...any)
// Error reports a failure messages when a test is supposed to continue.
Error(args ...any)
// Errorf reports a failure messages when a test is supposed to continue.
Errorf(format string, args ...any)
// Fatal reports a fatal failure message that immediate aborts of the test
// execution.
Fatal(args ...any)
// Fatalf reports a fatal failure message that immediate aborts of the test
// execution.
Fatalf(format string, args ...any)
// Fail reports a failure message that immediate aborts of the test
// execution.
Fail()
// FailNow reports fatal failure notifications without log output that
// aborts test execution immediately.
FailNow()
}
Reporter is a minimal interface for abstracting test report methods that are needed to setup an isolated test environment for GoMock and Testify.
type SetupFunc ¶ added in v0.0.28
type SetupFunc func(Test)
SetupFunc defines the common test setup function signature.
type Test ¶
type Test interface {
// Embeds the basic test reporter interface.
Reporter
// Name provides the test name.
Name() string
// Helper declares a test helper function.
Helper()
// Parallel declares that the test is to be run in parallel with (and only
// with) other parallel tests.
Parallel()
// TempDir creates a new temporary directory for the test.
TempDir() string
// Setenv sets an environment variable for the test.
Setenv(key, value string)
// Deadline returns the deadline of the test and a flag indicating whether
// the deadline is set.
Deadline() (deadline time.Time, ok bool)
// Skip is a helper method to skip the test.
Skip(args ...any)
// Skipf is a helper method to skip the test with a formatted message.
Skipf(format string, args ...any)
// SkipNow is a helper method to skip the test immediately.
SkipNow()
// Skipped reports whether the test has been skipped.
Skipped() bool
// Failed reports whether the test has failed.
Failed() bool
// Cleanup is a function called to setup test cleanup after execution.
Cleanup(cleanup func())
}
Test is a minimal interface for abstracting test methods that are needed to setup an isolated test environment for GoMock and Testify.
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator a test failure validator based on the test reporter interface.
func NewValidator ¶
func NewValidator(ctrl *gomock.Controller) *Validator
NewValidator creates a new test validator for validating error messages and panics created during test execution.
func (*Validator) EXPECT ¶
EXPECT implements the usual `gomock.EXPECT` call to request the recorder.
func (*Validator) Fail ¶ added in v0.0.22
func (v *Validator) Fail()
Fail receive expected method call to `Fail`.
func (*Validator) FailNow ¶
func (v *Validator) FailNow()
FailNow receive expected method call to `FailNow`.