go test用法

**注意:测试代码文件要以_test.go结尾, 代码函数Test开头参数为t testing.T 例如TestAdd(t testing.T)

同一目录下的calc.go代码

点击查看代码
package go_test_demo

func Add(a, b int) int {
	return a + b
}

func Sub(a, b int) int {
	return a - b
}

同一目录下的calc_test.go代码

点击查看代码
package go_test_demo

import (
	"testing"
)

func TestAdd(t *testing.T) {
	r := Add(2, 4)
	if r != 61 {
		t.Fatal(r)
	}
	t.Logf("test add successfully")
}

posted @ 2022-03-05 12:18  ty1539  阅读(63)  评论(0编辑  收藏  举报