Documentation
¶
Overview ¶
Package testrig is a Go test harness for hunting flaky tests. It exposes lifecycle hooks (GlobalSetup/Teardown, IterationSetup/Teardown) that callers wire into TestMain so the same hooks work whether the test suite is invoked directly via go test or under the testrig diagnose CLI.
Index ¶
- func Run(opts ...Option)
- type Hook
- type Option
- func GlobalSetup(h Hook) Option
- func GlobalTeardown(h Hook) Option
- func IterationSetup(h Hook) Option
- func IterationTeardown(h Hook) Option
- func WithCommand(cmd *cobra.Command) Option
- func WithResources(p ResourceProvider) Option
- func WithRootCommand(name string) Option
- func WithRootFlags(register func(*pflag.FlagSet)) Option
- type Resource
- type ResourceProvider
- type RunOptions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
func Run(opts ...Option)
Run is the entry point for running the testrig test suite from a test binary. Calling Run executes the testrig CLI engine and will call os.Exit upon completion.
Example ¶
package main
import (
"context"
"fmt"
"github.com/smartcontractkit/testrig"
)
func main() {
testrig.Run(
// GlobalSetup runs once before any tests start.
testrig.GlobalSetup(func(_ context.Context) error {
fmt.Println("Starting mock background service...")
return nil
}),
// IterationSetup runs before each diagnose iteration.
testrig.IterationSetup(func(_ context.Context) error {
fmt.Println("Resetting database state for next iteration...")
return nil
}),
// IterationTeardown runs after each diagnose iteration.
testrig.IterationTeardown(func(_ context.Context) error {
fmt.Println("Cleaning up iteration artifacts...")
return nil
}),
// GlobalTeardown runs once after all tests finish.
testrig.GlobalTeardown(func(_ context.Context) error {
fmt.Println("Stopping mock background service...")
return nil
}),
)
}
Output:
Types ¶
type Hook ¶
Hook is a lifecycle callback. The context carries cancellation from the test runner — hooks should respect it for long-running operations.
func NewShellHook ¶
NewShellHook returns a Hook that runs cmdStr via the system shell (sh -c). The hook respects context cancellation. On non-zero exit, the combined stdout+stderr is included in the returned error so failing setup commands are diagnosable.
type Option ¶
Option configures the testrig CLI runner.
func GlobalSetup ¶
GlobalSetup registers a hook to run once before any tests.
func GlobalTeardown ¶
GlobalTeardown registers a hook to run once after all tests finish.
func IterationSetup ¶
IterationSetup registers a hook to run before each diagnose iteration.
func IterationTeardown ¶
IterationTeardown registers a hook to run after each diagnose iteration.
func WithCommand ¶ added in v0.0.2
WithCommand registers an additional subcommand on the testrig root command, for project-specific utilities (e.g. persistent database management).
func WithResources ¶ added in v0.0.2
func WithResources(p ResourceProvider) Option
WithResources registers a provider that supplies isolated infrastructure (e.g. databases) for the run. See ResourceProvider for count semantics and partial-failure behavior. Each resource's Env is appended to the child go test process; Reset and DumpDiagnostics apply during diagnose; Cleanup runs once after the command finishes.
func WithRootCommand ¶ added in v0.0.2
WithRootCommand sets the CLI name used in help text, examples, and cobra.CommandPath(). Defaults to "testrig" when unset.
func WithRootFlags ¶ added in v0.0.2
WithRootFlags registers persistent flags on the root command, available to every subcommand. Use it to add consumer flags (e.g. --database-url) that a resource provider or custom command reads.
type Resource ¶ added in v0.0.2
Resource is one prepared, isolated piece of infrastructure (e.g. a database) supplied by a ResourceProvider. See hooks.Resource for field semantics.
type ResourceProvider ¶ added in v0.0.2
type ResourceProvider = hooks.ResourceProvider
ResourceProvider supplies isolated resources for a run. See WithResources.
type RunOptions ¶
type RunOptions = hooks.RunOptions
RunOptions contains the evaluated configuration for the testrig CLI. It is exported for internal use by the CLI engine.
func BuildOptions ¶
func BuildOptions(opts ...Option) RunOptions
BuildOptions evaluates the functional options and returns the internal struct. It is exported for internal use by the CLI engine.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
testrig
command
Package main is the testrig CLI entry point.
|
Package main is the testrig CLI entry point. |
|
internal
|
|
|
assets
Package assets contains embedded files and templates.
|
Package assets contains embedded files and templates. |
|
cmd
Package cmd implements the testrig CLI commands.
|
Package cmd implements the testrig CLI commands. |
|
config
Package config holds the harness's flag-bound application config.
|
Package config holds the harness's flag-bound application config. |
|
hooks
Package hooks defines the testrig lifecycle hook interfaces and configuration.
|
Package hooks defines the testrig lifecycle hook interfaces and configuration. |
|
output
Package output centralizes harness-owned terminal writes: human-rich vs sparse (--ai-output), and whether stderr supports carriage-return progress (TTY).
|
Package output centralizes harness-owned terminal writes: human-rich vs sparse (--ai-output), and whether stderr supports carriage-return progress (TTY). |
|
runner
Package runner implements go test wrappers, iteration orchestration, and result analysis.
|
Package runner implements go test wrappers, iteration orchestration, and result analysis. |
|
termstyle
Package termstyle holds lipgloss styles shared by diagnose progress, DB setup, and summary output so the CLI reads as one palette.
|
Package termstyle holds lipgloss styles shared by diagnose progress, DB setup, and summary output so the CLI reads as one palette. |
|
tools
|
|
|
gendocs
command
Command gendocs generates the lifecycle hooks reference table from hooks.go godoc and splices it into HOOKS.md and README files.
|
Command gendocs generates the lifecycle hooks reference table from hooks.go godoc and splices it into HOOKS.md and README files. |