November 2018
Intermediate to advanced
346 pages
8h 12m
English
Often, while writing tests, you will find that multiple tests for the same method result in a lot of duplication. Take this example:
func TestRound_down(t *testing.T) { in := float64(1.1) expected := 1 result := Round(in) assert.Equal(t, expected, result)}func TestRound_up(t *testing.T) { in := float64(3.7) expected := 4 result := Round(in) assert.Equal(t, expected, result)}func TestRound_noChange(t *testing.T) { in := float64(6.0) expected := 6 result := Round(in) assert.Equal(t, expected, result)}
There is nothing surprising, nor wrong with the intent here. Table-driven tests acknowledge the need for duplication and extract the variations into a table. It is this table that then drives a single copy of the code that would ...
Read now
Unlock full access