functional

package
v4.9.1 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

README

Functional Test Framework

go test-like functional testing framework.

Why use functional?

functional is suggested when you want to rapidly develop code in the format of a unit test or benchmark test using existing testing tools, but run it outside of the go test environment. This is handy for use cases needing data inspection and having low tolerance for errors.

go test doesn't support running tests programmatically from compiled code; it requires the source code, which won't/shouldn't be available in production.

One such use case: runtime health check. An admin may remotely invoke a health check job and watch it run. functional can manage the health check logic once the RPC request is received.

Tools like Testify may be used for assertions and mocking.

Run Tests

import (
	"context"
	"time"

	"github.com/mailgun/holster/v4/functional"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 10 * time.Minute)
	defer cancel()

	tests := []functional.TestFunc{
		myTest1,
	}
	functional.RunSuite(ctx, "My suite", tests)
}

func myTest1(t *functional.T) {
	t.Log("Hello World.")
}

Testify Assertions

Testify is compatible with the functional testing framework as-is.

import (
	"github.com/mailgun/holster/v4/functional"
	"github.com/stretchr/testify/require"
)

func myTest1(t *functional.T) {
	retval := DoSomething()
	require.Equal(t, "OK", retval)
}

Documentation

Overview

`go test`-like functional testing framework. Can be used with Testify require/assert/mock.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context, fn TestFunc, opts ...FunctionalOption) bool

Run a test. Test named after function name.

func RunBenchmarkSuiteTimes

func RunBenchmarkSuiteTimes(ctx context.Context, suiteName string, times int, tests []BenchmarkFunc, opts ...FunctionalOption) bool

Run a suite of benchmark tests as a unit. Run each benchmark n times. Generates summary when finished.

func RunSuite

func RunSuite(ctx context.Context, suiteName string, tests []TestFunc, opts ...FunctionalOption) bool

Run a suite of tests as a unit. Generates summary when finished.

func RunWithName

func RunWithName(ctx context.Context, name string, fn TestFunc, opts ...FunctionalOption) bool

Run a test with user-provided name.

Types

type B

type B struct {
	T
	N int
	// contains filtered or unexported fields
}

Functional benchmark context.

func (*B) ResetTimer

func (b *B) ResetTimer()

func (*B) Run

func (b *B) Run(name string, fn BenchmarkFunc, opts ...FunctionalOption) BenchmarkResult

func (*B) RunTimes

func (b *B) RunTimes(name string, fn BenchmarkFunc, times int, opts ...FunctionalOption) BenchmarkResult

type BenchmarkFunc

type BenchmarkFunc func(b *B)

Functional test code.

type BenchmarkResult

type BenchmarkResult struct {
	Pass bool
	// Mean nanoseconds per operation.
	NsPerOp float64
}

func RunBenchmarkTimes

func RunBenchmarkTimes(ctx context.Context, fn BenchmarkFunc, times int, opts ...FunctionalOption) BenchmarkResult

Run a benchmark test. Test named after function name.

func RunBenchmarkWithNameTimes

func RunBenchmarkWithNameTimes(ctx context.Context, name string, fn BenchmarkFunc, times int, opts ...FunctionalOption) BenchmarkResult

Run a benchmark test with user-provided name.

type FunctionalOption

type FunctionalOption interface {
	Apply(t *T)
}

func WithArgs

func WithArgs(args ...string) FunctionalOption

func WithWriter

func WithWriter(writer io.Writer) FunctionalOption

WithWriter sets log output writer.

type T

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

Functional test context.

func (*T) Args

func (t *T) Args() []string

func (*T) Deadline

func (t *T) Deadline() (time.Time, error)

func (*T) Error

func (t *T) Error(args ...interface{})

func (*T) Errorf

func (t *T) Errorf(format string, args ...interface{})

func (*T) FailNow

func (t *T) FailNow()

func (*T) Log

func (t *T) Log(message ...interface{})

func (*T) Logf

func (t *T) Logf(format string, args ...interface{})

func (*T) Name

func (t *T) Name() string

func (*T) Run

func (t *T) Run(name string, fn TestFunc) bool

type TestFunc

type TestFunc func(t *T)

Functional test code.

Jump to

Keyboard shortcuts

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