errassert

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 25, 2024 License: MIT Imports: 3 Imported by: 0

README

errassert: Simple table driven Go tests error assertions

go reference license CI coverage go report

Overview

  • set error assertion in a table driven test declaration
  • provides similar experience as Testify's ErrorAssertionFunc
  • define custom error assertions
  • combine assertions with errassert.Want
  • support assertions for gRPC status errors

Example

func Test(t *testing.T) {
	type testCase struct {
		in        string
		want      int
		errassert errassert.ErrorAssertion
	}

	run := func(t *testing.T, tc testCase) {
		_, err := strconv.Atoi(tc.in)

		tc.errassert.Require(t, err)
	}

	testCases := map[string]testCase{
		"ok": {
			in:        "42",
			want:      42,
			errassert: errassert.NilError(),
		},
		"invalid input fails": {
			in:        "invalid input",
			errassert: errassert.SomeError(),
		},
		"empty input cause invalid syntax error": {
			in:        "",
			// Common basic assertions.
			errassert: errassert.ErrorEndsWith("invalid syntax"),
		},
		"invalid input fails with input": {
			in: "input",
			// Combine basic assertions.
			errassert: errassert.Want(
				errassert.ErrorContains("\"input\""),
				errassert.ErrorEndsWith("invalid syntax"),
			),
		},
	}

	for name, tc := range testCases {
		t.Run(name, func(t *testing.T) { run(t, tc) })
	}
}


Credits

Based on experience within @h2oai.

Documentation

Overview

Package errassert provides set of simple error assertions for table driven tests.

Example
package main

import (
	"strconv"
	"testing"

	"github.com/zoido/errassert"
)

func main() {
	t := testing.T{} // Provided by the testing package.

	type testCase struct {
		in        string
		errassert errassert.ErrorAssertion
	}

	run := func(t *testing.T, tc testCase) {
		_, err := strconv.Atoi(tc.in)

		tc.errassert.Require(t, err)
	}

	testCases := map[string]testCase{
		"ok": {
			in:        "42",
			errassert: errassert.NilError(),
		},
		"invalid input fails": {
			in:        "invalid",
			errassert: errassert.SomeError(),
		},
		"empty input fails": {
			in:        "",
			errassert: errassert.ErrorEndsWith("invalid syntax"),
		},
	}

	for name, tc := range testCases {
		t.Run(name, func(t *testing.T) { run(t, tc) })
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorAssertion

type ErrorAssertion func(error) error

ErrorAssertion represents a single instance of the error assertion. If the error is not as expected, call returns an error.

Zero value (nil) is valid and Assert nor Require will never fail. It can be looked at as "I don't care about the error" assertion.

Example (Custom)
package main

import (
	"errors"
	"fmt"
	"strconv"
	"testing"

	"github.com/zoido/errassert"
)

func main() {
	t := testing.T{} // Provided by the testing package.

	type testCase struct {
		in        string
		errassert errassert.ErrorAssertion
	}

	run := func(t *testing.T, tc testCase) {
		_, err := strconv.Atoi(tc.in)

		tc.errassert.Require(t, err)
	}

	testCases := map[string]testCase{
		"empty input fails": {
			in: "very specific error input",
			errassert: func(err error) error {
				if err == nil {
					return errors.New("expected error, got nil")
				}
				if err.Error() != "very specific error" {
					return fmt.Errorf("expected very specific error, got: '%v'", err.Error())
				}
				return nil
			},
		},
	}

	for name, tc := range testCases {
		t.Run(name, func(t *testing.T) { run(t, tc) })
	}
}
Output:

func Error added in v0.4.0

func Error(msg string) ErrorAssertion

Error returns an assertion that checks if the error is equal to the error with the given message.

func ErrorAs

func ErrorAs(target interface{}) ErrorAssertion

ErrorAs returns an assertion that checks if the error passes errors.As check.

func ErrorContains

func ErrorContains(substring string) ErrorAssertion

ErrorContains returns an assertion that checks if the error contains the given substring.

func ErrorEndsWith

func ErrorEndsWith(suffix string) ErrorAssertion

ErrorEndsWith returns an assertion that checks if the error ends with the given suffix.

func ErrorIs

func ErrorIs(expected error) ErrorAssertion

ErrorIs returns an assertion that checks if the error passes errors.Is check.

func ErrorStartsWith

func ErrorStartsWith(prefix string) ErrorAssertion

ErrorStartsWith returns an assertion that checks if the error starts with the given prefix.

func NilError

func NilError() ErrorAssertion

NilError returns an assertion that checks if the error is nil.

func SomeError

func SomeError() ErrorAssertion

SomeError returns an assertion that checks if the error is not nil.

func Want

func Want(assertions ...ErrorAssertion) ErrorAssertion

Want combines multiple error assertions into a single assertion. All assertions must pass for the combined assertion to pass. If any of the assertions fails, the combined assertion fails and returns the first error encountered.

Example
package main

import (
	"strconv"
	"testing"

	"github.com/zoido/errassert"
)

func main() {
	t := testing.T{} // Provided by the testing package.

	type testCase struct {
		in        string
		errassert errassert.ErrorAssertion
	}

	run := func(t *testing.T, tc testCase) {
		_, err := strconv.Atoi(tc.in)

		tc.errassert.Require(t, err)
	}

	testCases := map[string]testCase{
		"ok": {
			in:        "42",
			errassert: errassert.NilError(),
		},
		"invalid input": {
			in: "input",
			errassert: errassert.Want(
				errassert.ErrorContains("\"input\""),
				errassert.ErrorEndsWith("invalid syntax"),
			),
		},
	}

	for name, tc := range testCases {
		t.Run(name, func(t *testing.T) { run(t, tc) })
	}
}
Output:

func (ErrorAssertion) Assert

func (assertion ErrorAssertion) Assert(t TestingT, err error)

Assert checks if the error is as expected and calls t.Fail if not.

func (ErrorAssertion) Require

func (assertion ErrorAssertion) Require(t TestingT, err error)

Require checks if the error is as expected and calls t.FailNow if not.

type TestingT

type TestingT interface {
	Log(...interface{})
	Fail()
	FailNow()
	Helper()
}

TestingT defines the subset of testing.TB methods used by errassert.

Directories

Path Synopsis
Package grpcerrassert provides assertions for gRPC status errors.
Package grpcerrassert provides assertions for gRPC status errors.

Jump to

Keyboard shortcuts

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