go-单元测试

待测试的代码

package main

import "errors"

func Division(a int32, b int32) (int32, error) {
    if 0 == b {
        return -1, errors.New("division by zero")
    }
    return a / b, nil
}

单元测试与性能测试

1、单元测试函数必须以Test开头,入参为*testing.T

    单元测试命令 go test

2、性能测试函数必须赢Benchmark开头,入参为*testing.B

   性能测试 go test -test.Bench ".*" -count=5 // 测试所有性能函数,测试测试为5次

package main

import "testing"

func Test_Division_1(t *testing.T) {
	if i, _ := Division(4, 6); i != 0 {
		t.Error("测试失败")
	} else {
		t.Log("测试成功")
	}
}

func Benchmark_Division(b *testing.B) {
	for i := 0; i < b.N; i++ {
		Division(4, 6)
	}
}

猜你喜欢

转载自www.cnblogs.com/zengyjun/p/10232037.html