assert

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2020 License: MIT Imports: 10 Imported by: 0

README

assert

Go GitHub tag (latest SemVer) GoDoc FOSSA Status Go Report Card Coverage Status

assert provides a set of assertion helpers for unit/bench testing in golang.

assert is inspired by these projects:

improvements
  • Can be used with both unit test and bench test.
  • Most of conventiional assertions:
    • Equal, NotEqual, EqualTrue, EqualFalse
    • Nil, NotNil
    • Error, NoError
    • PanicMatches: test for the function which might throw a panic
    • Match, NotMatch: compares a value with regexp test
  • Fresh coding in go 1.13~1.15 and later.
Short guide
package some_test

import (
	"github.com/hedzr/assert"
	"testing"
)

type Person struct {
	Name string
	Age  int
}

func TestEqual(t *testing.T) {
	expected := []*Person{{"Alec", 20}, {"Bob", 21}, {"Sally", 22}}
	actual := []*Person{{"Alex", 20}, {"Bob", 22}, {"Sally", 22}}
	assert.NotEqual(t, expected, actual)

	assert.Equal(t, actual, actual)
}

func TestEqualTrue(t *testing.T) {
	assert.EqualTrue(t, true)
	assert.EqualFalse(t, false)
}
LICENSE

MIT

Documentation

Overview

Package assert provides easy assertion for your golang test cases

Index

Constants

View Source
const (
	// AppName const
	AppName = "assert"
	// Version const
	Version = "0.1.3"
	// VersionInt const
	VersionInt = 0x000103
)

Variables

This section is empty.

Functions

func DiffValues

func DiffValues(a, b interface{}) string

DiffValues compares 'a' and 'b' and return its differences as a text string with terminal escaped sequences.

func DiffValuesDefault

func DiffValuesDefault(a, b interface{}) string

DiffValuesDefault compares 'a' and 'b' and return its differences as a text string with terminal escaped sequences.

func Equal

func Equal(t testing.TB, expect, actual interface{})

Equal validates that 'actual' is equal to 'expect' and throws an error with line number

func EqualFalse

func EqualFalse(t testing.TB, actual bool)

EqualFalse validates that 'actual' is false

func EqualSkip

func EqualSkip(t testing.TB, skip int, expect, actual interface{})

EqualSkip validates that 'actual' is equal to 'expect' and throws an error with line number but the skip variable tells EqualSkip how far back on the stack to report the error. This is a building block to creating your own more complex validation functions.

func EqualTrue

func EqualTrue(t testing.TB, actual bool)

EqualTrue validates that 'actual' is true

func Error

func Error(t testing.TB, err error)

Error asserts that a function returned an error (i.e. not `nil`).

  actualObj, err := SomeFunction()
  if assert.Error(t, err) {
	   assert.Equal(t, expectedError, err)
  }

func Match

func Match(t *testing.T, value string, regex interface{})

Match validates that value matches the regex, either string or *regex and throws an error with line number

func MatchSkip

func MatchSkip(t *testing.T, skip int, value string, regex interface{})

MatchSkip validates that value matches the regex, either string or *regex and throws an error with line number but the skip variable tells MatchRegexSkip how far back on the stack to report the error. This is a building block to creating your own more complex validation functions.

func Nil

func Nil(t testing.TB, value interface{})

Nil asserts that the specified object is nil.

assert.Nil(t, err)

func NilSkip

func NilSkip(t testing.TB, skip int, value interface{})

NilSkip asserts that the specified object is nil.

assert.NilSkip(t, err)

func NoError

func NoError(t testing.TB, err error)

NoError asserts that a function returned no error (i.e. `nil`).

  actualObj, err := SomeFunction()
  if assert.NoError(t, err) {
	   assert.Equal(t, expectedObj, actualObj)
  }

func NotEqual

func NotEqual(t *testing.T, expect, actual interface{})

NotEqual validates that val1 is not equal val2 and throws an error with line number

func NotEqualSkip

func NotEqualSkip(t *testing.T, skip int, expect, actual interface{})

NotEqualSkip validates that val1 is not equal to val2 and throws an error with line number but the skip variable tells NotEqualSkip how far back on the stack to report the error. This is a building block to creating your own more complex validation functions.

func NotMatch

func NotMatch(t *testing.T, value string, regex interface{})

NotMatch validates that value matches the regex, either string or *regex and throws an error with line number

func NotMatchSkip

func NotMatchSkip(t *testing.T, skip int, value string, regex interface{})

NotMatchSkip validates that value matches the regex, either string or *regex and throws an error with line number but the skip variable tells NotMatchRegexSkip how far back on the stack to report the error. This is a building block to creating your own more complex validation functions.

func NotNil

func NotNil(t testing.TB, value interface{})

NotNil asserts that the specified object is not nil.

assert.NotNil(t, err)

func NotNilSkip

func NotNilSkip(t testing.TB, skip int, value interface{})

NotNilSkip asserts that the specified object is not nil.

assert.NotNilSkip(t, 1, err)

func PanicMatches

func PanicMatches(t testing.TB, fn func(), matches interface{})

PanicMatches validates that the panic output of running fn matches the supplied string

func PanicMatchesSkip

func PanicMatchesSkip(t testing.TB, skip int, fn func(), matches interface{})

PanicMatchesSkip validates that the panic output of running fn matches the supplied string but the skip variable tells PanicMatchesSkip how far back on the stack to report the error. This is a building block to creating your own more complex validation functions.

Types

This section is empty.

Jump to

Keyboard shortcuts

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