suite

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

README

Test Suite

Language: EN | RU

testsuite provides a typed top-level-test harness on top of Go testing.

Highlights

  • real top-level TestXxx functions remain visible to IDE and go test -run;
  • typed Shared / Deps / Case state via suite.New[Shared, Deps, Case](...);
  • additive BeforeAll / AfterAll / BeforeEach / AfterEach hooks;
  • package-wide BeforeAll / AfterAll coordination through suite.RunMain(m);
  • optional Allure adapter in testing/suite/allure.
  • keep shared environment in BeforeAll / AfterAll, common per-test fixture in one or more WithBeforeEach(...) hooks, and scenario-specific actions in TestXxx;
  • add local aliases like type T = suite.T[Shared, Deps, Case] and type LT = suite.LifecycleT[Shared];
  • keep Shared for package-wide bootstrap contract, Deps for per-test bound dependencies, and Case for scenario data and fixtures;
  • stack reusable hooks with Use(...) and additional local hooks with WithBeforeEach(...) / WithAfterEach(...) instead of collapsing everything into one callback;
  • keep test-specific scenario data near the test body instead of switching on t.Name() inside hooks;
  • extract only repeated actions/assertions into helpers; the main scenario should stay visible inside the top-level test.

Validation

go test ./modules/testsuite/...
go vet ./modules/testsuite/...

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

View Source
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

func RunMain

func RunMain(m *testing.M) int

RunMain wraps package TestMain and finalizes all suites that participated in the current package test run.

Types

type AfterAllHook

type AfterAllHook[Shared any] func(*LifecycleT[Shared])

AfterAllHook runs once after m.Run() completes for a suite.

type AfterEachHook

type AfterEachHook[Shared, Deps, Case any] func(*T[Shared, Deps, Case])

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

type BeforeEachHook[Shared, Deps, Case any] func(*T[Shared, Deps, Case])

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 Use

func Use[Shared, Deps, Case any](hooks Hooks[Shared, Deps, Case]) Option[Shared, Deps, Case]

Use appends a reusable hook set.

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

func WithParallel[Shared, Deps, Case any](enabled bool) Option[Shared, Deps, Case]

WithParallel marks every top-level test that uses the suite as parallel.

type Reporter

type Reporter interface {
	Step(name string, run func())
	Attach(name, mime string, content []byte)
}

Reporter writes steps and attachments for a single test case.

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

func New[Shared, Deps, Case any](opts ...Option[Shared, Deps, Case]) *Suite[Shared, Deps, Case]

New constructs a typed suite harness. BeforeAll/AfterAll require package TestMain to call suite.RunMain(m) for package-wide semantics.

func (*Suite[Shared, Deps, Case]) Run

func (s *Suite[Shared, Deps, Case]) Run(t *testing.T, body func(*T[Shared, Deps, Case]))

Run executes one top-level test through the suite harness.

func (*Suite[Shared, Deps, Case]) RunNamed

func (s *Suite[Shared, Deps, Case]) RunNamed(t *testing.T, displayName string, body func(*T[Shared, Deps, Case]))

RunNamed executes one top-level test with a custom display name for adapters.

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

func (t *T[Shared, Deps, Case]) Allure() provider.T

Allure returns the current Allure provider when an adapter installed one.

func (*T[Shared, Deps, Case]) AllureStepContext

func (t *T[Shared, Deps, Case]) AllureStepContext() provider.StepCtx

AllureStepContext adapts the current Allure test provider to provider.StepCtx.

func (*T[Shared, Deps, Case]) AttachBytes

func (t *T[Shared, Deps, Case]) AttachBytes(name, mime string, content []byte)

AttachBytes writes a raw attachment.

func (*T[Shared, Deps, Case]) AttachJSON

func (t *T[Shared, Deps, Case]) AttachJSON(name string, value any)

AttachJSON writes a formatted JSON attachment.

func (*T[Shared, Deps, Case]) AttachText

func (t *T[Shared, Deps, Case]) AttachText(name, text string)

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]) Context

func (t *T[Shared, Deps, Case]) Context() context.Context

Context proxies testing.T.Context.

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]) Error

func (t *T[Shared, Deps, Case]) Error(args ...any)

Error proxies testing.T.Error.

func (*T[Shared, Deps, Case]) Errorf

func (t *T[Shared, Deps, Case]) Errorf(format string, args ...any)

Errorf proxies testing.T.Errorf.

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]) Fatal

func (t *T[Shared, Deps, Case]) Fatal(args ...any)

Fatal proxies testing.T.Fatal.

func (*T[Shared, Deps, Case]) Fatalf

func (t *T[Shared, Deps, Case]) Fatalf(format string, args ...any)

Fatalf proxies testing.T.Fatalf.

func (*T[Shared, Deps, Case]) Helper

func (t *T[Shared, Deps, Case]) Helper()

Helper proxies testing.T.Helper.

func (*T[Shared, Deps, Case]) LifecycleLogSnapshot

func (t *T[Shared, Deps, Case]) LifecycleLogSnapshot() string

LifecycleLogSnapshot returns the setup log captured before this test started.

func (*T[Shared, Deps, Case]) Log

func (t *T[Shared, Deps, Case]) Log(args ...any)

Log proxies testing.T.Log.

func (*T[Shared, Deps, Case]) Logf

func (t *T[Shared, Deps, Case]) Logf(format string, args ...any)

Logf proxies testing.T.Logf.

func (*T[Shared, Deps, Case]) Name

func (t *T[Shared, Deps, Case]) Name() string

Name proxies testing.T.Name.

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]) RealT

func (t *T[Shared, Deps, Case]) RealT() *testing.T

RealT returns the underlying *testing.T.

func (*T[Shared, Deps, Case]) SetAllureProvider

func (t *T[Shared, Deps, Case]) SetAllureProvider(p provider.T)

SetAllureProvider stores the active Allure provider. Adapter packages use it.

func (*T[Shared, Deps, Case]) SetReporter

func (t *T[Shared, Deps, Case]) SetReporter(reporter Reporter)

SetReporter swaps the active case reporter. Adapter packages use it.

func (*T[Shared, Deps, Case]) Setenv

func (t *T[Shared, Deps, Case]) Setenv(key, value string)

Setenv proxies testing.T.Setenv.

func (*T[Shared, Deps, Case]) Shared

func (t *T[Shared, Deps, Case]) Shared() *Shared

Shared returns suite-wide shared state.

func (*T[Shared, Deps, Case]) Step

func (t *T[Shared, Deps, Case]) Step(name string, run func(*T[Shared, Deps, Case]))

Step runs a reporting step when a reporter is installed.

func (*T[Shared, Deps, Case]) TempDir

func (t *T[Shared, Deps, Case]) TempDir() string

TempDir proxies testing.T.TempDir.

Directories

Path Synopsis
adapters

Jump to

Keyboard shortcuts

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