LiteIDE 编写Go的单元测试

第一步:新建一个 package  "even"



第二步:编写代码

// even project even.go
package even

func Even(i int)bool{
	return i%2==0
}

func odd(i int)bool{
	return i%2==1
}

第三步:在包 even 下新建测试文件 even_test.go



第四步:编写测试代码

// even_test
package even

import (
	"testing"
)

func TestEven(t *testing.T){
	if !Even(2){
		t.Log("2 should be even")
		t.Fail()
	}
}

func TestOdd(t *testing.T){
	if !odd(2){
		t.Log("2 should not be odd")
		t.Fail()
	}
}
注意: 即使函数 odd 首写字母为小写,但是在测试文件中依然要写成 TestOdd !!!


第五步:运行测试

LiteIDE 编写Go的单元测试_第1张图片


你可能感兴趣的:(单元测试,测试,Go,testing)