- 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版)
- Designing Purpose:Built Drones for Ardupilot Pixhawk 2.1
- Linux集群和自動化運維
- Linux操作系統應用編程
- Moodle 3.x Teaching Techniques(Third Edition)
- 新手學電腦從入門到精通(Windows 10+Office 2016版)
- 網絡操作系統管理與應用(第三版)
- 巧學活用Windows 7
- Cassandra 3.x High Availability(Second Edition)
- Learn CUDA Programming
- Agile IT Security Implementation Methodology
- Linux從入門到精通(視頻教學版)
- Microsoft DirectAccess Best Practices and Troubleshooting
- Hyper-V Replica Essentials
- Serverless Architectures with AWS