golang的测试框架stretchr/testify

golang的测试框架stretchr/testify

  • 安装
$ export GOPATH=~/go
$ go get github.com/stretchr/testify

然后在你的GOPATH目录下面就可以看到

$ ls ${GOPATH}/src/github.com/stretchr/testify
assert  _codegen  doc.go  Gopkg.lock  Gopkg.toml  http  LICENSE  mock  package_test.go  README.md  require  suite  vendor

我主要用两个包

  1. assert package
  2. require package

他们的唯一差别就是require的函数会直接导致case结束,而assert虽然也标记为case失败,但case不会退出,而是继续往下执行。看一个例子:
例子1:使用assert

package main

import (
  "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)
}

执行:

$ 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:           TestCase1
FAIL
exit status 1
FAIL    testUT  0.009s

在这个例子中我们使用的是assert,可以看到两个assert.Equal()指令都被执行了。
例子2:使用require

package main

import (
  "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)
}

执行:

$ go test
--- FAIL: TestCase1 (0.00s)
        assertions.go:254: 
                        Error Trace:    main_test.go:12
                        Error:          Not equal: 
                                        expected: "bob"
                                        actual  : "Bob"
                        Test:           TestCase1
FAIL
exit status 1
FAIL    testUT  0.007s

而在这个例子中我们使用的是require,可以看到只有第一个require.Equal()指令被执行了,第二个require.Equal()没有被执行。

常用的stretchr/testify框架函数:

func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool
func NotEqual(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool

func Nil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool
func NotNil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool

func Empty(t TestingT, object interface{}, msgAndArgs ...interface{}) bool
func NotEmpty(t TestingT, object interface{}, msgAndArgs ...interface{}) bool

func NoError(t TestingT, err error, msgAndArgs ...interface{}) bool
func Error(t TestingT, err error, msgAndArgs ...interface{}) bool

func Zero(t TestingT, i interface{}, msgAndArgs ...interface{}) bool
func NotZero(t TestingT, i interface{}, msgAndArgs ...interface{}) bool

func True(t TestingT, value bool, msgAndArgs ...interface{}) bool
func False(t TestingT, value bool, msgAndArgs ...interface{}) bool

func Len(t TestingT, object interface{}, length int, msgAndArgs ...interface{}) bool

func NotContains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool
func NotContains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool
func 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{}) bool
func DirExists(t TestingT, path string, msgAndArgs ...interface{}) bool

官方链接:
https://github.com/stretchr/testify

    原文作者:CodingCode
    原文地址: https://www.jianshu.com/p/ad46bbbf877c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