assert

package module
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2021 License: MIT Imports: 11 Imported by: 1

README

assert

Go codecov license PkgGoDev

assert 包是对 testing 的一个简单扩展,提供的一系列的断言函数, 方便在测试函数中使用:

func TestA(t *testing.T) {
    v := true
    assert.True(v)

    a := assert.New(t)
    a.True(v)
}

// 也可以对 testing.B 使用
func Benchmark1(b *testing.B) {
    a := assert.New(b)
    v := false
    a.True(v)
    for(i:=0; i<b.N; i++) {
        // do something
    }
}

// 对 API 请求做测试,可以引用 assert/rest
func TestHTTP( t *testing.T) {
    a := assert.New(t)

    srv := rest.NewServer(a, h, nil)
    a.NotNil(srv)
    defer srv.Close()

    srv.NewRequest(http.MethodGet, "/body").
        Header("content-type", "application/json").
        Query("page", "5").
        JSONBody(&bodyTest{ID: 5}).
        Do().
        Status(http.StatusCreated).
        Header("content-type", "application/json;charset=utf-8").
        JSONBody(&bodyTest{ID: 6})
}

版权

本项目采用 MIT 开源授权许可证,完整的授权说明可在 LICENSE 文件中找到。

Documentation

Overview

Package assert 是对 testing 包的一些简单包装

提供了两种操作方式:直接调用包函数;或是使用 Assertion 对象。 两种方式完全等价,可以根据自己需要,选择一种。

func TestAssert(t *testing.T) {
    var v interface{} = 5

    // 直接调用包函数
    assert.True(t, v == 5, "v的值[%v]不等于5", v)
    assert.Equal(t, 5, v, "v的值[%v]不等于5", v)
    assert.Nil(t, v)

    // 以 Assertion 对象方式使用
    a := assert.New(t)
    a.True(v==5, "v的值[%v]不等于5", v)
    a.Equal(5, v, "v的值[%v]不等于5", v)
    a.Nil(v)
    a.TB().Log("success")

    // 以函数链的形式调用 Assertion 对象的方法
    a.True(false).Equal(5,6)
}

