Golang 测试

go的一些测试的方法,懒得跑结果了, 直接上源代码, 心累!

package main

/**
go 测试相关的一些测试方法
	 性能测试
	 超时测试
	 基准测试
	 内容正确性测试
	 代码覆盖测试
**/

// 代码覆盖测试
// go test -cover -coverprofile=cover.out -covermode=count
// go tool -cover -html=cover.out 以html 页面打开结果
import (
	"os"
	"testing"
	"time"
)

func TestA(t *testing.T)     {}
func TestB(t *testing.T)     {}
func Benchmark(t *testing.B) {}

func sum(n ...int) int {
	var ret int
	ret = 0

	for _, i := range n {
		ret += i
	}
	return ret
}

// go test -v -bench .
// go test -v -bench . -benchmem -cpu 1,2,4 -benchtime 30s
func BenchmakSum(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if sum(1, 2, 3) != 6 {
			b.Fatal("sum fatal error")
		}
	}
}

// benchmark test   go test -v -test.bench .
func TestMain(m *testing.M) {
	println("Setup")
	code := m.Run()
	println("teardown")
	os.Exit(code)
}

// go test -v -run "(?i)sum"
func TestSum(t *testing.T) {
	time.Sleep(time.Second * 2)
	if sum(1, 2, 3) != 6 {
		t.Fatal("sum error")
	}
}

// timeout  test go test -v -timeout 3s
func TestTimeout(t *testing.T) {
	time.Sleep(time.Second * 5)
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值