一、要点
源文件以_test结尾:xxx_test.go
测试方法名以Test开头:func TestXXX(t *testing.T){...}
二、例子
package constant_test import "testing" const ( Monday = iota + 1 Tuesday Wednesday ) const ( Readable = 1 << iota Writable Executable ) func TestConstantTry(t *testing.T) { t.Log(Monday, Tuesday, Wednesday) } func TestConstantTry1(t *testing.T) { a := 7 //o111 t.Log(a&Readable == Readable, a&Writable == Writable, a&Executable == Executable) }
=== RUN TestConstantTry constant_try_test.go:17: 1 2 3 --- PASS: TestConstantTry (0.00s) === RUN TestConstantTry1 constant_try_test.go:22: true true true --- PASS: TestConstantTry1 (0.00s) PASS
package fib import ( "fmt" "testing" ) //菲波拉数列 变量赋值 func TestFibList(t *testing.T) { //var a int = 1 //var b int = 1 //var( // a int=1 // b=1 //) a := 1 b := 1 fmt.Print(a, " ") for i := 0; i < 5; i++ { fmt.Print(" ", b) tem := a a = b b = tem + a } fmt.Println() } func TestExchange(t *testing.T) { a := 1 b := 2 a, b = b, a t.Log(a, b) }
=== RUN TestFibList 1 1 2 3 5 8 --- PASS: TestFibList (0.00s) === RUN TestExchange fib_test.go:32: 2 1 --- PASS: TestExchange (0.00s) PASS
package try_test import "testing" func TestFirstTry(t *testing.T) { t.Log("My first try!") }
=== RUN TestFirstTry first_test.go:6: My first try! --- PASS: TestFirstTry (0.00s) PASS
三、Fail、Fatal
package testing func square(op int) int { return op * op }
package testing import ( "fmt" "testing" ) //Fail,Error:该测试失败,该测试继续,其他测试继续执行 //FailNow,Fatal:该测试失败,该测试中止,其他测试继续执行 func TestSquare(t *testing.T) { inputs := [...]int{1, 2, 3} expected := [...]int{1, 2, 3} for i := 0; i < len(inputs); i++ { ret := square(inputs[i]) if ret != expected[i] { t.Errorf("iput is %d,the expected is %d,the actual %d", inputs[i], expected[i], ret) } } } func TestErrorInCode(t *testing.T) { fmt.Println("start") t.Error("Error") fmt.Println("end") } func TestFailInCode(t *testing.T) { fmt.Println("start") t.Fatal("Error") fmt.Println("end") }
=== RUN TestSquare function_test.go:16: iput is 2,the expected is 2,the actual 4 function_test.go:16: iput is 3,the expected is 3,the actual 9 --- FAIL: TestSquare (0.00s) === RUN TestErrorInCode start function_test.go:23: Error end --- FAIL: TestErrorInCode (0.00s) === RUN TestFailInCode start function_test.go:29: Error --- FAIL: TestFailInCode (0.00s) FAIL
四、代码测试覆盖率
go test -v -cover
五、断言
安装依赖
go get -u github.com/stretchr/testify/assert
//使用断言 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) } }
=== RUN TestSquareWithAssert --- PASS: TestSquareWithAssert (0.00s) PASS
六、Benchmark性能测试
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 BenchmarkConcatStringByBytesBuffer(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) } } b.StopTimer() }
使用bytes.Buffer处理字符串,性能更优。
goos: windows goarch: amd64 pkg: jike_introduction/src/ch35/benchmark cpu: AMD Ryzen 7 4800H with Radeon Graphics BenchmarkConcatStringByAdd BenchmarkConcatStringByAdd-16 10294040 117.0 ns/op PASS goos: windows goarch: amd64 pkg: jike_introduction/src/ch35/benchmark cpu: AMD Ryzen 7 4800H with Radeon Graphics BenchmarkConcatStringByBytesBuffer BenchmarkConcatStringByBytesBuffer-16 16814658 70.41 ns /op PASS
七、BDD
安装依赖
go get -u github.com/smartystreets/goconvey/convey
测试编码
import ( . "github.com/smartystreets/goconvey/convey" "testing" ) func TestSpec(t *testing.T) { Convey("Given 2 even number", t, func() { a := 2 b := 4 Convey("when add two numbers", func() { c := a + b Convey("Then the result is still even", func() { So(c%2, ShouldEqual, 0) }) }) }) }
运行结果
=== RUN TestSpec . 1 total assertion --- PASS: TestSpec (0.00s) PASS
修改数值,错误结果 === RUN TestSpec x Failures: * F:/myCode/goland/jike_introduction/src/ch36/bdd/bdd_spec_test.go Line 15: Expected: '0' Actual: '1' (Should be equal) 1 total assertion --- FAIL: TestSpec (0.00s) FAIL