| 1 |
# qt: quicker Go tests |
| 2 |
|
| 3 |
`go get github.com/go-quicktest/qt` |
| 4 |
|
| 5 |
Package qt provides a collection of Go helpers for writing tests. It uses generics, |
| 6 |
so requires Go 1.18 at least. |
| 7 |
|
| 8 |
For a complete API reference, see the [package documentation](https://pkg.go.dev/github.com/go-quicktest/qt). |
| 9 |
|
| 10 |
Quicktest helpers can be easily integrated inside regular Go tests, for |
| 11 |
instance: |
| 12 |
```go |
| 13 |
import "github.com/go-quicktest/qt" |
| 14 |
|
| 15 |
func TestFoo(t *testing.T) { |
| 16 |
t.Run("numbers", func(t *testing.T) { |
| 17 |
numbers, err := somepackage.Numbers() |
| 18 |
qt.Assert(t, qt.DeepEquals(numbers, []int{42, 47}) |
| 19 |
qt.Assert(t, qt.ErrorMatches(err, "bad wolf")) |
| 20 |
}) |
| 21 |
t.Run("nil", func(t *testing.T) { |
| 22 |
got := somepackage.MaybeNil() |
| 23 |
qt.Assert(t, qt.IsNil(got), qt.Commentf("value: %v", somepackage.Value)) |
| 24 |
}) |
| 25 |
} |
| 26 |
``` |
| 27 |
|
| 28 |
### Assertions |
| 29 |
|
| 30 |
An assertion looks like this, where `qt.Equals` could be replaced by any available |
| 31 |
checker. If the assertion fails, the underlying `t.Fatal` method is called to |
| 32 |
describe the error and abort the test. |
| 33 |
|
| 34 |
qt.Assert(t, qt.Equals(someValue, wantValue)) |
| 35 |
|
| 36 |
If you don’t want to abort on failure, use `Check` instead, which calls `Error` |
| 37 |
instead of `Fatal`: |
| 38 |
|
| 39 |
qt.Check(t, qt.Equals(someValue, wantValue)) |
| 40 |
|
| 41 |
The library provides some base checkers like `Equals`, `DeepEquals`, `Matches`, |
| 42 |
`ErrorMatches`, `IsNil` and others. More can be added by implementing the Checker |
| 43 |
interface. |
| 44 |
|
| 45 |
### Other helpers |
| 46 |
|
| 47 |
The `Patch` helper makes it a little more convenient to change a global |
| 48 |
or other variable for the duration of a test. |