package cal
func Plus(a, b int) int {
return a + b
}
func Sub(a, b int) int {
return a - b
}
func Multi(a, b int) int {
return a * b
}
func Division(a, b int) int {
return a / b
}
cal_test.go(针对加乘除三个函数测试):
package cal
import (
"testing"
)
func TestPlus(t *testing.T) {
res := Plus(1, 2)
e := 3 // 期望值
if res != 3 { // 失败
t.Fatalf("执行失败:期望值 %v, 目标值 %v ", e, res) // 发生致命错误,并格式化错误信息
}
t.Logf("执行成功:期望值 %v, 目标值 %v", e, res) // 打印日志
}
func TestDivision(t *testing.T) {
res := Division(1, 2)
t.Logf("执行成功:目标值 %v", res)
}
// 测试除零的情况
func TestDivisionByZero(t *testing.T) {
res := Division(1, 0)
t.Logf("执行成功:目标值 %v", res) // 打印日志
}
sub_test.go(针对减法函数测试):
package cal
import "testing"
func TestSub(t *testing.T) {
res := Sub(1, 2)
if res != -1 {
t.Fatalf("执行失败:期望值 %v, 目标值 %v ", -1, res) // 失败并打印信息
}
}
执行命令go test -v可以测试当前所在文件夹的包中的所有测试用例:
$ go test -v
=== RUN TestPlus # Plus方法通过
cal_test.go:14: 执行成功:期望值 3, 目标值 3
--- PASS: TestPlus (0.00s)
=== RUN TestDivision # Division方法通过
cal_test.go:19: 执行成功:目标值 0
--- PASS: TestDivision (0.00s) # 这是运行时间
=== RUN TestDivisionByZero # Division除0不通过
--- FAIL: TestDivisionByZero (0.00s)
panic: runtime error: integer divide by zero [recovered]
panic: runtime error: integer divide by zero
goroutine 8 [running]:
testing.tRunner.func1.2(0x11217e0, 0x1215b40)
/usr/local/go/src/testing/testing.go:1143 +0x332
testing.tRunner.func1(0xc000001800)
/usr/local/go/src/testing/testing.go:1146 +0x4b6
panic(0x11217e0, 0x1215b40)
/usr/local/go/src/runtime/panic.go:965 +0x1b9
cal.Division(...)
/Users/yangsx/GoLandProjects/notes-golang/src/cal/cal.go:16
cal.TestDivisionByZero(0xc000001800)
/Users/yangsx/GoLandProjects/notes-golang/src/cal/cal_test.go:23 +0x12
testing.tRunner(0xc000001800, 0x114d280)
/usr/local/go/src/testing/testing.go:1193 +0xef
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:1238 +0x2b3
exit status 2
FAIL cal 0.353s
只打印测试失败结果
去除-v参数即可,如下测试就没有打印任何成功的测试用例:
$ go test
--- FAIL: TestDivisionByZero (0.00s)
panic: runtime error: integer divide by zero [recovered]
panic: runtime error: integer divide by zero
goroutine 18 [running]:
testing.tRunner.func1.2(0x11217e0, 0x1215b40)
/usr/local/go/src/testing/testing.go:1143 +0x332
testing.tRunner.func1(0xc000082600)
/usr/local/go/src/testing/testing.go:1146 +0x4b6
panic(0x11217e0, 0x1215b40)
/usr/local/go/src/runtime/panic.go:965 +0x1b9
cal.Division(...)
/Users/yangsx/GoLandProjects/notes-golang/src/cal/cal.go:16
cal.TestDivisionByZero(0xc000082600)
/Users/yangsx/GoLandProjects/notes-golang/src/cal/cal_test.go:23 +0x12
testing.tRunner(0xc000082600, 0x114d280)
/usr/local/go/src/testing/testing.go:1193 +0xef
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:1238 +0x2b3
exit status 2
FAIL cal 0.726s
针对某个测试文件测试
注意,一定要带上被测试的原文件:
$ go test sub_test.go cal.go
ok command-line-arguments 0.421s
针对某个测试函数测试
仅测试TestSub单元测试函数:
$ go test -v -test.run TestSub
=== RUN TestSub
--- PASS: TestSub (0.00s)
PASS
ok cal 0.235s
$ go get github.com/smartystreets/goconvey
# 或者
$ go install github.com/smartystreets/goconvey
使用案例
目标测试文件 calc.go:
package goconvey
import "errors"
func Add(a, b int) int {
return a + b
}
func Subtract(a, b int) int {
return a - b
}
func Multiply(a, b int) int {
return a * b
}
func Division(a, b int) (int, error) {
if b == 0 {
return 0, errors.New("除数不能为0")
}
return a / b, nil
}
$ go test
# 四个测试方法,共有四个测试结果
. # 代表一个So判断
1 total assertion
.
2 total assertions
.
3 total assertions
... # 代表三个So判断
6 total assertions
PASS
ok notes-golang/goconvey 0.693s
使用go test -v,查看更详细的结果:
$ go test -v
=== RUN TestAdd
将两个数相加 ✔ # ✔ 代表So的通过次数
1 total assertion
--- PASS: TestAdd (0.00s)
=== RUN TestSubtract
将两个数相减 ✔
2 total assertions
--- PASS: TestSubtract (0.00s)
=== RUN TestMultiply
将两个数相乘 ✔
3 total assertions
--- PASS: TestMultiply (0.00s)
=== RUN TestDivision
将两个数相除
除数不为0 ✔✔ # ✔ 代表So的通过次数,有可能为✘,就代表So不通过的个数
除数为0 ✔
6 total assertions
--- PASS: TestDivision (0.00s)
PASS
ok notes-golang/goconvey 0.236s