assert

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2023 License: MIT Imports: 4 Imported by: 0

README

Assertion for Golang

test codecov Version Badge License Badge Go Reference

A collection of Golang assertion functions for verifying invariants.

Installation

To install this library, just use go get command like the following line:

go get -u github.com/ghosind/go-assert

Getting Started

This library provided assertion functions to verify the equality of values, or assert for nil:

func TestExample(t *testing.T) {
  // assert equality
  assert.DeepEqual(t, actual, expect)

  // assert inequality
  assert.NotDeepEqual(t, actual, expect)

  // assert for nil
  assert.Nil(t, object)

  // assert for not nil
  assert.NotNil(t, object)
}

It also provided assertion functions to verify a function will panic or not:

func TestPanic(t *testing.T) {
  // assert panic
  assert.Panic(t, func () {
    // do something

    panic()
  })

  // assert no panic
  assert.NotPanic(t, func () {
    // do something

    // panic()
  })
}

Every assertion will not terminate the testing workflow. However, they'll return an error if the verification failed, and you can check the return value to get the verification result.

func TestExample(t *testing.T) {
  if err := assert.DeepEqual(t, actual, expect); err != nil {
    // terminate test
    t.Fail()
  }
}

If you need to assert many times, you can also create an Assertion instance:

func TestExample(t *testing.T) {
  assertion := assert.New(t)

  // test equality
  assertion.DeepEqual(actual, expect)

  // Test inequality
  assertion.NotDeepEqual(actual, expect)
}

License

This project was published under the MIT license, you can see LICENSE file to get more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepEqual

func DeepEqual(t *testing.T, actual, expect any, message ...string) error

DeepEqual tests deeply equality between actual and expect parameters.

func Nil added in v0.1.1

func Nil(t *testing.T, val any, message ...string) error

Nil tests whether a value is nil or not, and it'll fail when the value is not nil. It will always return false if the value is a bool, an integer, a floating number, a complex, or a string.

func NotDeepEqual

func NotDeepEqual(t *testing.T, actual, expect any, message ...string) error

NotDeepEqual tests deeply inequality between actual and expected parameters.

func NotNil added in v0.1.1

func NotNil(t *testing.T, val any, message ...string) error

NotNil tests whether a value is nil or not, and it'll fail when the value is nil. It will always return true if the value is a bool, an integer, a floating number, a complex, or a string.

func NotPanic

func NotPanic(t *testing.T, fn func(), message ...string) (err error)

NotPanic asserts that the function fn does not panic.

func Panic

func Panic(t *testing.T, fn func(), message ...string) (err error)

Panic expects the function fn to panic.

Types

type Assertion

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

func New

func New(t *testing.T) *Assertion

New returns an assertion instance for verifying invariants.

func (*Assertion) Cleanup added in v0.1.1

func (a *Assertion) Cleanup(f func())

Cleanup registers a function to be called when the test (or subtest) and all its subtests complete. Cleanup functions will be called in last added, first called order.

func (*Assertion) Deadline added in v0.1.1

func (a *Assertion) Deadline() (deadline time.Time, ok bool)

Deadline reports the time at which the test binary will have exceeded the timeout specified by the -timeout flag.

The ok result is false if the -timeout flag indicates “no timeout” (0).

func (*Assertion) DeepEqual

func (a *Assertion) DeepEqual(actual, expect any, message ...string) error

DeepEqual tests deeply equality between actual and expect parameters.

func (*Assertion) Error added in v0.1.1

func (a *Assertion) Error(args ...any)

Error is equivalent to Log followed by Fail.

func (*Assertion) Errorf added in v0.1.1

func (a *Assertion) Errorf(format string, args ...any)

Errorf is equivalent to Logf followed by Fail.

func (*Assertion) Fail added in v0.1.1

func (a *Assertion) Fail()

Fail marks the function as having failed but continues execution.

func (*Assertion) FailNow added in v0.1.1

func (a *Assertion) FailNow()

FailNow marks the function as having failed and stops its execution by calling runtime.Goexit (which then runs all deferred calls in the current goroutine). Execution will continue at the next test or benchmark. FailNow must be called from the goroutine running the test or benchmark function, not from other goroutines created during the test. Calling FailNow does not stop those other goroutines.

func (*Assertion) Failed added in v0.1.1

func (a *Assertion) Failed() bool

Failed reports whether the function has failed.

func (*Assertion) Fatal added in v0.1.1

func (a *Assertion) Fatal(args ...any)

Fatal is equivalent to Log followed by FailNow.

func (*Assertion) Fatalf added in v0.1.1

func (a *Assertion) Fatalf(format string, args ...any)

Fatalf is equivalent to Logf followed by FailNow.

