assert

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2020 License: LGPL-3.0 Imports: 4 Imported by: 0

README

assert

Assertions for unit tests in Golang, similar to JUnit.

Usage

In case you use "testing", you can directly call the methods. In case you use a different framework than "testing", you need to reassign the Fail function pointer. Due to a lack of generic exceptions, the assertion methods require a t *testing.T passed. The assertion methods need this to call t.Fail(). Therefore, the possibility of reuse of the assert module for other frameworks is limited.

Differences from JUnit

You cannot make any assumptions whether an assertion stops execution of the current test case. In the default implementation, assertions use t.Fail(), by calling t.Error(). In go testing, this will mark the test case as failed, but the test case still continues. If you map Fail to something else, you may make a test stop instead.

Supported Frameworks

Out of the box, assert supports the following frameworks:

  • "testing"
  • "github.com/DATA-DOG/godog"

assert with "testing"

To use assert with "testing", pass t *testing.T as first argument to the assertion.

Example:

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

func TestExample(t *testing.T) {
	assert.Equals(t, 23, 42)
}

assert with "godog"

To use assert with "godog", return the error returned by the assertion.

Example:

import (
	"github.com/christianhujer/assert"
)

func iMUSTHaveHotdogs(expectedHotdogsRemaining int) error {
	return assert.Equals(nil, expectedHotdogsRemaining, hotdogs)
}

Documentation

Overview

Package assert provides JUnit-style assertions for go testing.

Index

Constants

This section is empty.

Variables

View Source
var Fail = func(target interface{}, err error) error {
	if t, _ := target.(*testing.T); t != nil {
		t.Error(err)
	}
	return err
}

Fail is a pointer to a function that fails a test. It takes two arguments, the target and the error. The default implementation assumes that the target is of type *testing.T and invokes its Error() function. Reassign this function pointer if you are working with a framework other than "testing".

Functions

func DeepEquals

func DeepEquals(target interface{}, expected, actual interface{}) error

DeepEquals asserts that two arguments are equal, performing a deep comparison. The first argument target is to be forwarded to Fail(target interface{}, msg string) in case the assertion fails. In case of "testing", pass t *testing.T as the first argument. Implemented using reflect.DeepEqual

func Equals

func Equals(target interface{}, expected, actual interface{}) error

Equals asserts that two arguments are equal. The first argument target is to be forwarded to Fail(target interface{}, msg string) in case the assertion fails. In case of "testing", pass t *testing.T as the first argument. Currently, it supports the following types of arguments: - []byte - Everything that supports ==/!=

func False

func False(target interface{}, condition bool) error

False asserts that a condition is false. The first argument target is to be forwarded to Fail(target interface{}, msg string) in case the assertion fails. In case of "testing", pass t *testing.T as the first argument.

func Nil

func Nil(target interface{}, pointer interface{}) error

Nil asserts that a pointer is nil. The first argument target is to be forwarded to Fail(target interface{}, msg string) in case the assertion fails. In case of "testing", pass t *testing.T as the first argument.

func NotDeepEquals

func NotDeepEquals(target interface{}, expected, actual interface{}) error

NotDeepEquals asserts that two arguments are not equal, performing a deep comparison. The first argument target is to be forwarded to Fail(target interface{}, msg string) in case the assertion fails. In case of "testing", pass t *testing.T as the first argument. Implemented using reflect.DeepEqual

func NotEquals

func NotEquals(target interface{}, expected, actual interface{}) error

NotEquals asserts that two arguments are not equal. The first argument target is to be forwarded to Fail(target interface{}, msg string) in case the assertion fails. In case of "testing", pass t *testing.T as the first argument. Currently, it supports the following types of arguments: - []byte - Everything that supports ==/!=

func NotNil

func NotNil(target interface{}, pointer interface{}) error

NotNil asserts that a pointer is not nil. The first argument target is to be forwarded to Fail(target interface{}, msg string) in case the assertion fails. In case of "testing", pass t *testing.T as the first argument.

func True

func True(target interface{}, condition bool) error

True asserts that a condition is true. The first argument target is to be forwarded to Fail(target interface{}, msg string) in case the assertion fails. In case of "testing", pass t *testing.T as the first argument.

Types

This section is empty.

Jump to

Keyboard shortcuts

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