Creating subtests
In some cases, it is useful to create a set of tests that could have a similar setup or clean-up code. This could be done without having a separate function for each test.
How to do it...
- Open the console and create the folder
chapter11/recipe04
. - Navigate to the directory.
- Create the file
sample_test.go
with the following content:
package main import ( "fmt" "strconv" "testing" ) var testData = []int{10, 11, 017} func TestSampleOne(t *testing.T) { expected := "10" for _, val := range testData { tc := val t.Run(fmt.Sprintf("input = %d", tc), func(t *testing.T) { if expected != strconv.Itoa(tc) { t.Fail() } }) } }
- Execute the tests by
go test -v
. - See the output in the Terminal:

How it works...
The T
struct of the testing
package also provides the Run
method that could be used to run the nested...