As a powerful and two-year-old testing framework, go-testdeep allows you to deeply test any golang data structure. It is fully integrated with testing standard package.
import (
"testing"
"github.com/maxatome/go-testdeep/td"
)
func TestLoadPerson(t *testing.T) {
// LoadPerson() (Person, error)
person, err := LoadPerson()
if td.CmpNoError(t, err) {
// person has to match exactly
td.Cmp(t, person, Person{Name: "Bob", Age: 24})
}
}
But many testing frameworks allow you to do this kind of comparison, no need for a new one!
As go-testdeep uses its own function to deeply compare structures, it allows you to use 60 operators for more flexibility.
Take the Bag operator as an example:
import (
"testing"
"github.com/maxatome/go-testdeep/td"
)
func TestLoadPersons(t *testing.T) {
// LoadPersons(string) ([]Person, error)
persons, err := LoadPersons("B*")
if td.CmpNoError(t, err) {
// persons slice has to contains these 4 persons
// in whatever order
td.Cmp(t, persons,
td.Bag(
Person{Name: "Bob", Age: 24},
Person{Name: "Britt", Age: 29},
Person{Name: "Barbara", Age: 35},
Person{Name: "Brian", Age: 21},
))
}
}
It is just an example, as the tests can be much more complex. See:
- the synopsis;
- an example from classic approach to the go-testdeep ones;
- the FAQ, it contains some examples too.
The tdhttp helper package allows you to easily test your HTTP API using these operators.
See the example of a simple JSON/XML HTTP API tested using go-testdeep and its tdhttp package under the Go Play Space: https://goplay.space/#xrGKu2cNQFr
It reports colored error messages when something goes wrong, here used with another operator, Between:
All the functions and methods are fully documented, each operator comes with its examples, and last but not least the test code coverage is close to 100%.
Do not hesitate to comment, file an issue or a PR. I would be happy to help you or simply discuss.
Enjoy!
maxatome / go-testdeep
Extremely flexible golang deep comparison, extends the go testing package and tests HTTP APIs
go-testdeep
Extremely flexible golang deep comparison, extends the go testing package.
- Latest news
- Synopsis
- Description
- Installation
- Functions
- Available operators
- Helpers
- See also
- License
- FAQ
Latest news
- 2020/04/11
-
tdhttp
helper (the HTTP API tester) enhanced and fully documented;
-
- 2020/04/06
- new
Delay
operator;
- new
- 2020/02/29
-
github.com/maxatome/go-testdeep
deprecated (but still usable), usegithub.com/maxatome/go-testdeep/td
instead. See the FAQ point for details about migration;
-
- 2020/02/13:
- new operator anchoring feature, see the corresponding FAQ section and the new anchoring methods;
- new
SStruct
operator (and its friendsCmpSStruct
&T.SStruct
),
- see commits history for other/older changes.
Synopsis
Make golang tests easy, from simplest usage:
import (
"testing"
"github.com/maxatome/go-testdeep/td"
)
func TestMyFunc(t *testing.T) {
td.Cmp(t, MyFunc(), &Info{Name: "Alice", Age: 42})
}
To a bit more complex one, allowing…
Top comments (0)