Skip to content

Instantly share code, notes, and snippets.

@1000copy
Last active August 29, 2015 13:59
Show Gist options
  • Save 1000copy/10529356 to your computer and use it in GitHub Desktop.
Save 1000copy/10529356 to your computer and use it in GitHub Desktop.
基于约定,优美,简洁的 go testing framework吗。
package main
import (
"testing"
"time"
)
func sum(n ...int) int {
var c int
for _, i := range n {
c += i
}
return c
}
func TestSum(t *testing.T) {
time.Sleep(time.Second * 2)
if sum(1, 2, 3) != 6 {
t.Fatal("sum error!")
}
}
// 运行go test 的时候,会执行当前目录内所有*_test.go 文件内的所有TestXyyy打头的函数,传入t *testing.T 参数。
// 约定:
// 文件名必须以_test.go 结尾
// 函数必须以Test打头。跟着的字母大写
// 简约,来源于稍微记忆一点约定!
// testing.T
// ⽅法 说明 其他
// ---------------+---------------------------------------+-------------------------------
// Fail 标记失败,但继续执⾏该测试函数。
// FailNow 失败,⽴即停⽌当前测试函数。
// Log 输出信息。仅在失败或 -v 参数时输出。   Logf
// SkipNow 跳过当前测试函数。 Skipf = SkipNow + Logf
// Error Fail + Log Errorf
// Fatal FailNow + Log Fatalf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment