Go 表格驱动测试

简单的测试用例:

calcTriangle.go

// 需要被测试的函数
func calcTriangle(a, b int) int {
    return int(math.Sqrt(float64(a*a + b*b)))
}

calcTriangle_test.go   // 注意测试文件必须以_test结尾

package main

import "testing"

// 注意测试函数必须以 Test开头 func TestTriangle(t
*testing.T) { tests := []struct {a, b, c int} { {3,4,5}, {5,12,13}, {8,15,17}, {12,35,37}, {30000,40000,50000}, } for _, tt := range tests { if actual := calcTriangle(tt.a, tt.b); actual != tt.c { t.Errorf("calcTriangle(%d, %d); got %d; expected %d", tt.a, tt.b, actual, tt.c) } } }

测试执行方式:

1、IDE中直接执行

2、命令行 go test .

猜你喜欢

转载自www.cnblogs.com/vincenshen/p/9319893.html
今日推荐