Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zoido/errassert

Simple table driven Go tests error assertions
https://github.com/zoido/errassert

assert assertions go golang table-driven-test test testing testing-tools

Last synced: 9 days ago
JSON representation

Simple table driven Go tests error assertions

Awesome Lists containing this project

README

        

# `errassert`: Simple table driven Go tests error assertions

[![go reference](https://pkg.go.dev/badge/github.com/zoido/errassert)](https://pkg.go.dev/github.com/zoido/errassert)
[![license](https://img.shields.io/github/license/zoido/errassert?style=flat-square)](https://github.com/zoido/errassert/blob/master/LICENSE)
![CI](https://img.shields.io/github/actions/workflow/status/zoido/errassert/go.yaml?style=flat-square&logoColor=white&logo=github)
[![coverage](https://img.shields.io/codecov/c/github/zoido/errassert?style=flat-square&logoColor=white&logo=codecov)](https://codecov.io/gh/zoido/errassert)
[![go report](https://goreportcard.com/badge/github.com/zoido/errassert?style=flat-square)](https://goreportcard.com/report/github.com/zoido/errassert)

## Overview

- set error assertion in a table driven test declaration
- provides similar experience as Testify's [`ErrorAssertionFunc`](https://pkg.go.dev/github.com/stretchr/testify/assert#ErrorAssertionFunc)
- define custom error assertions
- combine assertions with `errassert.Want`
- support assertions for gRPC status errors

## Example

```go
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](https://github.com/h2oai).