Documentation
¶
Overview ¶
Package testutil contains utilities for common testing patterns.
Index ¶
- func AssertErrorMsg(t testing.TB, msg string, err error) (ok bool)
- func AssertMarshalText(t testing.TB, s string, v encoding.TextMarshaler) (ok bool)
- func AssertUnmarshalText(t testing.TB, s string, v encoding.TextUnmarshaler) (ok bool)
- func CleanupAndRequireSuccess(t testing.TB, f func() (err error))
- type PanicT
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertErrorMsg ¶
AssertErrorMsg asserts that the error is not nil and that its message is equal to msg. If msg is an empty string, AssertErrorMsg asserts that the error is nil instead.
func AssertMarshalText ¶
AssertMarshalText checks that the implementation of v's MarshalText works in all situations and results in the string s. v must be a pointer.
func AssertUnmarshalText ¶
AssertUnmarshalText checks that the implementation of v's UnmarshalText works in all situations and results in a value deeply equal to want.
func CleanupAndRequireSuccess ¶
CleanupAndRequireSuccess sets a cleanup function which checks the error returned by f and fails the test using t if there is one.
Types ¶
type PanicT ¶ added in v0.10.7
type PanicT struct{}
PanicT can be used with the helpers from package require in cases when testing.T and similar standard test helpers aren't safe for use, e.g. stub HTTP handlers and goroutines.
While this type also implements assert.TestingT, prefer to use require helper functions, since this helper panics, which immediately fails the test.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/AdguardTeam/golibs/testutil"
"github.com/stretchr/testify/require"
)
func main() {
sigChan := make(chan struct{})
catchf := func(substr, msg string) {
caught := fmt.Sprintf("%v", recover())
// Check against the OS-independent part of the test failure message.
fmt.Printf("%s: %t\n", msg, strings.Contains(caught, substr))
sigChan <- struct{}{}
}
t := testutil.PanicT{}
go func() {
defer catchf("Should be true", "contains meaningful message")
require.True(t, false)
}()
<-sigChan
go func() {
defer catchf("test failed", "contains a test failure")
t.FailNow()
}()
<-sigChan
}
Output: contains meaningful message: true contains a test failure: true