- 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) } }) } }
推薦閱讀
- Linux Mint Essentials
- 網(wǎng)絡(luò)操作系統(tǒng):Windows Server 2003管理與應(yīng)用
- Haskell Financial Data Modeling and Predictive Analytics
- 嵌入式應(yīng)用程序設(shè)計綜合教程(微課版)
- Installing and Configuring Windows 10:70-698 Exam Guide
- Windows Vista融會貫通
- Moodle 3.x Teaching Techniques(Third Edition)
- 8051軟核處理器設(shè)計實(shí)戰(zhàn)
- Fedora 12 Linux應(yīng)用基礎(chǔ)
- Vim 8文本處理實(shí)戰(zhàn)
- Linux內(nèi)核修煉之道
- BuddyPress Theme Development
- Mastering AWS CloudFormation
- Microsoft DirectAccess Best Practices and Troubleshooting
- 操作系統(tǒng)教程