Version: v0.0.0-...-d4a0794 Latest Latest

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

Go to latest
Published: Dec 9, 2020 License: Apache-2.0 Imports: 6 Imported by: 0



Package functest contains utilities to ease writing table-driven tests for pure functions and method.


func square(v int) int { return v * v }

func TestFunc(t *testing.T) {
	f := functest.New(square)

It can test whether things panic:

f := functest.New(condPanic)
	f.In(false, nil),
	f.In(true, "boom").Check(func(res functest.Result) error {
		if res.Panic != "boom" {
			return fmt.Errorf("panic = %v; want boom", res.Panic)
		return nil
	f.In(true, nil).Check(func(res functest.Result) error {
		if res.Panic != nil || res.Paniked {
			return fmt.Errorf("expected panic with nil value, got: %+v", res)
		return nil

If a test fails, functest does its best to format a useful error message. You can also name test cases:

f := functest.New(square)

Which would fail like:

--- FAIL: TestSquare (0.00s)
functest.go:304: square(1) = 1; want 111
functest.go:304: three: square(3) = 9; want 999



This section is empty.


This section is empty.


This section is empty.


type Case

type Case struct {
	// contains filtered or unexported fields

Case is a test case to run.

Test cases can be either named or unnamed, depending on how they're created. Naming cases is optional; all failures messages aim to have useful output and include the input to the function.

Unless the function's arity is zero, all cases should have their input set with In.

The case's expected output can be set with Want and/or Check.

func (*Case) Check

func (c *Case) Check(checker func(Result) error) *Case

Check adds a function to check the result of the case's function call. It is a low-level function when Want is insufficient. For instance, it allows checking whether a function panics. If no checker functions are registered, function panics are considered a test failure.

Check modifies and returns c. Callers my use both Want and Check, and may use Check multiple times.

func (*Case) In

func (c *Case) In(args ...interface{}) *Case

In sets the arguments of c used to call f.

func (*Case) Want

func (c *Case) Want(result ...interface{}) *Case

Want sets the expected result values of the test case. Want modifies and returns c. Callers my use both Want and Check.

type Func

type Func struct {
	// Name is the name of the function to use in error messages.
	// In most cases it is initialized by New, unless the function
	// being tested is an anonymous function.
	Name string
	// contains filtered or unexported fields

Func is a wrapper around a func to test. It must be created with New.

func New

func New(f interface{}) *Func

New wraps a function for testing. The provided value f must be a function or method.

func (*Func) Case

func (f *Func) Case(name string) *Case

Case returns a new named case. It should be modified before use.

func (*Func) In

func (f *Func) In(args ...interface{}) *Case

In returns a new unnamed test case. It will be identified by its arguments only.

func (*Func) Test

func (f *Func) Test(t testing.TB, cases ...*Case)

Test runs the provided test cases against f. If any test cases fail, t.Errorf is called.

type Result

type Result struct {
	// Result is the return value(s) of the function.
	Result []interface{}

	// Panic is the panic value of the function.
	Panic interface{}

	// Panicked is whether the function paniced.
	// It can be used to determine whether a function
	// called panic(nil).
	Panicked bool

Result is the result of a function call, for use with Check.

Source Files

Jump to

Keyboard shortcuts

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