golang测试

107 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路 golang测试某一个函数的性能和是否有bug
先在hello.go中

package main
func hello(n1 int,n2 int)int{
	return n1+n2
}

测试文件有一些规定

  • 测试文件,文件名需要***_test.go
  • 测试文件的测试函数需要是Test*(大写)**(t *testing.T)
    测试文件
    hello_test.go
package main
import "testing"
func TestHello(t *testing.T){
	res:=hello(1,2)
	if res!=3{
		t.Fatalf("have err")
	}else{
		t.Log("no err")
	}
}

运行测试文件

go test -v//无论是否出错,都会将结果输出
go test //如果结果正确,不会输出t.Log()函数中的内容