testcase

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FieldValue

func FieldValue[T any](
	tc any,
	k reflect.Kind,
	result func(v reflect.Value) T,
	fields ...string,
) (T, bool)

FieldValue retrieves a field value of a specified type (reflect.Kind) from a struct or pointer to a struct.

It checks for the specified field names and returns the value if found using a supplied function to obtain the value from the field ref.

func IsDebugging

func IsDebugging(tc any) bool

IsDebugging returns true if the specified test case is a struct with a bool debug/Debug field that is set true, otherwise it returns false.

func IsSkipping

func IsSkipping(tc any) bool

IsSkipping returns true if the specified test case is a struct with a bool skip/Skip field that is set true, otherwise it returns false.

func NameOrDefault

func NameOrDefault(tc any, n string, i int) string

NameOrDefault derives a name for a given test case. If a name (n) is given, it is used without checking the test case.

If no name is given and the test case is a struct with a string field named "Scenario", "scenario", "NameOrDefault", or "name", then this field is used as the name (if not empty or whitespace).

If the test case is not a struct, has no name field or has a name which is empty, a default name in the format "testcase-NNN" where NNN is the index (i) of the scenario.

Types

type AnonCaseExecution

type AnonCaseExecution[T any] func(tc T)

AnonCaseExecution[T] is a function type that takes a pointer to a test case of type T without a name.

type Controller

type Controller[T any] struct {
	// contains filtered or unexported fields
}

func NewController

func NewController[T any](data T, index int, name string) Controller[T]

type Executor

type Executor[T any] struct {
	// contains filtered or unexported fields
}

func NewExecutor

func NewExecutor[T any](fn any) Executor[T]

func (Executor[T]) Execute

func (exec Executor[T]) Execute(name string, tc T)

type Flags

type Flags int
const (
	// NoFlags indicates that no flags are set for the test case
	NoFlags Flags = 0

	// Parallel indicates that the test case should be run in parallel
	Parallel Flags = 1

	// Debug indicates that the test case is a debug target
	Debug Flags = 2

	// Skip indicates that the test case should be skipped
	Skip Flags = 4
)

type NamedCaseExecution

type NamedCaseExecution[T any] func(name string, tc T)

NamedCaseExecution[T] is a function type that takes a test case name and a pointer to a test case of type T.

type Registration

type Registration[T any] func(*Runner[T], Flags)

type Runner

type Runner[T any] struct {
	TestingT
	TestExecutor[T]

	CaseControllers []Controller[T]
}

Runner[T] is a struct that holds a testing.T interface, a test executor, a setup controller, and a list of case controllers, used for executing a number of test cases of type T.

It provides methods to configure setup functions and add and run test cases.

func NewRunner

func NewRunner[T any](exec TestExecutor[T]) Runner[T]

NewRunner creates a new Runner instance with the provided test executor

func (*Runner[T]) AddCase

func (tcr *Runner[T]) AddCase(name string, tc T, flags ...Flags)

AddCase adds a test case to the runner. The name is used to identify the test case in the test output. The tc provides the data for the test case.

If the name is an empty or whitespace string, a name will be determined as follows:

  • if the test case data is a struct (or pointer to a struct) and has a string field named "Scenario", "scenario", "Name", or "name", with a non-empty or whitespace value, that will be used as the name. Otherwise a default name is derived in the format "testcase-NNN" where NNN is the 1-based index of the test case in the list of test cases.

If the test case data has a bool debug/Debug field that is set true, the test case will be marked as a debug test case.

If the test case data has a bool skip/Skip field that is set true, the test case will be marked as a skip test case.

func (Runner[T]) Run

func (tcr Runner[T]) Run()

Run runs the test cases in the runner. Each test case is run as a subtest in the current test frame. The test case name is used to identify the test case in the test output.

type TestExecutor

type TestExecutor[T any] interface {
	Execute(name string, tc T)
}

TestExecutor[T] is an interface that defines a method to execute an individual test case of type T.

type TestingT

type TestingT interface {
	Errorf(string, ...any)
	Helper()
	Parallel()
	Run(string, func(t *testing.T)) bool
}

TestingT is an interface that describes the methods of a testing.T instance that are used in this package.

func GetT

func GetT() TestingT

GetT() is a helper function to retrieve the current TestingT instance. It is used to ensure that the test helper is called correctly.

Jump to

Keyboard shortcuts

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