fixenv – github.com/rekby/fixenv Index | Files | Directories

package fixenv

import "github.com/rekby/fixenv"

Index

Variables

var (
	// ErrSkipTest - error for return from fixtures
	// return the error mean skip test and cache decision about skip test for feature fixtures call
	// as usual result/error cache.
	//
	// Use special error instead of detect of test.SkipNow() need for prevent run fixture in separate goroutine for
	// skip detecting
	ErrSkipTest = errors.New("skip test")
)

Functions

func CacheResult

func CacheResult[TRes any](env Env, f GenericFixtureFunction[TRes], options ...CacheOptions) TRes

CacheResult is call f once per cache scope (default per test) and cache result (success or error). All other calls of the f will return same result.

func RunTests

func RunTests(m RunTestsI, opts ...CreateMainTestEnvOpts) int

RunTests runs the tests. It returns an exit code to pass to os.Exit.

Usage: declare in _test file TestMain function:

func TestMain(m *testing.M) {
	 os.Exit(fixenv.RunTests(m))
}

Types

type CacheOptions

type CacheOptions struct {
	// Scope for cache result
	Scope CacheScope

	// Key for cache results, must be json serializable value
	CacheKey interface{}
	// contains filtered or unexported fields
}

type CacheScope

type CacheScope int

CacheScope define life time of fixture value and allow use independent fixture values for different scopes, but share same value for one scope, which can be more then one test

const (
	// ScopeTest mean fixture function with same parameters called once per every test and subtests. Default value.
	// Second and more calls will use cached value.
	ScopeTest CacheScope = iota

	// ScopePackage mean fixture function with same parameters called once per package
	// for use the scope with TearDown function developer must initialize global handler and cleaner at TestMain.
	ScopePackage

	// ScopeTestAndSubtests mean fixture cached for top level test and subtests
	ScopeTestAndSubtests
)

type CreateMainTestEnvOpts

type CreateMainTestEnvOpts struct {
	// Fatalf equivalent of Fatalf in test.
	// Must write log, then exit from goroutine.
	// It may be panic.
	// Fatalf called if main envinment can't continue work
	Fatalf FatalfFunction

	// SkipNow is equivalent of SkipNow in test
	// default is panic
	//
	// SkipNow marks the test as having been skipped and stops its execution
	// by calling runtime.Goexit.
	// If a test fails (see Error, Errorf, Fail) and is then skipped,
	// it is still considered to have failed.
	// Execution will continue at the next test or benchmark. See also FailNow.
	// SkipNow must be called from the goroutine running the test, not from
	// other goroutines created during the test. Calling SkipNow does not stop
	// those other goroutines.
	SkipNow SkipNowFunction
}

CreateMainTestEnvOpts is options for manage package env scope

type Env

type Env interface {
	// T - return t object of current test/benchmark.
	T() T

	// CacheResult add result of call f to cache and return same result for all
	// calls for the same function and cache options within cache scope
	CacheResult(f FixtureFunction, options ...CacheOptions) interface{}
}

Env - fixture cache engine. Env interface described TEnv method and need for easy reuse different Envs with same fixtures.

The interface can be extended. Create own Envs with embed TEnv or the interface for auto-implement all methods in the future.

type EnvT

type EnvT struct {
	// contains filtered or unexported fields
}

EnvT - fixture cache and cleanup engine it created from test and pass to fixture function manage cache of fixtures, depends from fixture, param, test, scope. and call cleanup, when scope closed. It can be base to own, more powerful local environments.

func CreateMainTestEnv

func CreateMainTestEnv(opts *CreateMainTestEnvOpts) (env *EnvT, tearDown func())

CreateMainTestEnv called from TestMain for create global environment. It need only for use ScopePackage cache scope. If ScopePackage not used - no need to create main env.

func New

func New(t T) *EnvT

New create EnvT from test

func (*EnvT) CacheResult

func (e *EnvT) CacheResult(f FixtureFunction, options ...CacheOptions) interface{}

CacheResult call f callback once and cache result (ok and error), then return same result for all calls of the callback without additional calls f with same options calls max once per test (or defined test scope) See to generic wrapper: CacheResult

func (*EnvT) T

func (e *EnvT) T() T

T return test from EnvT created

