Categorygithub.com/issue9/assert/v4
modulepackage
4.3.1
Repository: https://github.com/issue9/assert.git
Documentation: pkg.go.dev

# 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 文件中找到。

# Packages

Package rest 简单的 API 测试库.

# Functions

DefaultFailureSprint 默认的 [FailureSprintFunc] 实现.
GetFailureSprintFunc 获取当前的 [FailureSprintFunc] 方法.
New 返回 [Assertion] 对象 fatal 决定在出错时是调用 [testing.TB.Error] 还是 [testing.TB.Fatal];.
NewFailure 声明 [Failure] 对象 user 表示用户提交的反馈,其第一个元素如果是 string,那么将调用 fmt.Sprintf(user[0], user[1:]...) 对数据进行格式化,否则采用 fmt.Sprint(user...) 格式化数据; kv 表示当前错误返回的数据;.
NewWithEnv 以指定的环境变量初始化 [Assertion] 对象 env 是以 [testing.TB.Setenv] 的形式调用。.
SetFailureSprintFunc 设置一个全局的转换方法 [New] 方法在默认情况下继承由此方法设置的值。.

# Structs

Assertion 是对 [testing.TB] 的二次包装.
Failure 在断言出错时输出的错误信息.

# Type aliases

FailureSprintFunc 将 [Failure] 转换成文本的函数 NOTE: 可以使用此方法实现对错误信息的本地化。.