func (*Assertion) Helper added in v0.1.1

func (a *Assertion) Helper()

Helper marks the calling function as a test helper function. When printing file and line information, that function will be skipped. Helper may be called simultaneously from multiple goroutines.

func (*Assertion) Log added in v0.1.1

func (a *Assertion) Log(args ...any)

Log formats its arguments using default formatting, analogous to Println, and records the text in the error log. For tests, the text will be printed only if the test fails or the -test.v flag is set. For benchmarks, the text is always printed to avoid having performance depend on the value of the -test.v flag.

func (*Assertion) Logf added in v0.1.1

func (a *Assertion) Logf(format string, args ...any)

Logf formats its arguments according to the format, analogous to Printf, and records the text in the error log. A final newline is added if not provided. For tests, the text will be printed only if the test fails or the -test.v flag is set. For benchmarks, the text is always printed to avoid having performance depend on the value of the -test.v flag.

func (*Assertion) Name added in v0.1.1

func (a *Assertion) Name() string

Name returns the name of the running (sub-) test or benchmark.

The name will include the name of the test along with the names of any nested sub-tests. If two sibling sub-tests have the same name, Name will append a suffix to guarantee the returned name is unique.

func (*Assertion) Nil added in v0.1.1

func (a *Assertion) Nil(val any, message ...string) error

Nil tests whether a value is nil or not, and it'll fail when the value is not nil. It will always return false if the value is a bool, an integer, a floating number, a complex, or a string.

func (*Assertion) NotDeepEqual

func (a *Assertion) NotDeepEqual(actual, expect any, message ...string) error

NotDeepEqual tests deeply inequality between actual and expected parameters.

func (*Assertion) NotNil added in v0.1.1

func (a *Assertion) NotNil(val any, message ...string) error

NotNil tests whether a value is nil or not, and it'll fail when the value is nil. It will always return true if the value is a bool, an integer, a floating number, a complex, or a string.

func (*Assertion) NotPanic

func (a *Assertion) NotPanic(fn func(), message ...string) (err error)

NotPanic asserts that the function fn does not panic.

func (*Assertion) Panic

func (a *Assertion) Panic(fn func(), message ...string) (err error)

Panic expects the function fn to panic.

func (*Assertion) Parallel added in v0.1.1

func (a *Assertion) Parallel()

Parallel signals that this test is to be run in parallel with (and only with) other parallel tests. When a test is run multiple times due to use of -test.count or -test.cpu, multiple instances of a single test never run in parallel with each other.

func (*Assertion) Run added in v0.1.1

func (a *Assertion) Run(name string, f func(*testing.T)) bool

Run runs f as a subtest of t called name. It runs f in a separate goroutine and blocks until f returns or calls t.Parallel to become a parallel test. Run reports whether f succeeded (or at least did not fail before calling t.Parallel).

Run may be called simultaneously from multiple goroutines, but all such calls must return before the outer test function for t returns.

func (*Assertion) Setenv added in v0.1.1

func (a *Assertion) Setenv(key, value string)

Setenv calls os.Setenv(key, value) and uses Cleanup to restore the environment variable to its original value after the test.

Because Setenv affects the whole process, it cannot be used in parallel tests or tests with parallel ancestors.

func (*Assertion) Skip added in v0.1.1

func (a *Assertion) Skip(args ...any)

Skip is equivalent to Log followed by SkipNow.

func (*Assertion) SkipNow added in v0.1.1

func (a *Assertion) SkipNow()

SkipNow marks the test as having been skipped and stops its execution by calling runtime.Goexit. If a test fails (see Error, Errorf, Fail) and is then skipped, it is still considered to have failed. Execution will continue at the next test or benchmark. See also FailNow. SkipNow must be called from the goroutine running the test, not from other goroutines created during the test. Calling SkipNow does not stop those other goroutines.

func (*Assertion) Skipf added in v0.1.1

func (a *Assertion) Skipf(format string, args ...any)

Skipf is equivalent to Logf followed by SkipNow.

func (*Assertion) Skipped added in v0.1.1

func (a *Assertion) Skipped() bool

Skipped reports whether the test was skipped.

func (*Assertion) TempDir added in v0.1.1

func (a *Assertion) TempDir() string

TempDir returns a temporary directory for the test to use. The directory is automatically removed by Cleanup when the test and all its subtests complete. Each subsequent call to t.TempDir returns a unique directory; if the directory creation fails, TempDir terminates the test by calling Fatal.

type AssertionError

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

AssertionError indicates the failure of an assertion.

func (AssertionError) Error

func (err AssertionError) Error() string

Error returns the message of the error.

Jump to

Keyboard shortcuts

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