Golang's Test framework Stretchr/testify
$ export GOPATH=~/go$ go get github.com/stretchr/testify
And then you can see it under your Gopath directory.
$ ls ${GOPATH}/src/github.com/stretchr/testifyassert _codegen doc.go Gopkg.lock Gopkg.toml http LICENSE mock package_test.go README.md require suite vendor
I mainly use two bags
- Assert package
- Require package
The only difference is that the Require function directly causes the case to end, while the assert is marked as a case failure, but the case does not exit, but continues to execute. See an example:
Example 1: Using Assert
package mainimport ( "testing" "github.com/stretchr/testify/assert" )func TestCase1(t *testing.T) { name := "Bob" age := 10 assert.Equal(t, "bob", name) assert.Equal(t, 20, age)}
Perform:
$ go test --- FAIL: TestCase1 (0.00s) assertions.go:254: Error Trace: main_test.go:13 Error: Not equal: expected: "bob" actual : "Bob" Test: TestCase1 assertions.go:254: Error Trace: main_test.go:14 Error: Not equal: expected: 20 actual : 10 Test: TestCase1FAILexit status 1FAIL testUT 0.009s
In this example we are using assert and we can see two assert. The Equal () instructions were executed.
Example 2: Using Require
package mainimport ( "testing" "github.com/stretchr/testify/require")func TestCase1(t *testing.T) { name := "Bob" age := 10 require.Equal(t, "bob", name) require.Equal(t, 20, age)}
Perform:
$ go test--- FAIL: TestCase1 (0.00s) assertions.go:254: Error Trace: main_test.go:12 Error: Not equal: expected: "bob" actual : "Bob" Test: TestCase1FAILexit status 1FAIL testUT 0.007s
And in this case we are using require, and we can see only the first require. The Equal () instruction was executed, the second require. Equal () was not executed.
Common Stretchr/testify Framework functions:
Func Equal (t Testingt, expected, actual interface{}, Msgandargs ... interface{}) boolfunc notequal (t testingt, expected, AC Tual interface{}, Msgandargs ... interface{}) boolfunc Nil (t testingt, Object interface{}, Msgandargs ... interface{}) bool Func Notnil (t testingt, Object interface{}, Msgandargs ... interface{}) boolfunc Empty (t testingt, Object interface{}, MSGA Ndargs interface{}) boolfunc Notempty (t testingt, Object interface{}, Msgandargs ... interface{}) boolfunc NoError (t Te Stingt, err error, Msgandargs ... interface{}) Boolfunc error (t testingt, err error, Msgandargs ... interface{}) Boolfunc Ze Ro (t testingt, I interface{}, Msgandargs ... interface{}) boolfunc Notzero (t testingt, I interface{}, Msgandargs ... Interfa ce{}) Boolfunc True (t testingt, value bool, Msgandargs ... interface{}) boolfunc False (t testingt, value bool, Msgandargs. .. interface{}) Boolfunc Len (t testingt, Object interface{}, length int, Msgandargs ... interface{}) boolfunc Notcontains (t Te Stingt, S, contains interface{}, Msgandargs ... interface{}) boolfunc Notcontains (t testingt, S, contains interface{}, Msgandargs ... interface{}) Boolfunc subset (t testingt, list, subset interface{}, Msgandargs ... interface{}) (ok bool) func Notsubset (t testingt, List, Subset interface{}, Msgandargs ... interface{}) (ok bool) func fileexists (t testingt, path string, Msgandargs ... interface{ }) Boolfunc direxists (t testingt, path string, Msgandargs ... interface{}) bool
Official link:
https://github.com/stretchr/testify