Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/zoido/errassert
- Owner: zoido
- License: mit
- Created: 2024-03-12T15:52:10.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-05-25T18:54:08.000Z (6 months ago)
- Last Synced: 2024-06-21T04:56:33.282Z (5 months ago)
- Topics: assert, assertions, go, golang, table-driven-test, test, testing, testing-tools
- Language: Go
- Homepage:
- Size: 115 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
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).