当前位置: 首页 > 工具软件 > Testify > 使用案例 >

【GO】23.Golang 测试库 testify

史钊
2023-12-01
  • 下载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

 

 类似资料: