GO学习笔记(17) -测试

2021-07-12  本文已影响0人  卡门001

测试工具 —go test

测试规范

命令操作

go test -bench=.*
go test -v -test.run TestQueryYellowPagesMessageWithStaff
go test

测试方法—格驱动测试

package tests

import (
    "math"
    "testing"
)

//表格驱动设计
func TestTriangle(t *testing.T)  {
    tests := [] struct{a,b,c int}{
        {1,2,3},
        {3,4,5},
        {0,0,0},
        {4,5,6},
        {100,200,20000},
    }

    for _, tt := range tests{
        if actual := calcTriangle(tt.a,tt.b); actual != tt.c{
            t.Error(" calcTriangle(tt.%d,tt.%d); got %d ;expected %d",
            tt.a,tt.b,actual,tt.c)
        }
    }
}


func calcTriangle(a, b int) int {
    var c int
    c = int(math.Sqrt(float64(a*a + b*b)))
    return c
}

上一篇 下一篇

猜你喜欢

热点阅读