单元测试
之前在刚开始写了如何编写测试程序。
内置单元测试框架:
-
Fail, Error: 该测试失败,该测试继续,其他测试继续执?
-
FailNow, Fatal: 该测试失败,该测试中?,其他测试继续执?
-
代码覆盖率
go test -v -cover -
断言
https://github.com/stretchr/testify
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | func TestErrorInCode(t *testing.T) { fmt.Println("Start") t.Error("Error") fmt.Println("End") /** 运行结果: === RUN TestErrorInCode Start TestErrorInCode: functions_test.go:25: Error End --- FAIL: TestErrorInCode (0.00s) */ } func TestFatalInCode(t *testing.T) { fmt.Println("Start") t.Fatal("Error") fmt.Println("End") /** 运行结果: === RUN TestFatalInCode Start TestFatalInCode: functions_test.go:38: Error --- FAIL: TestFatalInCode (0.00s) */ } |
使用断言:
1 2 3 4 5 6 7 8 9 10 11 12 | func square(op int) int { return op * op } func TestSquareWithAssert(t *testing.T) { inputs := [...]int{1, 2, 3} expected := [...]int{1, 4, 9} for i := 0; i < len(inputs); i++ { ret := square(inputs[i]) assert.Equal(t, expected[i], ret) } } |
Benchmark
文件名以下划线
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | // 利用+=连接 func TestConcatStringByAdd(t *testing.T) { assert := assert.New(t) elems := []string{"1", "2", "3", "4", "5"} ret := "" for _, elem := range elems { ret += elem } assert.Equal("12345", ret) } // 利用buffer连接 func TestConcatStringBytesBuffer(t *testing.T) { assert := assert.New(t) var buf bytes.Buffer elems := []string{"1", "2", "3", "4", "5"} for _, elem := range elems { buf.WriteString(elem) } assert.Equal("12345", buf.String()) } func BenchmarkConcatStringByAdd(b *testing.B) { elems := []string{"1", "2", "3", "4", "5"} b.ResetTimer() for i := 0; i < b.N; i++ { ret := "" for _, elem := range elems { ret += elem } } b.StopTimer() } func BenchmarkConcatStringBytesBuffer(b *testing.B) { elems := []string{"1", "2", "3", "4", "5"} b.ResetTimer() for i := 0; i < b.N; i++ { var buf bytes.Buffer for _, elem := range elems { buf.WriteString(elem) } } } |
在命令行输入
Windows 下使? go test 命令?时,-bench=.应写为-bench="."
运行结果:
1 2 3 4 5 6 7 8 | $ go test -bench=. -benchmem goos: darwin goarch: amd64 pkg: eighteen/benchmark BenchmarkConcatStringByAdd-8 8982729 130 ns/op 16 B/op 4 allocs/op BenchmarkConcatStringBytesBuffer-8 17703706 64.9 ns/op 64 B/op 1 allocs/op PASS ok eighteen/benchmark 2.532s |
使用
BDD
BDD in Go:
项??站 :
https://github.com/smartystreets/goconvey
安装:
启动 WEB UI :
1 2 3 4 5 6 7 8 9 10 11 12 | func TestSpec(t *testing.T) { convey.Convey("Given 2 even numbers", t, func() { a := 2 b := 4 convey.Convey("When add the two numbers", func() { c := a + b convey.Convey("Then the result is still even", func() { convey.So(c%2, convey.ShouldEqual, 0) }) }) }) } |
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 | $ go test -v bdd_spec_test.go === RUN TestSpec Given 2 even numbers When add the two numbers Then the result is still even ? 1 total assertion --- PASS: TestSpec (0.00s) PASS ok command-line-arguments 0.006s |
可以看到最后一步为 ?