Description
Package testdeep allows extremely flexible deep comparison, built for testing.
go-testdeep is a go rewrite and adaptation of wonderful Test::Deep perl.
In golang, comparing data structure is usually done using reflect.DeepEqual or using a package that uses this function behind the scene.
This function works very well, but it is not flexible. Both compared structures must match exactly and when a difference is returned, it is up to the caller to display it. Not easy when comparing big data structures.
The purpose of go-testdeep is to do its best to introduce this missing flexibility using βoperatorsβ, when the expected value (or one of its component) cannot be matched exactly, mixed with some useful comparison functions.
tdhttp sub package allows testing HTTP API very easily, possibly using the same operators described above.
go-testdeep alternatives and similar packages
Based on the "Testing Frameworks" category.
Alternatively, view go-testdeep alternatives based on common mentions on social networks and blogs.
-
Testify
A toolkit with common assertions and mocks that plays nicely with the standard library -
GoConvey
Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go. -
Looking for Maintainer
Selenium/Webdriver client for Go -
gnomock
Test your code without writing mocks with ephemeral Docker containers π¦ Setup popular services with just a couple lines of code β±οΈ No bash, no yaml, only code π» -
go-vcr
Record and replay your HTTP interactions for fast, deterministic and accurate tests -
testfixtures
Ruby on Rails like test fixtures for Go. Write tests against a real database -
goc
A Comprehensive Coverage Testing System for The Go Programming Language -
embedded-postgres
Run a real Postgres database locally on Linux, OSX or Windows as part of another Go application or test -
gotest.tools
A collection of packages to augment the go testing package and support common patterns. -
testza
Full-featured test framework for Go! Assertions, fuzzing, input testing, output capturing, and much more! π -
go-carpet
go-carpet - show test coverage in terminal for Go source files -
dbcleaner
Clean database for testing, inspired by database_cleaner for Ruby -
GoSpec
Testing framework for Go. Allows writing self-documenting tests/specifications, and executes them concurrently and safely isolated. [UNMAINTAINED] -
testcase
testcase is an opinionated testing framework to support test driven design. -
jsonassert
A Go test assertion library for verifying that two representations of JSON are semantically equal -
assert
:exclamation:Basic Assertion Library used along side native go testing, with building blocks for custom assertions -
gherkingen
Behaviour Driven Development tests generator for Golang -
gogiven
gogiven - BDD testing framework for go that generates readable output directly from source code -
schema
Quick and easy expression matching for JSON schemas used in requests and responses -
gosuite
Test suites support for standard Go1.7 "testing" by leveraging Subtests feature -
testsql
Generate test data from SQL files before testing and clear it after finished.
Static code analysis for 29 languages.
Do you think we are missing an alternative of go-testdeep or a related project?
README
go-testdeep
Extremely flexible golang deep comparison, extends the go testing package.
Currently supports go 1.9 β 1.19.
- Latest news
- Synopsis
- Description
- Installation
- Functions
- Available operators
- Helpers
- See also
- License
- FAQ
Latest news
- 2022/08/07: v1.12.0 release;
- 2022/01/05: v1.11.0 release;
- 2021/08/31: v1.10.0 release; with minor fixes;
- 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 flexible comparisons using TestDeep operators:
import (
"testing"
"github.com/maxatome/go-testdeep/td"
)
func TestMyFunc(t *testing.T) {
td.Cmp(t, MyFunc(), td.Struct(
&Info{Name: "Alice"},
td.StructFields{
"Age": td.Between(40, 45),
},
))
}
Or anchoring operators directly in literals, as in:
import (
"testing"
"github.com/maxatome/go-testdeep/td"
)
func TestMyFunc(tt *testing.T) {
t := td.NewT(tt)
t.Cmp(MyFunc(), &Info{
Name: "Alice",
Age: t.Anchor(td.Between(40, 45)).(int),
})
}
To most complex one, allowing to easily test HTTP API routes, using flexible operators:
import (
"testing"
"time"
"github.com/maxatome/go-testdeep/helpers/tdhttp"
"github.com/maxatome/go-testdeep/td"
)
type Person struct {
ID uint64 `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
CreatedAt time.Time `json:"created_at"`
}
func TestMyApi(t *testing.T) {
var id uint64
var createdAt time.Time
testAPI := tdhttp.NewTestAPI(t, myAPI) // β β
testAPI.PostJSON("/person", Person{Name: "Bob", Age: 42}). // β β‘
Name("Create a new Person").
CmpStatus(http.StatusCreated). // β β’
CmpJSONBody(td.JSON(`
// Note that comments are allowed
{
"id": $id, // set by the API/DB
"name": "Alice",
"age": Between(40, 45), // β β£
"created_at": "$createdAt", // set by the API/DB
}`,
td.Tag("id", td.Catch(&id, td.NotZero())), // β β€
td.Tag("created_at", td.All( // β β₯
td.HasSuffix("Z"), // β β¦
td.Smuggle(func(s string) (time.Time, error) { // β β§
return time.Parse(time.RFC3339Nano, s)
}, td.Catch(&createdAt, td.Between(testAPI.SentAt(), time.Now()))), // β β¨
)),
))
if !testAPI.Failed() {
t.Logf("The new Person ID is %d and was created at %s", id, createdAt)
}
}
- the API handler ready to be tested;
- the POST request with automatic JSON marshalling;
- the expected response HTTP status should be
http.StatusCreated
and the line just below, the body should match theJSON
operator; - some operators can be embedded, like
Between
here; - for the
$id
placeholder,Catch
its value: put it inid
variable and check it isNotZero
; - for the
$createdAt
placeholder, use theAll
operator. It combines several operators like a AND; - check that
$createdAt
date ends with "Z" usingHasSuffix
. As we expect a RFC3339 date, we require it in UTC time zone; - convert
$createdAt
date into atime.Time
using a custom function thanks to theSmuggle
operator; - then
Catch
the resulting value: put it increatedAt
variable and check it is greater or equal thantestAPI.SentAt()
(the time just before the request is handled) and lesser or equal thantime.Now()
.
See tdhttp
helper or the
FAQ
for details about HTTP API testing.
Example of produced error in case of mismatch:
Description
go-testdeep is historically a go rewrite and adaptation of wonderful Test::Deep perl.
In golang, comparing data structure is usually done using reflect.DeepEqual or using a package that uses this function behind the scene.
This function works very well, but it is not flexible. Both compared structures must match exactly and when a difference is returned, it is up to the caller to display it. Not easy when comparing big data structures.
The purpose of go-testdeep, via the
td
package
and its
helpers,
is to do its best to introduce this missing flexibility using
"operators", when the
expected value (or one of its component) cannot be matched exactly,
mixed with some useful
comparison functions.
See go-testdeep.zetta.rocks for details.
Installation
$ go get -u github.com/maxatome/go-testdeep
Helpers
The goal of helpers is to make use of go-testdeep
even more powerful
by providing common features using
TestDeep operators
behind the scene.
tdhttp
or HTTP API testing helper
The package github.com/maxatome/go-testdeep/helpers/tdhttp
provides
some functions to easily test HTTP handlers.
See tdhttp
documentation for details or
FAQ for an
example of use.
tdsuite
or testing suite helper
The package github.com/maxatome/go-testdeep/helpers/tdsuite
adds tests
suite feature to go-testdeep in a non-intrusive way, but easily and powerfully.
A tests suite is a set of tests run sequentially that share some data.
Some hooks can be set to be automatically called before the suite is run, before, after and/or between each test, and at the end of the suite.
See tdsuite
documentation for details.
tdutil
aka the helper of helpers
The package github.com/maxatome/go-testdeep/helpers/tdutil
allows to
write unit tests for go-testdeep helpers and so provides some helpful
functions.
See
tdutil
for details.
See also
- testify: a toolkit with common assertions and mocks that plays nicely with the standard library
- go-cmp: package for comparing Go values in tests
License
go-testdeep
is released under the BSD-style license found in the
[LICENSE
](LICENSE) file in the root directory of this source tree.
Internal function deepValueEqual
is based on deepValueEqual
from
reflect
golang package licensed
under the BSD-style license found in the LICENSE
file in the golang
repository.
Uses two files (bypass.go
& bypasssafe.go
) from
Go-spew which is licensed under
the copyfree ISC License.
Public Domain Gopher provided by Egon Elbre. The Go gopher was designed by Renee French.
FAQ
See FAQ.
<!-- links:begin -->
<!-- links:end -->
*Note that all licence references and agreements mentioned in the go-testdeep README section above
are relevant to that project's source code only.