- 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) } }) } }
推薦閱讀
- 每天5分鐘玩轉(zhuǎn)Kubernetes
- Google系統(tǒng)架構(gòu)解密:構(gòu)建安全可靠的系統(tǒng)
- Windows Server 2012 Hyper-V Cookbook
- Linux集群和自動(dòng)化運(yùn)維
- 計(jì)算機(jī)系統(tǒng)開(kāi)發(fā)與優(yōu)化實(shí)戰(zhàn)
- macOS效率手冊(cè)
- 嵌入式實(shí)時(shí)操作系統(tǒng):RT-Thread設(shè)計(jì)與實(shí)現(xiàn)
- Ceph分布式存儲(chǔ)實(shí)戰(zhàn)
- 從實(shí)踐中學(xué)習(xí)Kali Linux無(wú)線網(wǎng)絡(luò)滲透測(cè)試
- Advanced TypeScript Programming Projects
- HTML5 Enterprise Application Development
- Linux從入門到精通(視頻教學(xué)版)
- Docker容器技術(shù)與應(yīng)用
- Linux系統(tǒng)安全:縱深防御、安全掃描與入侵檢測(cè)
- 操作系統(tǒng)之哲學(xué)原理第2版