TIL: Go subtests
2021-12-06 00:00:00 +0000 UTCTo handle tests that require shared setup, Go allows the use of subtests. The type testing.T
that lives in the Go testing module has a T.Run
method.
The T.Run
method has this signature:
func (t *T) Run(name string, f func(t *T)) bool
The first argument is a name for the subtest. The second argument is typically passed as an anonymous function as below:
func TestMainTest(t *testing.T) {
sharedResource := NewResource(some, arguments)
t.Run("_FirstSubtest", func(t *testing.T) {
// test here
})
t.Run("_SecondSubtest", func(t *testing.T) {
// test here
})
sharedResource.TearDown()
}