https://github.com/kapetndev/expect
A (very) simple expectation, and diff, package for use in test suites.
https://github.com/kapetndev/expect
golang testing
Last synced: about 1 year ago
JSON representation
A (very) simple expectation, and diff, package for use in test suites.
- Host: GitHub
- URL: https://github.com/kapetndev/expect
- Owner: kapetndev
- License: mit
- Created: 2021-03-03T23:53:52.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-04-06T11:08:51.000Z (about 5 years ago)
- Last Synced: 2025-02-01T20:46:15.952Z (over 1 year ago)
- Topics: golang, testing
- Language: Go
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# expect 
A (very) simple expectation, and `diff`, package for use in test suites.
## Prerequisites
You will need the following things properly installed on your computer.
* [Go](https://golang.org/): any one of the **three latest major**
[releases](https://golang.org/doc/devel/release.html)
## Installation
With [Go module](https://github.com/golang/go/wiki/Modules) support (Go 1.11+),
simply add the following import
```go
import "github.com/crumbandbase/expect"
```
to your code, and then `go [build|run|test]` will automatically fetch the
necessary dependencies.
Otherwise, to install the `expect` package, run the following command:
```bash
$ go get -u github.com/crumbandbase/expect
```
## Usage
In the simple case where two like values need to be tested for equality the
`expect.Equal` function can be used.
```go
package main_test
import (
"testing"
"github.com/crumbandbase/expect"
)
func TestGreeting(t *testing.T) {
t.Run("succeeds when the greetings are equal", func(t *testing.T) {
expected := "Hello, Picard"
got := greeting("Picard")
expect.Equal(t, got, expected, "greetings were not equal")
})
t.Run("fails when the greeting are not equal", func(t *testing.T) {
expected := "Bonjour, Picard"
got := greeting("Picard")
expect.Equal(t, got, expected, "greetings were not equal")
})
}
```
In the above the second test will fail and will produce a `diff` describing the
differences. This is handled by the brilliant
[go-cmp](https://github.com/google/go-cmp) package. For example the output
that will be printed for this test suite is:
```bash
expect.go:15: greetings were not equal
expect.go:16:
string(
- "Hello, Picard",
+ "Bonjour, Picard",
)
```
## License
This project is licensed under the [MIT License](LICENSE.md).