assert

package module
v4.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 12 Imported by: 7

README

assert

Go codecov license PkgGoDev Go version

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

func TestA(t *testing.T) {
    v := true
    a := assert.New(t, false)
    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 测试用的断言包

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

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

func DefaultFailureSprint(f *Failure) string

DefaultFailureSprint 默认的 FailureSprintFunc 实现

func SetFailureSprintFunc

func SetFailureSprintFunc(f FailureSprintFunc)

SetFailureSprintFunc 设置一个全局的转换方法

New 方法在默认情况下继承由此方法设置的值。

Types

type Assertion

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

Assertion 是对 testing.TB 的二次包装

func New

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

New 返回 Assertion 对象

fatal 决定在出错时是调用 testing.TB.Error 还是 testing.TB.Fatal

func NewWithEnv

func NewWithEnv(tb testing.TB, fatal bool, env map[string]string) *Assertion

NewWithEnv 以指定的环境变量初始化 Assertion 对象

env 是以 testing.TB.Setenv 的形式调用。

func (*Assertion) Assert

func (a *Assertion) Assert(expr bool, f *Failure) *Assertion

Assert 断言 expr 条件成立

f 表示在断言失败时输出的信息

普通用户直接使用 Assertion.True 效果是一样的,此函数主要供 Assertion 自身调用。

func (*Assertion) Between added in v4.2.0

func (a *Assertion) Between(v interface{}, min, max float64, msg ...interface{}) *Assertion

Between 断言 v 是否存在于 (min,max) 之间

func (*Assertion) BetweenEqual added in v4.2.0

func (a *Assertion) BetweenEqual(v interface{}, min, max float64, msg ...interface{}) *Assertion

BetweenEqual 断言 v 是否存在于 [min,max] 之间

func (*Assertion) BetweenEqualMax added in v4.2.0

func (a *Assertion) BetweenEqualMax(v interface{}, min, max float64, msg ...interface{}) *Assertion

BetweenEqualMax 断言 v 是否存在于 (min,max] 之间

func (*Assertion) BetweenEqualMin added in v4.2.0

func (a *Assertion) BetweenEqualMin(v interface{}, min, max float64, msg ...interface{}) *Assertion

BetweenEqualMin 断言 v 是否存在于 [min,max) 之间

func (*Assertion) Contains

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

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

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

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),将断言失败

Assertion.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) FileExistsFS

func (a *Assertion) FileExistsFS(fsys fs.FS, path string, msg ...interface{}) *Assertion

func (*Assertion) FileNotExists

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

func (*Assertion) FileNotExistsFS

func (a *Assertion) FileNotExistsFS(fsys fs.FS, path string, msg ...interface{}) *Assertion

func (*Assertion) Go added in v4.1.0

func (a *Assertion) Go(f func(*Assertion)) *Assertion

Go 以 goroutine 方式执行 f

func (*Assertion) Greater added in v4.2.0

func (a *Assertion) Greater(v interface{}, val float64, msg ...interface{}) *Assertion

func (*Assertion) GreaterEqual added in v4.2.0

func (a *Assertion) GreaterEqual(v interface{}, val float64, msg ...interface{}) *Assertion

func (*Assertion) IsDir added in v4.2.0

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

IsDir 断言 path 是个目录

func (*Assertion) IsDirFS added in v4.2.0

func (a *Assertion) IsDirFS(fsys fs.FS, path string, msg ...interface{}) *Assertion

func (*Assertion) IsNotDir added in v4.2.0

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

IsNotDir 断言 path 不存在或是非目录

func (*Assertion) IsNotDirFS added in v4.2.0

func (a *Assertion) IsNotDirFS(fsys fs.FS, 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) Less added in v4.2.0

func (a *Assertion) Less(v interface{}, val float64, msg ...interface{}) *Assertion

func (*Assertion) LessEqual added in v4.2.0

func (a *Assertion) LessEqual(v interface{}, val float64, msg ...interface{}) *Assertion

func (*Assertion) Match

func (a *Assertion) Match(reg *regexp.Regexp, v interface{}, msg ...interface{}) *Assertion

Match 断言 v 是否匹配正则表达式 reg

func (*Assertion) Negative added in v4.2.0

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

Negative 断言 v 为负数

NOTE: 不包含 0

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 断言没有错误

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

func (*Assertion) NotLength

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

NotLength 断言长度不是指定的值

v 可以是以下类型:

  • map
  • string
  • slice
  • array

func (*Assertion) NotMatch

func (a *Assertion) NotMatch(reg *regexp.Regexp, v interface{}, msg ...interface{}) *Assertion

NotMatch 断言 v 是否不匹配正则表达式 reg

func (*Assertion) NotNil

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

func (*Assertion) NotPanic

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

NotPanic 断言 fn 不会 panic

func (*Assertion) NotSame

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

NotSame 断言为不是同一个对象

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

Panic 断言函数会发生 panic

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

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

PanicValue 断言函数会抛出与 v 相同的信息

func (*Assertion) Positive added in v4.2.0

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

Positive 断言 v 为正数

NOTE: 不包含 0

func (*Assertion) Same

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

Same 断言为同一个对象

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 为真

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

func (*Assertion) TypeEqual

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

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

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

func (*Assertion) Wait

func (a *Assertion) Wait(d time.Duration) *Assertion

Wait 等待一定时间再执行后续操作

func (*Assertion) WaitSeconds

func (a *Assertion) WaitSeconds(s int) *Assertion

WaitSeconds 等待 s 秒再执行后续操作

func (*Assertion) When added in v4.3.0

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

When 断言 expr 为 true 且在条件成立时调用 f

当有一组依赖 expr 的断言时,可以调用此方法。f 的参数 a 即为当前实例。

func (*Assertion) Zero

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

Zero 断言是否为零值

最终调用的是 reflect.Value.IsZero 进行判断,如果是指针,则会判断指向的对象。

type Failure

type Failure struct {
	Action string                 // 操作名称,比如 Equal,NotEqual 等方法名称。
	Values map[string]interface{} // 断言出错时返回的一些额外参数
	// contains filtered or unexported fields
}

Failure 在断言出错时输出的错误信息

func NewFailure

func NewFailure(action string, user []interface{}, kv map[string]interface{}) *Failure

NewFailure 声明 Failure 对象

user 表示用户提交的反馈,其第一个元素如果是 string,那么将调用 fmt.Sprintf(user[0], user[1:]...) 对数据进行格式化,否则采用 fmt.Sprint(user...) 格式化数据; kv 表示当前错误返回的数据;

func (*Failure) User

func (f *Failure) User() string

User 返回用户提交的返馈信息

type FailureSprintFunc

type FailureSprintFunc = func(*Failure) string

FailureSprintFunc 将 Failure 转换成文本的函数

NOTE: 可以使用此方法实现对错误信息的本地化。

func GetFailureSprintFunc

func GetFailureSprintFunc() FailureSprintFunc

GetFailureSprintFunc 获取当前的 FailureSprintFunc 方法

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