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 ¶
- Variables
- func ConsumedCall[T any](creator func(*gomock.Controller) *T, method, caller, ecaller string, ...) func(Test, *mock.Mocks) mock.SetupFunc
- func EqCall(x any) gomock.Matcher
- func EqError(x any) gomock.Matcher
- func Errorf(format string, args ...any) mock.SetupFunc
- func FailNow() mock.SetupFunc
- func Fatalf(format string, args ...any) mock.SetupFunc
- func InRun(expect Expect, test func(Test)) func(Test)
- func MissingCalls(setups ...mock.SetupFunc) func(Test, *mock.Mocks) mock.SetupFunc
- func NewErrUnknownParameterType(value any) error
- func Panic(arg any) mock.SetupFunc
- func Run(expect Expect, test func(Test)) func(*testing.T)
- func RunSeq(expect Expect, test func(Test)) func(*testing.T)
- func TestMain(main func()) func(t Test, param MainParams)
- func UnexpectedCall[T any](creator func(*gomock.Controller) *T, method, caller string, args ...any) func(Test, *mock.Mocks) mock.SetupFunc
- type Builder
- type Cleanuper
- type Expect
- type MainParams
- type Name
- type Recorder
- type Reporter
- type Runner
- type Test
- type Tester
- func (t *Tester) Cleanup(cleanup func())
- func (t *Tester) Errorf(format string, args ...any)
- func (t *Tester) FailNow()
- func (t *Tester) Fatalf(format string, args ...any)
- func (t *Tester) Helper()
- func (t *Tester) Name() string
- func (t *Tester) Panic(arg any)
- func (t *Tester) Parallel()
- func (t *Tester) Reporter(reporter Reporter)
- func (t *Tester) Run(test func(Test), parallel bool) Test
- func (t *Tester) Setenv(key, value string)
- func (t *Tester) TempDir() string
- func (t *Tester) WaitGroup(wg sync.WaitGroup)
- type Validator
Constants ¶
This section is empty.
Variables ¶
var ( // Error type for unknown parameter types. ErrUnkownParameterType = errors.New("unknown parameter type") )
Functions ¶
func ConsumedCall ¶
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 InRun ¶
InRun creates an isolated test environment for the given test function with given expectation. When executed via `t.Run()` it checks whether the result is matching the expectation.
func MissingCalls ¶
MissingCalls creates an expectation for all missing calls.
func NewErrUnknownParameterType ¶
NewErrUnknownParameterType creates a new unknown parameter 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 Run ¶
Run creates an isolated (by default) parallel test environment running the given test function with given expectation. When executed via `t.Run()` it checks whether the result is matching the expectation.
func RunSeq ¶
RunSeq creates an isolated, test environment for the given test function with given expectation. When executed via `t.Run()` it checks whether the result is matching the expectation.
func TestMain ¶ added in v0.0.8
func TestMain(main func()) func(t Test, param MainParams)
TestMain creates a test function that runs the given `main`-method in a separate test process to allow capturing the exit code and checking it against the expectation. This can be applied as follows:
testMainParams := map[string]test.MainParams{ "no mocks": { Args: []string{"mock"}, Env: []string{}, ExitCode: 0, }, } func TestMain(t *testing.T) { test.Map(t, testMainParams).Run(test.TestMain(main)) }
The test method is spawning a new test using the `TEST` environment variable to select the expected parameter set containing the command line arguments (`Args`) to execute in the spawned process. The test instance is also called setting the given additional environment variables (`Env`) to allow modification of the test environment.
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 Builder ¶ added in v0.0.20
type Builder[T any] interface { // Set sets the value of the field with the given name. If the name is empty, // and of the same type the stored target instance is replaced by the given // value. Set(name string, value any) Builder[T] // Get returns the value of the field with the given name. If the name is // empty, the stored target instance is returned. Get(name string) any // Build returns the created/modified target instance of the builder. Build() T }
Builder is a generic interface that allows you to access and modify unexported fields of a (pointer) struct by field name.
func NewAccessor ¶ added in v0.0.15
NewAccessor creates a generic builder/accessor for a given target struct. The builder allows you to access and modify unexported fields of the struct by field name.
If the target is a pointer to a struct (template), the pointer is stored and the instance is modified directly. If the target is a struct, it is ignored and a new pointer struct is created for modification, since a struct cannot be modified directly by reflection.
func NewBuilder ¶ added in v0.0.20
NewBuilder creates a generic builder for a target struct type. The builder allows you to access and modify unexported fields of the struct by field name.
type Cleanuper ¶
type Cleanuper interface {
Cleanup(cleanup func())
}
Cleanuper defines an interface to add a custom mehtod that is called after the test execution to cleanup the test environment.
type MainParams ¶ added in v0.0.8
MainParams provides the test parameters for testing a `main`-method.
type Recorder ¶
type Recorder struct {
// contains filtered or unexported fields
}
Recorder a test failure validator recorder.
type Reporter ¶
type Reporter interface { // Panic reports a panic. Panic(arg any) // Errorf reports a failure messages when a test is supposed to continue. Errorf(format string, args ...any) // Fatalf reports a fatal failure message that immediate aborts of the test // execution. Fatalf(format string, args ...any) // 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 Runner ¶
type Runner[P any] interface { // Cleanup register a function to be called for cleanup after all tests // have been finished. Cleanup(call func()) // Run runs the test parameter sets (by default) parallel. Run(call func(t Test, param P)) Runner[P] // RunSeq runs the test parameter sets in a sequence. RunSeq(call func(t Test, param P)) Runner[P] }
Runner is a generic test runner interface.
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 Test ¶
type Test interface { // 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 // Errorf handles a failure messages when a test is supposed to continue. Errorf(format string, args ...any) // Fatalf handles a fatal failure message that immediate aborts of the test // execution. Fatalf(format string, args ...any) // FailNow handles fatal failure notifications without log output that // aborts test execution immediately. FailNow() // Setenv sets an environment variable for the test. Setenv(key, value string) }
Test is a minimal interface for abstracting test methods that are needed to setup an isolated test environment for GoMock and Testify.
type Tester ¶
type Tester struct { sync.Synchronizer // contains filtered or unexported fields }
Tester 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 (*Tester) Cleanup ¶
func (t *Tester) 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 (*Tester) Errorf ¶
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 (*Tester) FailNow ¶
func (t *Tester) 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 (*Tester) Fatalf ¶
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 (*Tester) Helper ¶
func (t *Tester) Helper()
Helper delegates request to the parent test context.
func (*Tester) Panic ¶
Panic handles failure notifications of panics that also abort the test execution immediately.
func (*Tester) Parallel ¶
func (t *Tester) 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 (*Tester) Reporter ¶
Reporter sets up a test failure reporter. This can be used to validate the reported failures in a test environment.
func (*Tester) Run ¶
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 (*Tester) Setenv ¶ added in v0.0.15
Setenv delegates request to the parent context, if it is of type `*testing.T`. Else it is swallowing the request silently.
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) FailNow ¶
func (v *Validator) FailNow()
FailNow receive expected method call to `FailNow`.