assert

package module
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2016 License: Apache-2.0 Imports: 3 Imported by: 0

README

assert

Build Status GoDoc Go Report Card

assert is Go package that provides convenience methods for writing assertions in standard Go tests.

You can write this test with assert:

func TestSomething(t *testing.T) {
  i, err := doSomething()
  assert.NoErr(t, err)
  assert.Equal(t, i, 123, "returned integer")
}

Instead of writing this test with only the standard testing library:

func TestSomething(t *testing.T) {
  i, err := doSomething()
  if err != nil {
    t.Fatalf("error encountered: %s", err)
  }
  if i != 123 {
    t.Fatalf("returned integer was %d, not %d", i, 123)
  }
}

Documentation

Overview

Package assert provides convenience assert methods to complement the built in go testing library. It's intended to add onto standard Go tests. Example usage:

func TestSomething(t *testing.T) {
	i, err := doSomething()
	assert.NoErr(t, err)
	assert.Equal(t, i, 123, "returned integer")
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal(t Tester, actual, expected interface{}, noun string)

Equal ensures that the actual value returned from a test was equal to an expected. it uses reflect.DeepEqual to do so. name is used to describe the values being compared. it's used in the error string if actual != expected.

func Err

func Err(t Tester, expected error, actual error)

Err calls t.Fatalf if expected is not equal to actual. it uses reflect.DeepEqual to determine if the errors are equal

func ExistsErr

func ExistsErr(t Tester, err error, noun string)

ExistsErr calls t.Fatalf if err == nil. The message will explain that the error described by noun was nil when it shouldn't have been

func False

func False(t Tester, b bool, fmtStr string, vals ...interface{})

False is the equivalent of True(t, !b, fmtStr, vals...).

func Nil

func Nil(t Tester, i interface{}, noun string)

Nil uses reflect.DeepEqual(i, nil) to determine if i is nil. if it's not, Nil calls t.Fatalf explaining that the noun i is not nil when it should have been

func NoErr

func NoErr(t Tester, e error)

NoErr calls t.Fatalf if e is not nil.

func NotNil

func NotNil(t Tester, i interface{}, noun string)

NotNil uses reflect.DeepEqual(i, nil) to determine if i is nil. if it is, NotNil calls t.Fatalf explaining that the noun i is nil when it shouldn't have been.

func True

func True(t Tester, b bool, fmtStr string, vals ...interface{})

True fails the test if b is false. on failure, it calls t.Fatalf(fmtStr, vals...)

Types

type Equaler

type Equaler interface {
	Equal(Equaler) bool
}

Equaler determines if a type is equal to any other type that conforms to Equaler. All types passed to assert.Equal are checked to see if they conform to this interface, and if they do, their Equal function is called to check for their equality. This call is made instead of the call to reflect.DeepEqual

type Tester

type Tester interface {
	Fatalf(string, ...interface{})
}

Tester is a stub interface that *testing.T conforms to. It is used in all exported function calls in this assert library so that the library can be tested, or a caller can use a custom testing library. As said before, however, the most widely used implementation of this interface will be *testing.T. Example usage:

func TestSomething(t *testing.T) {
	assert.Equal(t, "something", "something", "something")
}

func WithFrameWrapper

func WithFrameWrapper(t Tester) Tester

WithFrameWrapper returns the original Tester, wrapped by a frameWrapper that adds context about how many frames to backtrack on the call stack when identifying the source of a failed assertion. If the Tester passed in is already a frameWrapper, the Tester wrapped by that frameWrapper is unwrapped and re-wrapped with updated context.

Directories

Path Synopsis
Package examples shows example code that illustrates how to use the assert package
Package examples shows example code that illustrates how to use the assert package

Jump to

Keyboard shortcuts

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