Golang 測試框架
案例
cal.go
package test
func add(a,b int) int{
return a + b
}
cal_test.go
package test
import (
"fmt"
//引入go的測試框架
"testing"
)
//測試框架的函數必須以TestXxx形式
func TestAdd(t *testing.T) {
res := add(10, 2)
if res == 2 {
fmt.Println("錯誤")
} else {
t.Log("正確")
}
}
測試套件要與被測試的包在同一個包下,文件名必須以_test.go
結尾,方法必須以TestXxx(*testing.T)
的形式