// 也可以对 testing.B 使用
func Benchmark1(b *testing.B) {
    a := assert.New(b)
    a.True(false)
    for(i:=0; i<b.N; i++) {
        // do something
    }
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(t testing.TB, container, item interface{}, args ...interface{})

Contains 断言 container 包含 item 的或是包含 item 中的所有项

具体函数说明可参考 IsContains()

func Empty

func Empty(t testing.TB, expr interface{}, args ...interface{})

Empty 断言 expr 的值为空(nil,"",0,false),否则输出错误信息

func Equal

func Equal(t testing.TB, v1, v2 interface{}, args ...interface{})

Equal 断言 v1 与 v2 两个值相等

func Error

func Error(t testing.TB, expr interface{}, args ...interface{})

Error 断言有错误发生

传递未初始化的 error 值(var err error = nil),将断言失败

func ErrorIs added in v1.4.0

func ErrorIs(t testing.TB, expr interface{}, target error, args ...interface{})

ErrorIs 断言 expr 为 target 类型

相当于 True(t, errors.Is(expr, target))

func ErrorString

func ErrorString(t testing.TB, expr interface{}, str string, args ...interface{})

ErrorString 断言有错误发生且错误信息中包含指定的字符串 str

传递未初始化的 error 值(var err error = nil),将断言失败

func ErrorType

func ErrorType(t testing.TB, expr interface{}, typ error, args ...interface{})

ErrorType 断言有错误发生且错误的类型与 typ 的类型相同

传递未初始化的 error 值(var err error = nil),将断言失败。

仅对 expr 是否与 typ 为同一类型作简单判断,如果要检测是否是包含关系,可以使用 errors.Is 检测。

ErrorType 与 ErrorIs 有本质的区别:ErrorIs 检测是否是包含关系,而 ErrorType 检测是否类型相同。比如:

err := os.WriteFile(...)

返回的 err 是一个 os.PathError 类型,用 ErrorType(err, &os.PathError{}) 断方正常; 而 ErrorIs(err, &os.PathError{}) 则会断言失败。

func False

func False(t testing.TB, expr bool, args ...interface{})

False 断言表达式 expr 为 false

func FileExists

func FileExists(t testing.TB, path string, args ...interface{})

FileExists 断言文件存在

func FileNotExists

func FileNotExists(t testing.TB, path string, args ...interface{})

FileNotExists 断言文件不存在

func HasPanic

func HasPanic(fn func()) (has bool, msg interface{})

HasPanic 判断 fn 函数是否会发生 panic 若发生了 panic,将把 msg 一起返回。

func IsContains

func IsContains(container, item interface{}) bool

IsContains 判断 container 是否包含了 item 的内容。若是指针,会判断指针指向的内容, 但是不支持多重指针。

若 container 是字符串(string、[]byte 和 []rune,不包含 fmt.Stringer 接口), 都将会以字符串的形式判断其是否包含 item。 若 container 是个列表(array、slice、map)则判断其元素中是否包含 item 中的 的所有项,或是 item 本身就是 container 中的一个元素。

func IsEmpty

func IsEmpty(expr interface{}) bool

IsEmpty 判断一个值是否为空(0, "", false, 空数组等)。 []string{""}空数组里套一个空字符串,不会被判断为空。

func IsEqual

func IsEqual(v1, v2 interface{}) bool

IsEqual 判断两个值是否相等。

除了通过 reflect.DeepEqual() 判断值是否相等之外,一些类似 可转换的数值也能正确判断,比如以下值也将会被判断为相等:

int8(5)                     == int(5)
[]int{1,2}                  == []int8{1,2}
[]int{1,2}                  == [2]int8{1,2}
[]int{1,2}                  == []float32{1,2}
map[string]int{"1":"2":2}   == map[string]int8{"1":1,"2":2}

// map 的键值不同,即使可相互转换也判断不相等。
map[int]int{1:1,2:2}        != map[int8]int{1:1,2:2}

func IsNil

func IsNil(expr interface{}) bool

IsNil 判断一个值是否为 nil。 当特定类型的变量,已经声明,但还未赋值时,也将返回 true

func Length added in v1.5.0

func Length(t testing.TB, v interface{}, l int, args ...interface{})

func Nil

func Nil(t testing.TB, expr interface{}, args ...interface{})

Nil 断言表达式 expr 为 nil

func NotContains

func NotContains(t testing.TB, container, item interface{}, args ...interface{})

NotContains 断言 container 不包含 item 的或是不包含 item 中的所有项

func NotEmpty

func NotEmpty(t testing.TB, expr interface{}, args ...interface{})

NotEmpty 断言 expr 的值为非空(除 nil,"",0,false之外),否则输出错误信息

func NotEqual

func NotEqual(t testing.TB, v1, v2 interface{}, args ...interface{})

NotEqual 断言 v1 与 v2 两个值不相等

func NotError

func NotError(t testing.TB, expr interface{}, args ...interface{})

NotError 断言没有错误发生

func NotLength added in v1.5.0

func NotLength(t testing.TB, v interface{}, l int, args ...interface{})

func NotNil

func NotNil(t testing.TB, expr interface{}, args ...interface{})

NotNil 断言表达式 expr 为非 nil 值

func NotPanic

func NotPanic(t testing.TB, fn func(), args ...interface{})

NotPanic 断言函数不会发生 panic

func NotZero added in v1.5.0

func NotZero(t testing.TB, v interface{}, args ...interface{})

NotZero 断言是否为非零值

最终调用的是 reflect.Value.IsZero 进行判断

func Panic

func Panic(t testing.TB, fn func(), args ...interface{})

Panic 断言函数会发生 panic

func PanicString

func PanicString(t testing.TB, fn func(), str string, args ...interface{})

PanicString 断言函数会发生 panic 且 panic 信息中包含指定的字符串内容

func PanicType

func PanicType(t testing.TB, fn func(), typ interface{}, args ...interface{})

PanicType 断言函数会发生 panic 且抛出指定的类型

func True

func True(t testing.TB, expr bool, args ...interface{})

True 断言表达式 expr 为 true

args 对应 fmt.Printf() 函数中的参数,其中 args[0] 对应第一个参数 format,依次类推, 具体可参数 formatMessage() 函数的介绍。其它断言函数的 args 参数,功能与此相同。

func Zero added in v1.5.0

func Zero(t testing.TB, v interface{}, args ...interface{})

Zero 断言是否为零值

最终调用的是 reflect.Value.IsZero 进行判断

Types

type Assertion

type Assertion struct {
	// contains filtered or unexported fields
}

Assertion 是对 testing.TB 进行了简单的封装。 可以以对象的方式调用包中的各个断言函数。

func New

func New(t testing.TB) *Assertion

New 返回 Assertion 对象。

func (*Assertion) Contains

func (a *Assertion) Contains(container, item interface{}, msg ...interface{}) *Assertion

Contains 参照 assert.Contains() 函数

func (*Assertion) Empty

func (a *Assertion) Empty(expr interface{}, msg ...interface{}) *Assertion

Empty 参照 assert.Empty() 函数

func (*Assertion) Equal

func (a *Assertion) Equal(v1, v2 interface{}, msg ...interface{}) *Assertion

Equal 参照 assert.Equal() 函数

func (*Assertion) Error

func (a *Assertion) Error(expr interface{}, msg ...interface{}) *Assertion

Error 参照 assert.Error() 函数

func (*Assertion) ErrorIs added in v1.4.0

func (a *Assertion) ErrorIs(expr interface{}, target error, msg ...interface{}) *Assertion

ErrorIs 断言 expr 为 target 类型

相当于 a.True(errors.Is(expr, target))

func (*Assertion) ErrorString

func (a *Assertion) ErrorString(expr interface{}, str string, msg ...interface{}) *Assertion

ErrorString 参照 assert.ErrorString() 函数

func (*Assertion) ErrorType

func (a *Assertion) ErrorType(expr interface{}, typ error, msg ...interface{}) *Assertion

ErrorType 参照 assert.ErrorType() 函数

func (*Assertion) False

func (a *Assertion) False(expr bool, msg ...interface{}) *Assertion

False 参照 assert.False() 函数

func (*Assertion) FileExists

func (a *Assertion) FileExists(path string, msg ...interface{}) *Assertion

FileExists 参照 assert.FileExists() 函数

func (*Assertion) FileNotExists

func (a *Assertion) FileNotExists(path string, msg ...interface{}) *Assertion

FileNotExists 参照 assert.FileNotExists() 函数

func (*Assertion) Length added in v1.5.0

func (a *Assertion) Length(v interface{}, l int, msg ...interface{}) *Assertion

func (*Assertion) Nil

func (a *Assertion) Nil(expr interface{}, msg ...interface{}) *Assertion

Nil 参照 assert.Nil() 函数

func (*Assertion) NotContains

func (a *Assertion) NotContains(container, item interface{}, msg ...interface{}) *Assertion

NotContains 参照 assert.NotContains() 函数

func (*Assertion) NotEmpty

func (a *Assertion) NotEmpty(expr interface{}, msg ...interface{}) *Assertion

NotEmpty 参照 assert.NotEmpty() 函数

func (*Assertion) NotEqual

func (a *Assertion) NotEqual(v1, v2 interface{}, msg ...interface{}) *Assertion

NotEqual 参照 assert.NotEqual() 函数

func (*Assertion) NotError

func (a *Assertion) NotError(expr interface{}, msg ...interface{}) *Assertion

NotError 参照 assert.NotError() 函数

func (*Assertion) NotLength added in v1.5.0

func (a *Assertion) NotLength(v interface{}, l int, msg ...interface{}) *Assertion

func (*Assertion) NotNil

func (a *Assertion) NotNil(expr interface{}, msg ...interface{}) *Assertion

NotNil 参照 assert.NotNil() 函数

func (*Assertion) NotPanic

func (a *Assertion) NotPanic(fn func(), msg ...interface{}) *Assertion

NotPanic 参照 assert.NotPanic() 函数

func (*Assertion) NotZero added in v1.5.0

func (a *Assertion) NotZero(v interface{}, msg ...interface{}) *Assertion

NotZero 断言是否为非零值

最终调用的是 reflect.Value.IsZero 进行判断

func (*Assertion) Panic

func (a *Assertion) Panic(fn func(), msg ...interface{}) *Assertion

Panic 参照 assert.Panic() 函数

func (*Assertion) PanicString

func (a *Assertion) PanicString(fn func(), str string, msg ...interface{}) *Assertion

PanicString 参照 assert.PanicString() 函数

func (*Assertion) PanicType

func (a *Assertion) PanicType(fn func(), typ interface{}, msg ...interface{}) *Assertion

PanicType 参照 assert.PanicType() 函数

func (*Assertion) TB

func (a *Assertion) TB() testing.TB

TB 返回 testing.TB 接口

func (*Assertion) True

func (a *Assertion) True(expr bool, msg ...interface{}) *Assertion

True 参照 assert.True() 函数

func (*Assertion) Zero added in v1.5.0

func (a *Assertion) Zero(v interface{}, msg ...interface{}) *Assertion

Zero 断言是否为零值

最终调用的是 reflect.Value.IsZero 进行判断

Directories

Path Synopsis
Package rest 简单的 API 测试库
Package rest 简单的 API 测试库

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL