單元測試
- t.Error()測試失敗纱注,后面的可以執(zhí)行畏浆,其他測試?yán)^續(xù)執(zhí)行
- t.Fatal()測試失敗,后面的不會執(zhí)行狞贱,其它測試?yán)^續(xù)執(zhí)行
package testing
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSquare(t *testing.T) {
inputs := [...]int{1, 2, 3}
expected := [...]int{1, 4, 9}
for i := 0; i < len(inputs); i++ {
ret := square(inputs[i])
if ret != expected[i] {
t.Errorf("input 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")
}
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)
}
}
代碼覆蓋率 -cover
斷言
benchmark
go test -bench=. -benchmem
> 方法名以Benchmark開頭刻获,參數(shù)testing.B
benchmem 打印內(nèi)存分配次數(shù)
## bdd