A (very) simple expectation, and diff, package for use in test suites.
You will need the following things properly installed on your computer.
With Go module support (Go 1.11+), simply add the following import
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:
$ go get -u github.com/crumbandbase/expectIn the simple case where two like values need to be tested for equality the
expect.Equal function can be used.
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 package. For example the output
that will be printed for this test suite is:
expect.go:15: greetings were not equal
expect.go:16:
string(
- "Hello, Picard",
+ "Bonjour, Picard",
)This project is licensed under the MIT License.