March 2019
Intermediate to advanced
336 pages
9h 9m
English
Tests are driven by a test table. The following code snippet shows how a test table can be used:
//main package has examples shown// in Go Data Structures and algorithms bookpackage main// importing testing packagesimport ( "testing")func TestAddition(test *testing.T) { cases := []struct{ integer1 , integer2 , resultSum int }{ {1, 1, 2}, {1, -1, 0}, {1, 0, 1}, {0, 0, 0}, {3, 2, 1}, } for _, cas := range cases { var sum int var expected int sum = cas.integer1 + cas.integer2 expected = cas.resultSum if sum != expected { test.Errorf("%d + %d = %d, expected %d", cas.integer1, cas.integer2, sum, expected) } }}
Run the following command to execute the preceding code snippet:
go test -run TestAddition -v
The output is as follows: ...
Read now
Unlock full access