Documentation
¶
Overview ¶
Package suite provides a typed test harness on top of Go testing.
Suites keep shared package-level state in BeforeAll/AfterAll hooks and typed per-test state in BeforeEach/AfterEach hooks, while top-level TestXxx functions remain real Go tests for IDE and go test tooling.
Index ¶
- Constants
- func RunMain(m *testing.M) int
- type AfterAllHook
- type AfterEachHook
- type BeforeAllHook
- type BeforeEachHook
- type CaseRunner
- type Hooks
- type LifecycleT
- func (t *LifecycleT[Shared]) AttachBytes(name, mime string, content []byte)
- func (t *LifecycleT[Shared]) AttachJSON(name string, value any)
- func (t *LifecycleT[Shared]) AttachText(name, text string)
- func (t *LifecycleT[Shared]) Cleanup(fn func())
- func (t *LifecycleT[Shared]) Fatal(args ...any)
- func (t *LifecycleT[Shared]) Fatalf(format string, args ...any)
- func (t *LifecycleT[Shared]) Log(args ...any)
- func (t *LifecycleT[Shared]) Logf(format string, args ...any)
- func (t *LifecycleT[Shared]) Setenv(key, value string)
- func (t *LifecycleT[Shared]) Shared() *Shared
- func (t *LifecycleT[Shared]) Step(name string, run func(*LifecycleT[Shared]))
- func (t *LifecycleT[Shared]) TempDir() string
- func (t *LifecycleT[Shared]) TempFilePath(name string) string
- type Option
- func Use[Shared, Deps, Case any](hooks Hooks[Shared, Deps, Case]) Option[Shared, Deps, Case]
- func WithAfterAll[Shared, Deps, Case any](hook AfterAllHook[Shared]) Option[Shared, Deps, Case]
- func WithAfterEach[Shared, Deps, Case any](hook AfterEachHook[Shared, Deps, Case]) Option[Shared, Deps, Case]
- func WithBeforeAll[Shared, Deps, Case any](hook BeforeAllHook[Shared]) Option[Shared, Deps, Case]
- func WithBeforeEach[Shared, Deps, Case any](hook BeforeEachHook[Shared, Deps, Case]) Option[Shared, Deps, Case]
- func WithCaseRunner[Shared, Deps, Case any](runner CaseRunner[Shared, Deps, Case]) Option[Shared, Deps, Case]
- func WithParallel[Shared, Deps, Case any](enabled bool) Option[Shared, Deps, Case]
- type Reporter
- type Suite
- type T
- func (t *T[Shared, Deps, Case]) Allure() provider.T
- func (t *T[Shared, Deps, Case]) AllureStepContext() provider.StepCtx
- func (t *T[Shared, Deps, Case]) AttachBytes(name, mime string, content []byte)
- func (t *T[Shared, Deps, Case]) AttachJSON(name string, value any)
- func (t *T[Shared, Deps, Case]) AttachText(name, text string)
- func (t *T[Shared, Deps, Case]) Case() *Case
- func (t *T[Shared, Deps, Case]) Cleanup(fn func())
- func (t *T[Shared, Deps, Case]) Context() context.Context
- func (t *T[Shared, Deps, Case]) Deps() *Deps
- func (t *T[Shared, Deps, Case]) Error(args ...any)
- func (t *T[Shared, Deps, Case]) Errorf(format string, args ...any)
- func (t *T[Shared, Deps, Case]) Fail()
- func (t *T[Shared, Deps, Case]) FailNow()
- func (t *T[Shared, Deps, Case]) Fatal(args ...any)
- func (t *T[Shared, Deps, Case]) Fatalf(format string, args ...any)
- func (t *T[Shared, Deps, Case]) Helper()
- func (t *T[Shared, Deps, Case]) LifecycleLogSnapshot() string
- func (t *T[Shared, Deps, Case]) Log(args ...any)
- func (t *T[Shared, Deps, Case]) Logf(format string, args ...any)
- func (t *T[Shared, Deps, Case]) Name() string
- func (t *T[Shared, Deps, Case]) Parallel()
- func (t *T[Shared, Deps, Case]) RealT() *testing.T
- func (t *T[Shared, Deps, Case]) SetAllureProvider(p provider.T)
- func (t *T[Shared, Deps, Case]) SetReporter(reporter Reporter)
- func (t *T[Shared, Deps, Case]) Setenv(key, value string)
- func (t *T[Shared, Deps, Case]) Shared() *Shared
- func (t *T[Shared, Deps, Case]) Step(name string, run func(*T[Shared, Deps, Case]))
- func (t *T[Shared, Deps, Case]) TempDir() string
Constants ¶
const ( // MimeText is a plain text attachment. MimeText = "text/plain; charset=utf-8" // MimeJSON is a JSON attachment. MimeJSON = "application/json" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AfterAllHook ¶
type AfterAllHook[Shared any] func(*LifecycleT[Shared])
AfterAllHook runs once after m.Run() completes for a suite.
type AfterEachHook ¶
AfterEachHook runs for every top-level test that uses the suite in reverse order.
type BeforeAllHook ¶
type BeforeAllHook[Shared any] func(*LifecycleT[Shared])
BeforeAllHook runs once per package test run for a suite.
type BeforeEachHook ¶
BeforeEachHook runs for every top-level test that uses the suite.
type CaseRunner ¶
type CaseRunner[Shared, Deps, Case any] func(ctx *T[Shared, Deps, Case], displayName string, run func())
CaseRunner controls how one test case is executed.
The default runner executes run() directly on the current *testing.T. Adapters such as testing/suite/allure replace it to provide richer reporting.
type Hooks ¶
type Hooks[Shared, Deps, Case any] struct { BeforeAll []BeforeAllHook[Shared] AfterAll []AfterAllHook[Shared] BeforeEach []BeforeEachHook[Shared, Deps, Case] AfterEach []AfterEachHook[Shared, Deps, Case] }
Hooks groups reusable lifecycle hooks.
type LifecycleT ¶
type LifecycleT[Shared any] struct { // contains filtered or unexported fields }
LifecycleT runs suite-level hooks outside a concrete *testing.T.
func (*LifecycleT[Shared]) AttachBytes ¶
func (t *LifecycleT[Shared]) AttachBytes(name, mime string, content []byte)
AttachBytes records a lifecycle attachment in the shared setup log.
func (*LifecycleT[Shared]) AttachJSON ¶
func (t *LifecycleT[Shared]) AttachJSON(name string, value any)
AttachJSON records a JSON lifecycle attachment.
func (*LifecycleT[Shared]) AttachText ¶
func (t *LifecycleT[Shared]) AttachText(name, text string)
AttachText records a text lifecycle attachment.
func (*LifecycleT[Shared]) Cleanup ¶
func (t *LifecycleT[Shared]) Cleanup(fn func())
Cleanup registers a suite-level cleanup that runs after AfterAll hooks.
func (*LifecycleT[Shared]) Fatal ¶
func (t *LifecycleT[Shared]) Fatal(args ...any)
Fatal aborts suite lifecycle execution.
func (*LifecycleT[Shared]) Fatalf ¶
func (t *LifecycleT[Shared]) Fatalf(format string, args ...any)
Fatalf aborts suite lifecycle execution with a formatted message.
func (*LifecycleT[Shared]) Log ¶
func (t *LifecycleT[Shared]) Log(args ...any)
Log records a lifecycle log line.
func (*LifecycleT[Shared]) Logf ¶
func (t *LifecycleT[Shared]) Logf(format string, args ...any)
Logf records a formatted lifecycle log line.
func (*LifecycleT[Shared]) Setenv ¶
func (t *LifecycleT[Shared]) Setenv(key, value string)
Setenv applies an environment variable and restores it during lifecycle cleanup.
func (*LifecycleT[Shared]) Shared ¶
func (t *LifecycleT[Shared]) Shared() *Shared
Shared returns suite-wide state for BeforeAll/AfterAll hooks.
func (*LifecycleT[Shared]) Step ¶
func (t *LifecycleT[Shared]) Step(name string, run func(*LifecycleT[Shared]))
Step records a lifecycle step and runs its body.
func (*LifecycleT[Shared]) TempDir ¶
func (t *LifecycleT[Shared]) TempDir() string
TempDir creates a temp directory and removes it during lifecycle cleanup.
func (*LifecycleT[Shared]) TempFilePath ¶
func (t *LifecycleT[Shared]) TempFilePath(name string) string
TempFilePath allocates a path inside a new temp directory.
type Option ¶
type Option[Shared, Deps, Case any] func(*config[Shared, Deps, Case])
Option configures a typed test suite harness.
func WithAfterAll ¶
func WithAfterAll[Shared, Deps, Case any](hook AfterAllHook[Shared]) Option[Shared, Deps, Case]
WithAfterAll appends a suite-wide teardown hook.
func WithAfterEach ¶
func WithAfterEach[Shared, Deps, Case any](hook AfterEachHook[Shared, Deps, Case]) Option[Shared, Deps, Case]
WithAfterEach appends a per-test teardown hook.
func WithBeforeAll ¶
func WithBeforeAll[Shared, Deps, Case any](hook BeforeAllHook[Shared]) Option[Shared, Deps, Case]
WithBeforeAll appends a suite-wide setup hook.
func WithBeforeEach ¶
func WithBeforeEach[Shared, Deps, Case any](hook BeforeEachHook[Shared, Deps, Case]) Option[Shared, Deps, Case]
WithBeforeEach appends a per-test setup hook.
func WithCaseRunner ¶
func WithCaseRunner[Shared, Deps, Case any](runner CaseRunner[Shared, Deps, Case]) Option[Shared, Deps, Case]
WithCaseRunner replaces the low-level case runner. Adapter packages use it.
func WithParallel ¶
WithParallel marks every top-level test that uses the suite as parallel.
type Suite ¶
type Suite[Shared, Deps, Case any] struct { // contains filtered or unexported fields }
Suite coordinates typed shared/per-test state across top-level tests.
func New ¶
New constructs a typed suite harness. BeforeAll/AfterAll require package TestMain to call suite.RunMain(m) for package-wide semantics.
type T ¶
type T[Shared, Deps, Case any] struct { // contains filtered or unexported fields }
T wraps a concrete top-level test and typed shared/per-test state.
func (*T[Shared, Deps, Case]) Allure ¶
Allure returns the current Allure provider when an adapter installed one.
func (*T[Shared, Deps, Case]) AllureStepContext ¶
AllureStepContext adapts the current Allure test provider to provider.StepCtx.
func (*T[Shared, Deps, Case]) AttachBytes ¶
AttachBytes writes a raw attachment.
func (*T[Shared, Deps, Case]) AttachJSON ¶
AttachJSON writes a formatted JSON attachment.
func (*T[Shared, Deps, Case]) AttachText ¶
AttachText writes a text attachment.
func (*T[Shared, Deps, Case]) Case ¶
func (t *T[Shared, Deps, Case]) Case() *Case
Case returns state scoped to the current top-level test.
func (*T[Shared, Deps, Case]) Cleanup ¶
func (t *T[Shared, Deps, Case]) Cleanup(fn func())
Cleanup proxies testing.T.Cleanup.
func (*T[Shared, Deps, Case]) Deps ¶
func (t *T[Shared, Deps, Case]) Deps() *Deps
Deps returns per-test infrastructure dependencies derived from shared state.
func (*T[Shared, Deps, Case]) Fail ¶
func (t *T[Shared, Deps, Case]) Fail()
Fail proxies testing.T.Fail.
func (*T[Shared, Deps, Case]) FailNow ¶
func (t *T[Shared, Deps, Case]) FailNow()
FailNow proxies testing.T.FailNow.
func (*T[Shared, Deps, Case]) Helper ¶
func (t *T[Shared, Deps, Case]) Helper()
Helper proxies testing.T.Helper.
func (*T[Shared, Deps, Case]) LifecycleLogSnapshot ¶
LifecycleLogSnapshot returns the setup log captured before this test started.
func (*T[Shared, Deps, Case]) Parallel ¶
func (t *T[Shared, Deps, Case]) Parallel()
Parallel proxies testing.T.Parallel for nested subtests if needed.
func (*T[Shared, Deps, Case]) SetAllureProvider ¶
SetAllureProvider stores the active Allure provider. Adapter packages use it.
func (*T[Shared, Deps, Case]) SetReporter ¶
SetReporter swaps the active case reporter. Adapter packages use it.
func (*T[Shared, Deps, Case]) Shared ¶
func (t *T[Shared, Deps, Case]) Shared() *Shared
Shared returns suite-wide shared state.