Golang's Test framework Stretchr/testify

Source: Internet
Author: User

Golang's Test framework Stretchr/testify

    • Installation
$ 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

    1. Assert package
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.