assert

package module
v2.3.2 Latest Latest
Warning

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

Go to latest
Published: May 20, 2022 License: MIT Imports: 8 Imported by: 1

README

assert

Go codecov license PkgGoDev Go version

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

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

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

// 也可以对 testing.B 使用
func Benchmark1(b *testing.B) {
    a := assert.New(b, false)
    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, false)

    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})
}

也可以直接对原始数据进行测试。

// 请求数据
req :=`POST /users HTTP/1.1
Host: example.com
Content-type: application/json

{"username": "admin", "password":"123"}

`

// 期望的返回数据
resp :=`HTTP/1.1 201
Location: https://example.com/users/1
`

func TestRaw(t *testing.T) {
    a := assert.New(t, false)
    rest.RawHTTP(a, nil,req, resp)
}

版权

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

Documentation

Overview

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

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

    a := assert.New(t, false)
    a.True(v==5, "v的值[%v]不等于5", v).
        Equal(5, v, "v的值[%v]不等于5", v).
        Nil(v).
        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 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

Types

type Assertion

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

Assertion 可以以对象的方式调用包中的各个断言函数

func New

func New(tb testing.TB, fatal bool) *Assertion

New 返回 Assertion 对象

fatal 决定在出错时是调用 tb.Error 还是 tb.Fatal;

func (*Assertion) Assert

func (a *Assertion) Assert(expr bool, msg1, msg2 []interface{}) *Assertion

Assert 断言 expr 条件成立

msg1,msg2 输出的错误信息,优先使用 msg1 中的信息,若不存在,则使用 msg2 的内容。 msg1 和 msg2 格式完全相同,根据其每一个元素是否为 string 决定是调用 Error 还是 Errorf。

普通用户直接使用 True 效果是一样的, 之所以提供该函数,主要供库调用,可以提供一个默认的错误信息。

func (*Assertion) Contains

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

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

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

func (*Assertion) Empty

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

func (*Assertion) Equal

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

func (*Assertion) Error

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

Error 断言有错误发生

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

NotNil 的特化版本,限定了类型为 error。

func (*Assertion) ErrorIs

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

ErrorIs 断言 expr 为 target 类型

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

func (*Assertion) ErrorString

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

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

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

func (*Assertion) False

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

func (*Assertion) FileExists

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

func (*Assertion) FileNotExists

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

func (*Assertion) Length

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

Length 断言长度是否为指定的值

v 可以是 map,string,slice,array

func (*Assertion) Nil

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

func (*Assertion) NotContains

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

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

func (*Assertion) NotEmpty

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

func (*Assertion) NotEqual

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

func (*Assertion) NotError

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

NotError 断言没有错误

Nil 的特化版本,限定了类型为 error。

func (*Assertion) NotLength

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

NotLength 断言长度不是指定的值

v 可以是 map,string,slice,array

func (*Assertion) NotNil

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

func (*Assertion) NotPanic

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

func (*Assertion) NotZero

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

func (*Assertion) PanicString

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

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

func (*Assertion) PanicType

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

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

func (*Assertion) Run

func (a *Assertion) Run(name string, f func(a *Assertion)) *Assertion

Run 添加子测试

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 断言表达式 expr 为 true

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

func (*Assertion) TypeEqual

func (a *Assertion) TypeEqual(ptr bool, v1, v2 interface{}, msg ...interface{}) *Assertion

TypeEqual 断言两个值的类型是否相同

ptr 如果为 true,则会在对象为指定时,查找其指向的对象。

func (*Assertion) Zero

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