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
- func Bench(ctx context.Context, testingB *testing.B, suite any, ...)
- func N(b *TB[*testing.B]) int
- func Run(ctx context.Context, testingT *testing.T, suite any, ...)
- type B
- type Middleware
- type MiddlewareB
- type MiddlewareT
- type RunnableTB
- type T
- type TB
- func (t *TB[Type]) BaseName() string
- func (t *TB[Type]) BeforeAll(f func(*TB[Type]) *TB[Type]) *TB[Type]
- func (t TB[Type]) BeforeEach(f Middleware[Type]) *TB[Type]
- func (t *TB[Type]) Context() context.Context
- func (t *TB[Type]) Error(vals ...any)
- func (t *TB[Type]) Errorf(format string, vals ...any)
- func (t *TB[Type]) Errors() string
- func (t *TB[Type]) Fatal(vals ...any)
- func (t *TB[Type]) Fatalf(format string, vals ...any)
- func (t *TB[Type]) Log(vals ...any)
- func (t *TB[Type]) Logf(format string, vals ...any)
- func (t *TB[Type]) Run(name string, f func(context.Context, *TB[Type])) bool
- func (t *TB[Type]) Skip(vals ...any)
- func (t *TB[Type]) Skipf(format string, vals ...any)
- func (t TB[Type]) WithContext(ctx context.Context) *TB[Type]
- func (t TB[Type]) WithLogger(logger func(*TB[Type], string)) *TB[Type]
- func (t *TB[Type]) WithTimeout(timeout time.Duration) *TB[Type]
Constants ¶
const TestCtxNameAttr = "dagger.io/testctx.name"
const TestCtxPrewarmAttr = "dagger.io/testctx.prewarm"
const TestCtxTypeAttr = "dagger.io/testctx.type"
Variables ¶
This section is empty.
Functions ¶
Types ¶
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 TB ¶ added in v0.15.3
type TB[Type RunnableTB[Type]] struct { RunnableTB[Type] // contains filtered or unexported fields }
func (*TB[Type]) BeforeAll ¶ added in v0.15.3
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.