testctx

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2025 License: Apache-2.0 Imports: 6 Imported by: 3

README

testctx

*testing.T with context.Context and middleware

Package testctx aims to represent what *testing.T might have looked like if context.Context existed at the time it was created.

The main benefit of joining these two packages is support for test middleware, which can greatly cut down on repetition (t.Parallel()) and allow for transparent test instrumentation.

differences from Go 1.24 t.Context()

Go 1.24 adds a t.Context() accessor which provides a context.Context that will be canceled when the test completes, just before t.Cleanup-registered funcs are executed. However, it does not provide any way to modify the context.

By contrast, this package provides both a t.Context() and a t.WithContext(ctx) for modifying the context that is passed to sub-tests, and additionally passes the t.Context() value as a ctx argument to tests.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type B

type B = W[*testing.B]

B is a wrapper around *testing.B

type BenchFunc

type BenchFunc = RunFunc[*testing.B]

BenchFunc is a benchmark function that takes a context and B

type Middleware

type Middleware[T Runner[T]] func(RunFunc[T]) RunFunc[T]

Middleware represents a function that can wrap a test function

func WithParallel

func WithParallel() Middleware[*testing.T]

WithParallel creates middleware that runs tests in parallel

func WithTimeout

func WithTimeout[T Runner[T]](d time.Duration) Middleware[T]

WithTimeout creates middleware that adds a timeout to the test context

type RunFunc

type RunFunc[T Runner[T]] func(context.Context, *W[T])

RunFunc represents a test function that takes a context and a wrapper

type Runner

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

Runner is a constraint for types that support subtests/subbenchmarks

type T

type T = W[*testing.T]

T is a wrapper around *testing.T

type TestFunc

type TestFunc = RunFunc[*testing.T]

TestFunc is a test function that takes a context and T

type W

type W[T Runner[T]] struct {
	// contains filtered or unexported fields
}

W is a context-aware wrapper for test/benchmark types

func New

func New[T Runner[T]](t T) *W[T]

New creates a new context-aware test helper

func (*W[T]) BaseName

func (w *W[T]) BaseName() string

BaseName returns the name of the test without the full path prefix

func (*W[T]) Cleanup

func (w *W[T]) Cleanup(f func())

Cleanup registers a function to be called when the test completes

func (*W[T]) Context

func (w *W[T]) Context() context.Context

Context returns the current context

func (*W[T]) Error

func (w *W[T]) Error(args ...any)

Forward testing.TB methods

func (*W[T]) Errorf

func (w *W[T]) Errorf(format string, args ...any)

func (*W[T]) Fail

func (w *W[T]) Fail()

func (*W[T]) FailNow

func (w *W[T]) FailNow()

func (*W[T]) Failed

func (w *W[T]) Failed() bool

func (*W[T]) Fatal

func (w *W[T]) Fatal(args ...any)

func (*W[T]) Fatalf

func (w *W[T]) Fatalf(format string, args ...any)

func (*W[T]) Helper

func (w *W[T]) Helper()

func (*W[T]) Log

func (w *W[T]) Log(args ...any)

func (*W[T]) Logf

func (w *W[T]) Logf(format string, args ...any)

func (*W[T]) Name

func (w *W[T]) Name() string

func (*W[T]) Run

func (w *W[T]) Run(name string, fn RunFunc[T]) bool

Run runs a subtest with the given name and function

func (*W[T]) RunSuite

func (w *W[T]) RunSuite(s any)

RunSuite runs all test methods on the given suite as subtests

func (*W[T]) Skip

func (w *W[T]) Skip(args ...any)

func (*W[T]) SkipNow

func (w *W[T]) SkipNow()

func (*W[T]) Skipf

func (w *W[T]) Skipf(format string, args ...any)

func (*W[T]) Skipped

func (w *W[T]) Skipped() bool

func (*W[T]) TempDir

func (w *W[T]) TempDir() string

func (*W[T]) Unwrap

func (w *W[T]) Unwrap() T

Unwrap returns the underlying test/benchmark type

func (*W[T]) Use

func (w *W[T]) Use(m ...Middleware[T]) *W[T]

Use adds middleware to the test helper

func (*W[T]) WithContext

func (w *W[T]) WithContext(ctx context.Context) *W[T]

WithContext creates a new wrapper with the given context

Directories

Path Synopsis
otelmw module
oteltest module

Jump to

Keyboard shortcuts

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