码迷,mamicode.com
首页 > 其他好文 > 详细

go之单元测试

时间:2018-11-27 19:34:30      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:package   0.00   cte   tin   success   单元测试   imp   i++   log   

cal.go

package testcase

func sum(n int) int{
	sum := 0
	for i:=1;i<=n;i++{
		sum += i
	}
	return sum
}

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

  

sum_test.go

package testcase

import "testing"

func TestSumCase(t *testing.T){
	res := sum(10)

	if res != 55{
		t.Fatalf("error occurred, expected %d, got %d",55,res)
	}
	t.Logf("successfully executed")
}

  

sub_test.go

package testcase

import "testing"

func TestSubCase(t *testing.T){
	res := sub(10, 3)
	if res != 7{
		t.Fatalf("error occurred,expected %d,got %d",7, res)
	}
	t.Logf("successfully executed")
}

进入testcase目录下,输入go test -v

C:\Users\Administrator\go\src\awesomeProject\testcase>go test -v
=== RUN   TestSubCase
--- PASS: TestSubCase (0.00s)
    sub_test.go:10: successfully executed
=== RUN   TestSumCase
--- PASS: TestSumCase (0.00s)
    sum_test.go:11: successfully executed
PASS
ok      awesomeProject/testcase 0.061s

  

这是测试全部,如果只是测试单个文件,go test -v sum_test.go cal.go

测试单个方法,go test -v -test.run TestSubCase

go之单元测试

标签:package   0.00   cte   tin   success   单元测试   imp   i++   log   

原文地址:https://www.cnblogs.com/traditional/p/10027983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!