- Distributed Computing with Go
- V.N. Nikhil Anurag
- 147字
- 2021-06-24 18:36:09
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) } }) } }
推薦閱讀
- 30天自制操作系統
- Linux設備驅動開發詳解:基于最新的Linux4.0內核
- Puppet實戰
- 大學計算機應用基礎實踐教程(Windows 7+Office 2013)
- BPEL and Java Cookbook
- VMware Horizon View 6 Desktop Virtualization Cookbook
- 新手易學:系統安裝與重裝
- Linux集群和自動化運維
- Android物聯網開發細致入門與最佳實踐
- Troubleshooting Docker
- Windows 7實戰從入門到精通
- Windows 7實戰從入門到精通(超值版)
- Linux從入門到精通(視頻教學版)
- Learning IBM Watson Analytics
- Implementing Domain-Specific Languages with Xtext and Xtend(Second Edition)