【GO】23.Golang 测试库 testify

  • 下载testify库

go get github.com/stretchr/testify
  • 测试方法

package goconveydemo

import "errors"

func IsEqual(a, b int) bool{
	return a == b
}

func IsEqualWithErr(a, b int) (bool, error){
	if a > b {
		return false, errors.New("over")
	} else if a < b{
		return false, errors.New("under")
	} else{
		return true, nil
	}
}
  • 测试用例

package testifydemo

import (
	"github.com/stretchr/testify/assert"
	"gopractice/projects/testdemo/goconveydemo"
	"testing"
)

func TestIsEqual(t *testing.T){
	ok1 := goconveydemo.IsEqual(1, 1)
	ok2 := goconveydemo.IsEqual(1, 2)

	ok3, err3 := goconveydemo.IsEqualWithErr(1, 1)
	ok4, err4 := goconveydemo.IsEqualWithErr(1, 2)

	assert.True(t, ok1, "a:1 b:1 true")
	assert.False(t, ok2, "a:1 b:2 false")

	assert.Equal(t, true, ok3, "a:1 b:1 true")
	assert.Nil(t, err3, "a:1 b:1 nil")

	assert.Equal(t, false, ok4, "a:1 b:2 false")
	assert.NotNil(t, err4, "a:1 b:2 not nil")
}
  • 结果

go test -v
=== RUN   TestIsEqual
--- PASS: TestIsEqual (0.00s)
PASS
ok  	gopractice/projects/testdemo/testifydemo	0.014s

如果出现错误会把信息打印出来

go test -v
=== RUN   TestIsEqual
--- FAIL: TestIsEqual (0.00s)
    gotestify_functions_test.go:22: 
        	Error Trace:	gotestify_functions_test.go:22
        	Error:      	Not equal: 
        	            	expected: true
        	            	actual  : false
        	Test:       	TestIsEqual
        	Messages:   	a:1 b:2 false
FAIL
exit status 1
FAIL	gopractice/projects/testdemo/testifydemo	0.014s
  • 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

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值