A test suite groups related tests that share setup or teardown, so each case can focus on behavior instead of repeating fixture and mock initialization. section 6.5.1
In Go, Testify wraps standard testing in a suite struct. SetupTest runs before each test; setup and teardown hooks reduce duplication and can reset mocks. section 6.5.1
Testing a translation client
A remote-service suite initializes a mock client and service for each test. A client suite starts a fake HTTP server and tests successful, missing-result, and error responses. section 6.5.1 section 6.6
Listing 6.13 remote_translator_test.go
package translation_test
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/holmes89/hello-api/translation"
)
func TestRemoteServiceTestSuite(t *testing.T) { #1
suite.Run(t, new(RemoteServiceTestSuite))
}
type RemoteServiceTestSuite struct { #2
suite.Suite #3
client *MockHelloClient
underTest *translation.RemoteService
}
func (suite *RemoteServiceTestSuite) SetupTest() { #4
suite.client = new(MockHelloClient)
suite.underTest = translation.NewRemoteService(suite.client)
}
type MockHelloClient struct { #5
mock.Mock #6
}
func (m *MockHelloClient) Translate(word, language string) (string, error) { #7
args := m.Called(word, language) #8
return args.String(0), args.Error(1) #9
}
See how a Testify suite embeds suite.Suite and initializes its mock client and service in SetupTest. section 6.5.1










