# README
assert
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)
}
版权
# Packages
Package rest 简单的 API 测试库.
# Functions
HasPanic 判断 fn 函数是否会发生 panic 若发生了 panic,将把 msg 一起返回。.
IsContains 判断 container 是否包含了 item 的内容。若是指针,会判断指针指向的内容, 但是不支持多重指针。
若 container 是字符串(string、[]byte 和 []rune,不包含 fmt.Stringer 接口), 都将会以字符串的形式判断其是否包含 item。 若 container 是个列表(array、slice、map)则判断其元素中是否包含 item 中的 的所有项,或是 item 本身就是 container 中的一个元素。.
IsEmpty 判断一个值是否为空(0, "", false, 空数组等)。 []string{""}空数组里套一个空字符串,不会被判断为空。.
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}.
IsNil 判断一个值是否为 nil。 当特定类型的变量,已经声明,但还未赋值时,也将返回 true.
New 返回 Assertion 对象
fatal 决定在出错时是调用 tb.Error 还是 tb.Fatal;.