quicktest

package module
v0.0.0-...-5af72c3 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2017 License: MIT Imports: 14 Imported by: 0

README

GoDoc Build Status

quicktest

A collection of Go helpers for writing tests.

Installation

To install the package, run go get github.com/frankban/quicktest.

Usage

Quicktest helpers can be easily integrated inside regular Go tests, for instance:

import qt "github.com/frankban/quicktest"

func TestFoo(t *testing.T) {
    t.Run("numbers", func(t *testing.T) {
        c := qt.New(t)
        numbers, err := somepackage.Numbers()
        c.Assert(numbers, qt.DeepEquals, []int{42, 47})
        c.Assert(err, qt.ErrorMatches, "bad wolf")
    })
    t.Run("nil", func(t *testing.T) {
        c := qt.New(t)
        got := somepackage.MaybeNil()
        c.Assert(got, qt.IsNil, qt.Commentf("value: %v", somepackage.Value))
    })
}

The library provides some base checkers like Equals, DeepEquals, Matches, ErrorMatches, IsNil and others. More can be added by implementing the Checker interface.

See the go documentation for this library.

Documentation

Overview

Package quicktest provides a collection of Go helpers for writing tests.

Quicktest helpers can be easily integrated inside regular Go tests, for instance:

import qt "github.com/frankban/quicktest"

func TestFoo(t *testing.T) {
    t.Run("numbers", func(t *testing.T) {
        c := qt.New(t)
        numbers, err := somepackage.Numbers()
        c.Assert(numbers, qt.DeepEquals, []int{42, 47})
        c.Assert(err, qt.ErrorMatches, "bad wolf")
    })
    t.Run("nil", func(t *testing.T) {
        c := qt.New(t)
        got := somepackage.MaybeNil()
        c.Assert(got, qt.IsNil, qt.Commentf("value: %v", somepackage.Value))
    })
}

The library provides some base checkers like Equals, DeepEquals, Matches, ErrorMatches, IsNil and others. More can be added by implementing the Checker interface.

Index

Constants

This section is empty.

Variables

View Source
var DeepEquals = CmpEquals()

DeepEquals is a Checker deeply checking equality of two arbitrary values. For instance:

c.Assert(got, qt.DeepEquals, []int{42, 47})

Functions

func BadCheckf

func BadCheckf(format string, a ...interface{}) error

BadCheckf returns an error used to report a problem with the checker invocation or testing execution itself (like wrong number or type of arguments) rather than a real Check or Assert failure. This helper can be used when implementing checkers.

func IsBadCheck

func IsBadCheck(err error) bool

IsBadCheck reports whether the given error has been created by BadCheckf. This helper can be used when implementing checkers.

Types

type C

type C struct {
	testing.TB
}

C is a quicktest checker. It embeds a testing.TB value and provides additional checking functionality. If an Assert or Check operation fails, it uses the wrapped TB value to fail the test appropriately.

func New

func New(t testing.TB) *C

New returns a new checker instance that uses t to fail the test when checks fail. It only ever calls the Fatal, Error and (when available) Run methods of t. For instance.

func TestFoo(t *testing.T) {
    t.Run("A=42", func(t *testing.T) {
        c := qt.New(t)
        c.Assert(a, qt.Equals, 42)
    })
}

The library already provides some base checkers, and more can be added by implementing the Checker interface.

func (*C) Assert

func (c *C) Assert(got interface{}, checker Checker, args ...interface{}) bool

Assert runs the given check and stops execution in case of failure. For instance:

c.Assert(got, qt.DeepEquals, []int{42, 47})
c.Assert(got, qt.ErrorMatches, "bad wolf .*", qt.Commentf("a comment"))

Additional args (not consumed by the checker), when provided, are included as comments in the failure output when the check fails.

func (*C) Check

func (c *C) Check(got interface{}, checker Checker, args ...interface{}) bool

Check runs the given check and continues execution in case of failure. For instance:

c.Check(answer, qt.Equals, 42)
c.Check(got, qt.IsNil, qt.Commentf("iteration %d", i))

Additional args (not consumed by the checker), when provided, are included as comments in the failure output when the check fails.