type FatalfFunction

type FatalfFunction func(format string, args ...interface{})

FatalfFunction function signature of Fatalf

type FixtureCleanupFunc

type FixtureCleanupFunc func()

FixtureCleanupFunc - callback function for cleanup after fixture value out from lifetime scope it called exactly once for every succesully call fixture

type FixtureFunction

type FixtureFunction func() (*Result, error)

FixtureFunction - callback function with structured result the function can return ErrSkipTest error for skip the test

type GenericFixtureFunction

type GenericFixtureFunction[ResT any] func() (*GenericResult[ResT], error)

GenericFixtureFunction - callback function with structured result

type GenericResult

type GenericResult[ResT any] struct {
	Value ResT
	ResultAdditional
}

GenericResult of fixture callback

func NewGenericResult

func NewGenericResult[ResT any](res ResT) *GenericResult[ResT]

NewGenericResult return result struct and nil error. Use it for smaller boilerplate for define generic specifications

func NewGenericResultWithCleanup

func NewGenericResultWithCleanup[ResT any](res ResT, cleanup FixtureCleanupFunc) *GenericResult[ResT]

type Result

type Result struct {
	Value interface{}
	ResultAdditional
}

Result of fixture callback

func NewResult

func NewResult(res interface{}) *Result

func NewResultWithCleanup

func NewResultWithCleanup(res interface{}, cleanup FixtureCleanupFunc) *Result

type ResultAdditional

type ResultAdditional struct {
	Cleanup FixtureCleanupFunc
}

type RunTestsI

type RunTestsI interface {
	// Run runs the tests. It returns an exit code to pass to os.Exit.
	Run() (code int)
}

type SkipNowFunction

type SkipNowFunction func()

SkipNowFunction is function signature for SkipNow

type T

type T interface {
	// Cleanup registers a function to be called when the test (or subtest) and all its subtests complete.
	// Cleanup functions will be called in last added, first called order.
	Cleanup(func())

	// Fatalf is equivalent to Logf followed by FailNow.
	//
	// Logf formats its arguments according to the format, analogous to Printf, and records the text in the error log.
	// A final newline is added if not provided. For tests, the text will be printed only if the test fails or the -test.v flag is set.
	// For benchmarks, the text is always printed to avoid having performance depend on the value of the -test.v flag.
	//
	// FailNow marks the function as having failed and stops its execution by calling runtime.Goexit
	// (which then runs all deferred calls in the current goroutine). Execution will continue at the next test or benchmark. FailNow must be called from the goroutine running the test or benchmark function, not from other goroutines created during the test. Calling FailNow does not stop those other goroutines.
	Fatalf(format string, args ...interface{})

	// Logf formats its arguments according to the format, analogous to Printf, and
	// records the text in the error log. A final newline is added if not provided. For
	// tests, the text will be printed only if the test fails or the -test.v flag is
	// set. For benchmarks, the text is always printed to avoid having performance
	// depend on the value of the -test.v flag.
	Logf(format string, args ...interface{})

	// Name returns the name of the running (sub-) test or benchmark.
	//
	// The name will include the name of the test along with the names
	// of any nested sub-tests. If two sibling sub-tests have the same name,
	// Name will append a suffix to guarantee the returned name is unique.
	Name() string

	// SkipNow is followed by testing.T.SkipNow().
	// Don't use SkipNow() for skip test from fixture - use special error ErrSkipTest for it.
	//
	// SkipNow marks the test as having been skipped and stops its execution
	// by calling runtime.Goexit.
	// If a test fails (see Error, Errorf, Fail) and is then skipped,
	// it is still considered to have failed.
	// Execution will continue at the next test or benchmark. See also FailNow.
	// SkipNow must be called from the goroutine running the test, not from
	// other goroutines created during the test. Calling SkipNow does not stop
	// those other goroutines.
	SkipNow()

	// Skipped reports whether the test was skipped.
	Skipped() bool
}

T is subtype of testing.TB

Source Files

cache.go env.go env_generic_sugar.go interface.go maintest.go scope_info.go

Directories

PathSynopsis
internal
sf
Version
v0.7.0 (latest)
Published
Aug 9, 2024
Platform
linux/amd64
Imports
8 packages
Last checked
now

Tools for package owners.