https://github.com/mattmeyers/assert
Opinionated test assertions.
https://github.com/mattmeyers/assert
assertions generics go testing unit-testing
Last synced: 6 months ago
JSON representation
Opinionated test assertions.
- Host: GitHub
- URL: https://github.com/mattmeyers/assert
- Owner: mattmeyers
- License: mit
- Created: 2022-03-12T21:51:16.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-03-30T03:52:36.000Z (over 4 years ago)
- Last Synced: 2024-04-17T21:11:37.898Z (about 2 years ago)
- Topics: assertions, generics, go, testing, unit-testing
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# assert

`assert` is an opinionated test assertion libary. It is not intended to fulfill every use case and is not intended to be flexible/extendable.
## Installing
Because some of the assertions use type parameters, Go 1.18+ is required. This library can be installed with
```sh
go get -u github.com/mattmeyers/assert
```
## Usage
The assertions found within this library can replace any simple assertion logic normally found in tests. All assertions are marked as `t.Helper`s so stack traces will point to the appropriate line in the test. Additionally, all assertions are not fatal by default. To convert an assertion to a fatal assertion, the `*testing.T` struct can be wrapped with `Fatal()`.
For example, consider the following function that returns a stringified JSON array.
```go
// In foo.go
package foo
func getJSON() string {
return "[1,2,3,4,5]"
}
```
Then this test will assert that the JSON contains a certain value.
```go
// In foo_test.go
package foo
import (
"encoding/json"
"testing"
"github.com/mattmeyers/assert"
)
func TestGetJSONContains3(t *testing.T) {
str := getJSON()
var values []int
err := json.Unmarshal([]byte(str), &values)
// Will fatally fail test if JSON is malformed
assert.NoError(assert.Fatal(t), err)
assert.SliceContains(t, values, 3)
// Will fail test
// assert.SliceContains(t, values, 9)}
}
```