testctx

package
v0.0.0-llm.1 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package testctx provides a generic wrapper around Go's testing types that enables context propagation and middleware-based test configuration. It allows for structured test suites with OpenTelemetry tracing, timeouts, and other context-aware features while maintaining compatibility with standard Go testing patterns.

The core type TB[Type] is a generic wrapper around testing.TB implementations that preserves all the familiar testing.T and testing.B methods while adding context management. Two concrete types are provided for convenience:

testctx.T = TB[*testing.T]  // For standard tests
testctx.B = TB[*testing.B]  // For benchmarks

The package provides two main entry points to integrate with Go's testing machinery:

testctx.Run()   // For running test suites
testctx.Bench() // For running benchmark suites

Example usage for a hypothetical suite named "Module":

var testCtx = context.Background()

func TestModule(t *testing.T) {
    testctx.Run(testCtx, t, ModuleSuite{}, TestMiddleware()...)
}

func BenchmarkModule(b *testing.B) {
    testctx.Bench(testCtx, b, ModuleSuite{}, BenchMiddleware()...)
}

The generic Middleware[Type] system allows for writing test configuration code that works across both test and benchmark contexts. Some middlewares like WithOTelTracing and WithOTelLogging are generic and work with both types, while others like WithParallel are specialized for specific test types.

Test methods in suites receive a context.Context and either a *testctx.T or *testctx.B, which implement all the familiar methods from testing.T/testing.B:

type ModuleSuite struct{}

func (s ModuleSuite) TestSomething(ctx context.Context, t *testctx.T) {
    t.Run("subtest", func(ctx context.Context, t *testctx.T) {
        // Use context for deadlines, tracing, etc
        // Use t.Fatal, t.Error, etc as normal
    })
}

Index

Constants

View Source
const TestCtxNameAttr = "dagger.io/testctx.name"
View Source
const TestCtxPrewarmAttr = "dagger.io/testctx.prewarm"
View Source
const TestCtxTypeAttr = "dagger.io/testctx.type"

Variables

This section is empty.

Functions

func Bench added in v0.15.3

func Bench(ctx context.Context, testingB *testing.B, suite any, middleware ...Middleware[*testing.B])

func N added in v0.15.3

func N(b *TB[*testing.B]) int

func Run

func Run(ctx context.Context, testingT *testing.T, suite any, middleware ...Middleware[*testing.T])

Types

type B added in v0.15.3

type B = TB[*testing.B]

type Middleware

type Middleware[Type RunnableTB[Type]] func(*TB[Type]) *TB[Type]

func Combine

func Combine[Type RunnableTB[Type]](middleware ...Middleware[Type]) Middleware[Type]

func WithOTelLogging

func WithOTelLogging[Type RunnableTB[Type]](logger log.Logger) Middleware[Type]

func WithOTelTracing

func WithOTelTracing[Type RunnableTB[Type]](tracer trace.Tracer) Middleware[Type]

type MiddlewareB added in v0.15.3

type MiddlewareB = Middleware[*testing.B]

type MiddlewareT added in v0.15.3

type MiddlewareT = Middleware[*testing.T]

type RunnableTB added in v0.15.3

type RunnableTB[Type testing.TB] interface {
	testing.TB
	Run(string, func(Type)) bool
}

type T

type T = TB[*testing.T]

type TB added in v0.15.3

type TB[Type RunnableTB[Type]] struct {
	RunnableTB[Type]
	// contains filtered or unexported fields
}

func New

func New[Type RunnableTB[Type]](ctx context.Context, t Type) *TB[Type]

func WithParallel

func WithParallel(t *TB[*testing.T]) *TB[*testing.T]

func (*TB[Type]) BaseName added in v0.15.3

func (t *TB[Type]) BaseName() string

func (*TB[Type]) BeforeAll added in v0.15.3

func (t *TB[Type]) BeforeAll(f func(*TB[Type]) *TB[Type]) *TB[Type]

BeforeAll calls f immediately with itself and returns the result.

It is not inherited by subtests.

func (TB[Type]) BeforeEach added in v0.15.3

func (t TB[Type]) BeforeEach(f Middleware[Type]) *TB[Type]

BeforeEach configures f to run prior to each subtest.

func (*TB[Type]) Context added in v0.15.3

func (t *TB[Type]) Context() context.Context

func (*TB[Type]) Error added in v0.15.3

func (t *TB[Type]) Error(vals ...any)

func (*TB[Type]) Errorf added in v0.15.3

func (t *TB[Type]) Errorf(format string, vals ...any)

func (*TB[Type]) Errors added in v0.15.3

func (t *TB[Type]) Errors() string

func (*TB[Type]) Fatal added in v0.15.3

func (t *TB[Type]) Fatal(vals ...any)

func (*TB[Type]) Fatalf added in v0.15.3

func (t *TB[Type]) Fatalf(format string, vals ...any)

func (*TB[Type]) Log added in v0.15.3

func (t *TB[Type]) Log(vals ...any)

func (*TB[Type]) Logf added in v0.15.3

func (t *TB[Type]) Logf(format string, vals ...any)

func (*TB[Type]) Run added in v0.15.3

func (t *TB[Type]) Run(name string, f func(context.Context, *TB[Type])) bool

func (*TB[Type]) Skip added in v0.15.3

func (t *TB[Type]) Skip(vals ...any)

func (*TB[Type]) Skipf added in v0.15.3

func (t *TB[Type]) Skipf(format string, vals ...any)

func (TB[Type]) WithContext added in v0.15.3

func (t TB[Type]) WithContext(ctx context.Context) *TB[Type]

func (TB[Type]) WithLogger added in v0.15.3

func (t TB[Type]) WithLogger(logger func(*TB[Type], string)) *TB[Type]

func (*TB[Type]) WithTimeout added in v0.15.3

func (t *TB[Type]) WithTimeout(timeout time.Duration) *TB[Type]

Jump to

Keyboard shortcuts

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