devtest

package
v1.13.7 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const ExpectPreconditionsMet = "DEVNET_EXPECT_PRECONDITIONS_MET"

Variables

View Source
var DefaultTestLogLevel = log.LevelInfo

DefaultTestLogLevel is set to info level to show relevant logs without being overly verbose unless configured otherwise.

View Source
var (
	// RootContext is the context that is used for the root of the test suite.
	// It should be set for good before any tests are run.
	RootContext = context.Background()
)

Functions

func AddTestScope added in v1.13.4

func AddTestScope(ctx context.Context, scope string) context.Context

AddTestScope combines the sub-scope with the test-scope of the context, and returns a context with the updated scope value.

func RunParallel

func RunParallel[V any](t T, elems []V, fn func(t T, v V))

RunParallel runs the test-function, for each of the given elements, in parallel. This awaits the result of all sub-tests to complete, by grouping everything into a regular sub-test.

func TestScope added in v1.13.4

func TestScope(ctx context.Context) string

TestScope retrieves the test-scope from the context

Types

type CommonT

type CommonT interface {
	Error(args ...any)
	Errorf(format string, args ...any)
	Fail()
	FailNow()
	SkipNow()

	TempDir() string
	Cleanup(fn func())
	Log(args ...any)
	Logf(format string, args ...any)
	Helper()
	Name() string

	Logger() log.Logger
	Tracer() trace.Tracer
	Ctx() context.Context
	Require() *testreq.Assertions
}

CommonT is a subset of testing.T, extended with a few common utils. This interface should not be used directly. Instead, use T in test-scope, or P when operating at package level.

This CommonT interface is minimal enough such that it can be implemented by tooling, and a *testing.T can be used with minimal wrapping.

type P

type P interface {
	CommonT

	// WithCtx makes a copy of P with a specific context.
	// The ctx must match the test-scope of the existing context.
	// This function is used to create a P with annotated context, e.g. a specific resource.
	WithCtx(ctx context.Context) P

	// TempDir creates a temporary directory, and returns the file-path.
	// This directory is cleaned up at the end of the package,
	// and can be shared safely between tests that run in that package scope.
	TempDir() string

	// Cleanup runs the given function at the end of the package-scope.
	// This function will clean-up once the package-level testing is fully complete.
	// These resources can thus be shared safely between tests.
	Cleanup(fn func())

	// Close closes the testing handle. This cancels the context and runs all cleanup.
	Close()
	// contains filtered or unexported methods
}

P is used by the preset package and system backends as testing interface, to host package-wide resources.

func NewP

func NewP(ctx context.Context, logger log.Logger, onFail func(now bool), onSkipNow func()) P

type T

type T interface {
	CommonT

	// TempDir creates a temporary directory, and returns the file-path.
	// This directory is cleaned up at the end of the test, and must not be shared between tests.
	TempDir() string

	// Cleanup runs the given function at the end of the test-scope,
	// or at the end of the sub-test (if this is a nested test).
	// This function will clean-up before the package-level testing scope may be complete.
	// Do not use the test-scope cleanup with shared resources.
	Cleanup(fn func())

	// Run runs the given function in as a sub-test.
	Run(name string, fn func(T))

	// Ctx returns a context that will be canceled at the end of this (sub) test-scope,
	// and inherits the context of the parent-test-scope.
	Ctx() context.Context

	// WithCtx makes a copy of T with a specific context.
	// The ctx must match the test-scope of the existing context.
	// This function is used to create a T with annotated context, e.g. a specific resource, rather than a sub-scope.
	WithCtx(ctx context.Context) T

	// Parallel signals that this test is to be run in parallel with (and only with) other parallel tests.
	Parallel()

	// Skip is equivalent to Log followed by SkipNow.
	Skip(args ...any)
	// Skipped reports whether the test was skipped.
	Skipped() bool
	// Skipf is equivalent to Logf followed by SkipNow.
	Skipf(format string, args ...any)
	// SkipNow marks the test as skipped and stops test execution.
	// It is remapped to FailNow if the env var DEVNET_EXPECT_PRECONDITIONS_MET is set to true.
	SkipNow()

	// Gate provides everything that Require does, but skips instead of fails the test upon error.
	Gate() *testreq.Assertions

	// Deadline reports the time at which the test binary will have
	// exceeded the timeout specified by the -timeout flag.
	//
	// The ok result is false if the -timeout flag indicates "no timeout" (0).
	Deadline() (deadline time.Time, ok bool)

	// This distinguishes the interface from other testing interfaces,
	// such as the one used at package-level for shared system construction.
	TestOnly()
}

func ParallelT

func ParallelT(t *testing.T) T

ParallelT creates a T interface with parallel testing enabled by default

func SerialT

func SerialT(t *testing.T) T

SerialT wraps around a test-logger and turns it into a T for devstack testing.

Jump to

Keyboard shortcuts

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