Documentation
¶
Overview ¶
Package assert provides a simple and no intrusive Go test library.
Installation ¶
Use go get tool:
go get -u github.com/gribouille/go-assert
Or dep tool:
dep ensure -add github.com/gribouille/go-assert
Behavior ¶
All errors that are not assertions are panic, for example:
a.EqualTemplate(got, filename, data)
will cause a panic if the filename does not exist or if the data does not match with the template.
You could customize the behavior of the library with the environment variables:
- GO_ASSERT_STACK: show the stacktrace if the test fails
- GO_ASSERT_FATAL: uses fatal errors
- GO_ASSERT_TMP_DISABLE: disable the deletion of temporary directory with ItTmp and ItEnv
- GO_ASSERT_OS: test for a specific OS (the possible values are similar to runtime.GOOS)
or with the NewCustom constructor.
a := T.NewCustom(t, true, false)
Examples ¶
See the examples/*.go for more examples.
func TestXXX(t *testing.T) { T.New(t). It("sub test 1", func(a *T.Assert) { a.Equal(...) // ... }). It("sub test 2", func(a *T.Assert) { // ... }) }
Index ¶
- func Cp(src, dest string) error
- type Assert
- func (a *Assert) Capture(msg string, act func(), fn func(*Assert, string, string)) *Assert
- func (a *Assert) Crash(msg string, act func(), fn func(*Assert, int, string, string)) *Assert
- func (a *Assert) Equal(exp, got interface{}, msg ...interface{}) *Assert
- func (a *Assert) EqualDeep(exp, got interface{}, msg ...interface{}) *Assert
- func (a *Assert) EqualFAbs(exp, got, epsilon float64, msg ...interface{}) *Assert
- func (a *Assert) EqualFRel(exp, got, epsilon float64, msg ...interface{}) *Assert
- func (a *Assert) EqualFile(got string, filename string, msg ...interface{}) *Assert
- func (a *Assert) EqualSlice(exp, got []interface{}, msg ...interface{}) *Assert
- func (a *Assert) EqualStringSlice(exp, got []string, msg ...interface{}) *Assert
- func (a *Assert) EqualTemplate(got string, filename string, data interface{}, msg ...interface{}) *Assert
- func (a *Assert) Error(err error, errFormat string, errA ...interface{}) *Assert
- func (a *Assert) False(v bool, msg ...interface{}) *Assert
- func (a *Assert) IsDir(pth string, msg ...interface{}) *Assert
- func (a *Assert) IsFile(pth string, msg ...interface{}) *Assert
- func (a *Assert) It(msg string, fn func(*Assert)) *Assert
- func (a *Assert) ItEnv(msg string, copies ...Copy) func(func(*Assert, string)) *Assert
- func (a *Assert) ItTmp(msg string, fn func(*Assert, string)) *Assert
- func (a *Assert) Match(pattern, s string, msg ...interface{}) *Assert
- func (a *Assert) Nil(v interface{}, msg ...interface{}) *Assert
- func (a *Assert) NotEqual(exp, got interface{}, msg ...interface{}) *Assert
- func (a *Assert) NotEqualDeep(exp, got interface{}, msg ...interface{}) *Assert
- func (a *Assert) NotExists(pth string, msg ...interface{}) *Assert
- func (a *Assert) NotNil(v interface{}, msg ...interface{}) *Assert
- func (a *Assert) SetFatal(v bool) *Assert
- func (a *Assert) SetOS(v string) *Assert
- func (a *Assert) SetStack(v bool) *Assert
- func (a *Assert) Skip(args ...interface{}) *Assert
- func (a *Assert) True(v bool, msg ...interface{}) *Assert
- type Copy
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Assert ¶
type Assert struct {
// contains filtered or unexported fields
}
Assert wraps the standard testing.T structure.
func New ¶
New creates a new Assert object.
Uses the environments variables to customize the behavior:
- GO_ASSERT_STACK: show the stacktrace if the test fails
- GO_ASSERT_FATAL: uses fatal errors
- GO_ASSERT_OS: test for a specific OS (the possible values are similar to runtime.GOOS)
func NewCustom ¶
NewCustom is similar to New but not uses the environment variables.
If stack is true then the stacktrace is showed; if fatal is true then uses the fatal errors.
func (*Assert) Capture ¶
Capture the messages sends to the standard output and the standard error of the function.
Example:
T.New(t).Capture("Sub test capture", func() { fmt.Fprintf(os.Stdout, "Hello") fmt.Fprintf(os.Stderr, "World") }, func(a *T.Assert, stdout, stderr string) { a.Equal("Hello", stdout).Equal("World", stderr) })
func (*Assert) Crash ¶
Crash is similar to Capture but the function should exit the program too. The assertion captures the return code too.
func (*Assert) Equal ¶
Equal assertion.
The assertion function can add an optional custom error message:
a.Equal("a", "b"). // no message a.Equal("a", "b", "a is different of b"). a.Equal("a", "b", "%s is different of %s", "a", "b")
The assertion function can be chained:
a.Nil(...).Equal(...).True(...)
func (*Assert) EqualDeep ¶
EqualDeep is similar to Equal but uses reflect.DeepEqual to test the equality.
func (*Assert) EqualFile ¶
EqualFile tests the equality between the content of file and the got string.
func (*Assert) EqualSlice ¶
EqualSlice compares two slices of interface{}.
func (*Assert) EqualStringSlice ¶
EqualStringSlice compares tow slices of strings.
If the assertion fails then a message shows the first differences:
Error: Exp: [aaa bbb ccc ddd] Got: [aaa bbc ccc ddd] ┗━━━━━━━━━┛
func (*Assert) EqualTemplate ¶
func (a *Assert) EqualTemplate(got string, filename string, data interface{}, msg ...interface{}) *Assert
EqualTemplate is similar to EqualFile but the file can be a template. The spaces are trimed. Example:
data := struct { Version string Authors []string }{ Version: "2.0", Authors: []string{"Bob Leponge", "Mr. Krabs"}, } a.EqualTemplate(got, "testdata/version.tpl", data)
func (*Assert) Error ¶
Error message assertion.
Example:
file := "/file/not/exist" _, err := ioutil.ReadFile(file) a.Error(err, "open %s: no such file or directory", file)
func (*Assert) ItEnv ¶
ItEnv is similar to ItTmp but it copies the files or the folders in the temporary directory.
Example:
T.New(t).ItEnv("Sub test 1 with temp dir", T.Copy{"testdata/a", "a"}, T.Copy{"testdata/version.tpl", "version.tpl"}, T.Copy{"testdata/ipsum.txt", "a/ipsum.txt"}, )(func(a *T.Assert, dir string) { // ... })
func (*Assert) ItTmp ¶
ItTmp creates a sub test with a temporary directory. This directory is clean up after the test.
For debugging, the deletion of temporary folder can be disable with the environment variable GO_ASSERT_TMP_DISABLE=1.
Example:
a.ItTmp("Sub test 1 with temp dir", func(a *T.Assert, dir string) { // dir is the temporary directory })
func (*Assert) Match ¶
Match a string to a regex expression.
Example:
a.Match(`^[a-z]+\[[0-9]+\]$`, "adam[23]")
func (*Assert) NotEqualDeep ¶
NotEqualDeep is the inverse of EqualDeep.
func (*Assert) SetOS ¶
SetOS specifies if the test is operating system dependant.
The possible values are similar to the values of runtime.GOOS.