devtest

package
v1.13.3 Latest Latest
Warning

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

Go to latest
Published: May 15, 2025 License: MIT Imports: 14 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const ExpectPreconditionsMet = "DEVNET_EXPECT_PRECONDITIONS_MET"

Variables

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 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.

Types

type CommonT

type CommonT interface {
	Errorf(format string, args ...interface{})
	FailNow()

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

	Logger() Logger
	Tracer() trace.Tracer
	Ctx() context.Context
	Require() *require.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 Logger added in v1.13.3

type Logger interface {
	log.Logger
	WithContext(context.Context) Logger
}

func NewLogger added in v1.13.3

func NewLogger(ctx context.Context, t testlog.Testing, level slog.Level) Logger

type P

type P interface {
	CommonT

	// 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()) 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

	// 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() *require.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)
	// contains filtered or unexported methods
}

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