testoplugin

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package testoplugin provides plugin primitives for using plugins in testo.

Implementing a plugin

Plugins can implement Plugin interface to be registered as such.

Method "Plugin" will be called for each plugin before running a suite. Parent is nil for top-level tests. For sub-tests it referes to the plugin instance of the parent test.

It is encouraged to ensure a plugin implements Plugin interface with the following line:

var _ testoplugin.Plugin = (*PluginFoo)(nil)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FuncChdir added in v1.1.0

type FuncChdir func(dir string)

FuncChdir describes testing.T.Chdir method.

type FuncCleanup added in v1.1.0

type FuncCleanup func(f func())

FuncCleanup describes testing.T.Cleanup method.

type FuncContext

type FuncContext func() context.Context

FuncContext describes testing.T.Context method.

type FuncDeadline

type FuncDeadline func() (deadline time.Time, ok bool)

FuncDeadline describes testing.T.Deadline method.

type FuncError

type FuncError func(args ...any)

FuncError describes testing.T.Error method.

type FuncFail

type FuncFail func()

FuncFail describes testing.T.Fail method.

type FuncFailNow

type FuncFailNow func()

FuncFailNow describes testing.T.FailNow method.

type FuncFailed

type FuncFailed func() bool

FuncFailed describes testing.T.Failed method.

type FuncFatal

type FuncFatal func(args ...any)

FuncFatal describes testing.T.Fatal method.

type FuncLog

type FuncLog func(args ...any)

FuncLog describes testing.T.Log method.

type FuncParallel

type FuncParallel func()

FuncParallel describes testing.T.Parallel method.

type FuncSetenv

type FuncSetenv func(key, value string)

FuncSetenv describes testing.T.Setenv method.

type FuncSkip

type FuncSkip func(args ...any)

FuncSkip describes testing.T.Skip method.

type FuncSkipNow

type FuncSkipNow func()

FuncSkipNow describes testing.T.SkipNow method.

type FuncSkipped

type FuncSkipped func() bool

FuncSkipped describes testing.T.Skipped method.

type FuncTempDir

type FuncTempDir func() string

FuncTempDir describes testing.T.TempDir method.

type Hook

type Hook struct {
	// Priority defines execution order.
	// Lower values indicate that this hook should be run earlier than others and vice versa.
	// Zero value won't affect the order - it uses stable sort internally.
	//
	// See [TryFirst] and [TryLast] for predefined priority constants.
	Priority Priority

	// Func to be run for this hook.
	Func func()
}

Hook is the plugin hook.

type Hooks

type Hooks struct {
	// BeforeAll is called before all tests once.
	BeforeAll Hook

	// BeforeEach is called before each test.
	BeforeEach Hook

	// BeforeEachSub is called before each subtest.
	BeforeEachSub Hook

	// AfterEachSub is called after each subtest.
	//
	// WARN: this hook is defered to run at the end of the test.
	// If that test has sub-tests marked as parallel,
	// this hook will run BEFORE those sub-tests are finished.
	//
	// Unless you need to run sub-tests during this hook,
	// it is recommended to use t.Cleanup during BeforeEachSub.
	AfterEachSub Hook

	// AfterEach is called after each test.
	//
	// NOTE: this hook is defered to run at the end of the test.
	// If that test has sub-tests marked as parallel,
	// this hook will run BEFORE those sub-tests are finished.
	//
	// Unless you need to run sub-tests during this hook,
	// it is recommended to use t.Cleanup during BeforeEach.
	AfterEach Hook

	// AfterAll is called after all tests once.
	AfterAll Hook
}

Hooks defines all hooks a plugin can define.

type Option

type Option struct {
	// Value of this option.
	Value any

	// Propagate states whether this option
	// should be passed automatically to all subtests.
	Propagate bool
}

Option is used to configure plugin upon creation.

All user-supplied options are passed to the Plugin method for each plugin. It is a plugin responsibility to check if the given option corresponds to it. One way to check it is with type assertion:

var opt Option
o, ok := opt.Value.(MyPluginOption)

func (Option) Propagated

func (o Option) Propagated() Option

Propagated returns a shallow clone of this option with "Propagate" field set to true.

type Override

type Override[F any] func(f F) F

Override for the function.

Nil value is valid and represents absence of override.

Use Override.Call to safely call an override.

func (Override[F]) Call

func (o Override[F]) Call(f F) F

Call returns an overridden f. If override is nil f is returned as is.

type Overrides

type Overrides struct {
	// Priority defines global priority for these overrides.
	// Overrides with lower priority values are called first.
	Priority Priority

	Log      Override[FuncLog]
	Parallel Override[FuncParallel]
	TempDir  Override[FuncTempDir]
	Deadline Override[FuncDeadline]
	Context  Override[FuncContext]
	Cleanup  Override[FuncCleanup]

	// Setenv calls Cleanup to restore an environment variable.
	// On error, it calls Fatal.
	Setenv Override[FuncSetenv]

	// Chdir calls Cleanup to restore a current directory.
	// On error, it calls Fatal.
	Chdir Override[FuncChdir]

	// Error calls Log followed by Fail.
	Error Override[FuncError]

	// Skip calls Log followed by SkipNow.
	Skip    Override[FuncSkip]
	SkipNow Override[FuncSkipNow]
	Skipped Override[FuncSkipped]
	Fail    Override[FuncFail]
	FailNow Override[FuncFailNow]
	Failed  Override[FuncFailed]

	// Fatal calls Log followed by FailNow.
	Fatal Override[FuncFatal]
}

Overrides defines all builtin methods of T a plugin can override.

Overrides work using middleware pattern - multiple overrides are stacked on top of each other, passing by a "next" function.

There exists a certain hierarchy what method calls what underneath. For example, overriding Log will affect Error, Skip, Fatal and their printf equivalents.

type Plan

type Plan struct {
	// Priority sets plan priority.
	// Plans with lower priority value are executed first.
	Priority Priority

	// Prepare may filter or re-order planned tests in-place.
	// Nil values are ignored.
	//
	// This function will be called once before running any top-level tests.
	// It will not receive subtests.
	Prepare func(suite testoreflect.SuiteInfo, tests *[]PlannedTest)
}

Plan for running the tests.

type PlannedTest

type PlannedTest interface {
	pragma.DoNotImplement

	// Info about this test.
	Info() testoreflect.TestInfo

	// Annotations of this test.
	Annotations() []Option
}

PlannedTest is a test to be scheduled for execution.

type Plugin

type Plugin interface {
	Plugin(parent Plugin, options ...Option) Spec
}

Plugin is an interface that plugins implement to provide Plan, Hooks and Overrides to the tests.

type Priority

type Priority int

Priority defines execution order (priority). It defines when a plugin component should be invoked when other parts are available.

"Plugin component" means plan, hook, override, etc.

See TryFirst and TryLast for predefined priority values.

const (
	// TryFirst indicates that this plugin part should be run as early as possible.
	TryFirst Priority = math.MinInt

	// TryLast indicates that this plugin part should be run as late as possible.
	TryLast Priority = math.MaxInt
)

type Spec

type Spec struct {
	Plan      Plan
	Hooks     Hooks
	Overrides Overrides
}

Spec is a plugin specification.

Jump to

Keyboard shortcuts

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