官术网_书友最值得收藏!

addInt_test.go

You might have also noticed in TestSimpleVariadicToSlice that we duplicated a lot of logic, while the only varying factor was the input and expected values. One style of testing, known as Table-driven development, defines a table of all the required data to run a test, iterates over the "rows" of the table and runs tests against them.

Let's look at the tests we will be testing against no arguments and variadic arguments:

// addInt_test.go 
 
package main 
 
import ( 
    "testing" 
) 
 
func TestAddInt(t *testing.T) { 
    testCases := []struct { 
        Name     string 
        Values   []int 
        Expected int 
    }{ 
        {"addInt() -> 0", []int{}, 0}, 
        {"addInt([]int{10, 20, 100}) -> 130", []int{10, 20, 100}, 130}, 
    } 
 
    for _, tc := range testCases { 
        t.Run(tc.Name, func(t *testing.T) { 
            sum := addInt(tc.Values...) 
            if sum != tc.Expected { 
                t.Errorf("%d != %d", sum, tc.Expected) 
            } else { 
                t.Logf("%d == %d", sum, tc.Expected) 
            } 
        }) 
    } 
} 
主站蜘蛛池模板: 台州市| 开阳县| 当雄县| 普安县| 林西县| 耒阳市| 泽库县| 铜山县| 莒南县| 陇南市| 沙河市| 闸北区| 沂南县| 逊克县| 新建县| 潮州市| 离岛区| 邮箱| 怀宁县| 滕州市| 祁阳县| 长沙县| 衡南县| 沛县| 锦州市| 蒙城县| 锡林郭勒盟| 泉州市| 中江县| 潼关县| 沾益县| 江门市| 青浦区| 泾川县| 万盛区| 祁阳县| 夏河县| 石狮市| 泽普县| 扶沟县| 太谷县|