func (*C) Run

func (c *C) Run(name string, f func(c *C)) bool

Run runs f as a subtest of t called name. It's a wrapper around *testing.T.Run that provides the quicktest checker to f. For instance:

func TestFoo(t *testing.T) {
    c := qt.New(t)
    c.Run("A=42", func(c *qt.C) {
        // This assertion only stops the current subtest.
        c.Assert(a, qt.Equals, 42)
    })
}

A panic is raised when Run is called and the embedded concrete type does not implement Run, for instance if TB's concrete type is a benchmark.

type Checker

type Checker interface {
	// Check performs the check and returns an error in the case it fails.
	// The check is performed using the provided got argument and any
	// additional args required.
	Check(got interface{}, args []interface{}) error
	// Negate negates the check performed by Check. In essence, it checks that
	// the opposite is true. For instance, if Check ensures that two values are
	// equal, Negate ensures that they are not equal.
	Negate(got interface{}, args []interface{}) error
	// NumArgs returns the number of additional arguments (excluding got)
	// expected to be provided to Check and Negate.
	NumArgs() int
}

Checker is implemented by types used as part of Check/Assert invocations.

var Equals Checker = &equalsChecker{
	numArgs: 1,
}

Equals is a Checker checking equality of two comparable values. For instance:

c.Assert(answer, qt.Equals, 42)

Note that the following will fail:

c.Assert((*sometype)(nil), qt.Equals, nil)

Use the IsNil checker below for this kind of nil check.

var ErrorMatches Checker = &errorMatchesChecker{
	numArgs: 1,
}

ErrorMatches is a Checker checking that the provided value is an error whose message matches the provided regular expression pattern. For instance:

c.Assert(err, qt.ErrorMatches, "bad wolf .*")
var HasLen Checker = &hasLenChecker{
	numArgs: 1,
}

HasLen is a Checker checking that the provided value has the provided length. For instance:

c.Assert([]int{42, 47}, qt.HasLen, 2)
c.Assert(myMap, qt.HasLen, 42)
var IsNil Checker = &isNilChecker{}

IsNil is a Checker checking that the provided value is nil. For instance:

c.Assert(got, qt.IsNil)
var Matches Checker = &matchesChecker{
	numArgs: 1,
}

Matches is a Checker checking that the provided string, or the string representation of the provided value, matches the provided regular expression pattern. For instance:

c.Assert("these are the voyages", qt.Matches, "these are .*")
c.Assert(net.ParseIP("1.2.3.4"), qt.Matches, "1.*")
var PanicMatches Checker = &panicMatchesChecker{
	numArgs: 1,
}

PanicMatches is a Checker checking that the provided function panics with a message matching the provided regular expression pattern. For instance:

c.Assert(func() {panic("bad wolf ...")}, qt.PanicMatches, "bad wolf .*")

func CmpEquals

func CmpEquals(opts ...cmp.Option) Checker

CmpEquals returns a Checker checking equality of two arbitrary values according to the provided compare options. See DeepEquals as an example of such a checker, commonly used when no compare options are required. For instance:

c.Assert(list, qt.CmpEquals(cmpopts.SortSlices), []int{42, 47})
c.Assert(got, qt.CmpEquals(), []int{42, 47}) // Same as qt.DeepEquals.

func Not

func Not(checker Checker) Checker

Not returns a Checker negating the given Checker. For instance:

c.Assert(got, qt.Not(qt.IsNil))
c.Assert(answer, qt.Not(qt.Equals), 42)

type Comment

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

Comment represents additional information on a check or an assertion which is displayed when the check or assertion fails.

func Commentf

func Commentf(format string, args ...interface{}) Comment

Commentf returns a test comment whose output is formatted according to the given format specifier and args. It may be provided as the last argument to any check or assertion and will be displayed if the check or assertion fails. For instance:

c.Assert(a, qt.Equals, 42, qt.Commentf("answer is not %d", 42))

func (Comment) String

func (c Comment) String() string

String outputs a string formatted according to the stored format specifier and args.

Jump to

Keyboard shortcuts

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