td.T
Constructing *td.T
import (
"testing"
"github.com/maxatome/go-testdeep/td"
)
func TestMyFunc(tt *testing.T) {
t := td.NewT(tt)
t.Cmp(MyFunc(), 12)
}func NewT(t testing.TB, config ...ContextConfig) *Tfunc Assert(t testing.TB, config ...ContextConfig) *Tfunc Require(t testing.TB, config ...ContextConfig) *Tfunc AssertRequire(t testing.TB, config ...ContextConfig) (assert, require *T)
Configuring *td.T
func TestMyFunc(tt *testing.T) {
t := td.NewT(tt).UseEqual().RootName("RECORD")
...
}func (t *T) BeLax(enable ...bool) *Tfunc (t *T) FailureIsFatal(enable ...bool) *Tfunc (t *T) IgnoreUnexported(types ...any) *Tfunc (t *T) RootName(rootName string) *Tfunc (t *T) UseEqual(types ...any) *T
Main methods of *td.T
import (
"testing"
"github.com/maxatome/go-testdeep/td"
)
func TestMyFunc(tt *testing.T) {
t := td.NewT(tt).UseEqual()
// Compares MyFunc() result against a fixed value
t.Cmp(MyFunc(), 128, "MyFunc() result is 128")
// Compares MyFunc() result using the Between Testdeep operator
t.Cmp(MyFunc(), td.Between(100, 199),
"MyFunc() result is between 100 and 199")
}func (t *T) Cmp(got, expected any, args ...any) boolfunc (t *T) CmpError(got error, args ...any) boolfunc (t *T) CmpLax(got, expected any, args ...any) bool(in fact the shortcut ofLaxoperator)func (t *T) CmpNoError(got error, args ...any) boolfunc (t *T) CmpNotPanic(fn func(), args ...any) boolfunc (t *T) CmpPanic(fn func(), expected any, args ...any) boolfunc (t *T) False(got any, args ...any) boolfunc (t *T) Not(got, notExpected any, args ...any) bool(in fact the shortcut ofNotoperator)func (t *T) Run(name string, f func(t *T)) boolfunc (t *T) RunAssertRequire(name string, f func(assert, require *T)) boolfunc (t *T) True(got any, args ...any) bool
CmpDeeply()
method is now replaced by
Cmp(),
but it is still available for backward compatibility purpose.
Anchoring methods of *td.T
func (t *T) A(operator TestDeep, model ...any) anyfunc (t *T) Anchor(operator TestDeep, model ...any) anyfunc (t *T) AnchorsPersistTemporarily() func()func (t *T) DoAnchorsPersist() boolfunc (t *T) ResetAnchors()func (t *T) SetAnchorsPersist(persist bool)
Thanks to generics, one can also use:
Shortcut methods of *td.T
import (
"testing"
"github.com/maxatome/go-testdeep/td"
)
func TestMyFunc(tt *testing.T) {
t := td.NewT(tt).UseEqual()
t.Between(MyFunc(), 100, 199, td.BoundsInIn,
"MyFunc() result is between 100 and 199")
}For each of these methods, it is always a shortcut on
T.Cmp() and
the correponding Testdeep operator:
T.HasPrefix(got, expected, …) ⇒ T.Cmp(t, got, HasPrefix(expected), …)
^-------^ ^-------^
+-------------------------------------------+Excluding Lax operator for which the
shortcut method stays CmpLax.
Each shortcut method is described in the corresponding operator page. See operators list.
Comparison hooks
func TestCmpHook(tt *testing.T) {
t := td.NewT(tt)
// Test time.Time via its Equal() method instead of default
// field/field (note it bypasses the UseEqual flag)
t = t.WithCmpHooks((time.Time).Equal)
date, _ := time.Parse(time.RFC3339, "2020-09-08T22:13:54+02:00")
t.Cmp(date, date.UTC()) // succeeds
// Each encountered string is converted to int
t = t.WithSmuggleHooks(strconv.Atoi)
t.Cmp("123", 123) // succeeds
}