is Testing Framework

repository·master·Indexed 24 days ago

https://github.com/matryer/is

A lightweight testing mini-framework for Go providing a simple API for common assertions. It features strict and relaxed modes via is.New and is.NewRelaxed, and includes methods for equality checks (Equal), boolean assertions (True), and error validation (NoErr).

Tokens
831
Snippets
7
Records
8
Agent score
34%

What's inside is

  1. Initialize the `is` testing helper

    master

    To use is, you must first create a new instance of the testing helper using is.New(t) or is.NewRelaxed(t). You should always start your tests with this initialization.

    There are two modes of operation:

    1. Strict Mode (is.New): When an assertion fails, it calls t.FailNow(), which immediately aborts the current test. This is the recommended mode for most tests.
    2. Relaxed Mode (is.NewRelaxed): When an assertion fails, it calls t.Fail(), allowing the test to continue executing. This is useful if you want to report multiple failures within a single test function.
    func Test(t *testing.T) {
    	// always start tests with this
    	is := is.New(t)
    
    	// ... assertions ...
    }
  2. Configure color output for `is`

    master

    The is package supports disabling colors. This can be controlled via:

    1. The -nocolor command-line flag.
    2. The NO_COLOR environment variable (following the no-color.org standard).
    3. The IS_NO_COLOR environment variable (set to a boolean value like true or false).
  3. Manually trigger a failure with `Fail()`

    master

    The Fail method immediately reports a failure.

    • In Strict Mode (is.New), the test will abort.
    • In Relaxed Mode (is.NewRelaxed), the test will continue executing but will still be marked as failed.
    func (is *I) Fail()
  4. Assert equality with `Equal(a, b)`

    master

    The Equal method asserts that two values a and b are equal. It uses reflect.DeepEqual for comparison. If the assertion fails, it outputs the values and their types. If one of the values is nil, it specifically reports <nil>.

    Note on Comments: You can add a description to your failure output by adding a Go comment on the same line as the assertion.

    func (is *I) Equal(a, b interface{})
  5. Use `is` in subtests

    master

    When using Go's t.Run for subtests, you should create a new is instance inside the subtest function to ensure failures are correctly scoped to that subtest.

    func Test(t *testing.T) {
    	is := is.New(t)
    	t.Run("sub", func(t *testing.T) {
    		is := is.New(t)
    		// test logic here
    	})
    }
  6. Assert no error with `NoErr(err)`

    master

    The NoErr method asserts that the provided error is nil. If an error is present, it logs the error message prefixed with err: .

    func (is *I) NoErr(err error)
  7. Assert a boolean condition with `True(expression)`

    master

    The True method asserts that the provided boolean expression is true. If the expression is false, it reports not true: $ARGS, where $ARGS is expanded to the actual arguments used in the call.

    func (is *I) True(expression bool)
  8. The `T` interface for reporting failures

    master

    The is package requires an object that implements the T interface to report failures. The standard *testing.T from the Go library implements this interface.

    type T interface {
    	// Fail indicates that the test has failed but
    	// allowed execution to continue.
    	Fail()
    	// FailNow indicates that the test has failed and
    	// aborts the test.
    	FailNow()
    }