Onepager: testify test suite

stretchr: testify…

… is used in many projects, minimal for assertions, but also it is really useful for mocks and testsuites

https://github.com/stretchr/testify

Even when the README contains all information, this is my one-pager to quickstart start a new testfile

type ToTestTestSuite struct {
	suite.Suite
}

func (s *ToTestTestSuite) SetupTest() {}
func (s *ToTestTestSuite) AfterTest() {}
func (s *ToTestTestSuite) BeforeTest(suiteName, testName string) {}
func (s *ToTestTestSuite) TearDownTest() {}


type DependencyMock struct {
	mock.Mock
}

func (a *DependencyMock) MockedFunc(someArgs string) (string, error) {
	args := a.Called(someArgs)
	return args.String(0), args.Error(1)
}

func (s *ToTestTestSuite) TestAllTheThings() {
	depMock := new(DependencyMock)
	depMock.On("MockedFunc", "much hard").Return("have a break", nil)
	depMock.On("MockedFunc", mock.Anything).Return("", errors.New("too hard"))

	service := &Service{
		Dependency: depMock,
	}

	result, err := service.DoHardWork("much hard")
	require.Nil(s.T(), err)
	require.Equal(s.T(), "have a break", result)
	depMock.AssertExpectations(s.T())
}

func TestToTestTestSuite(t *testing.T) {
	suite.Run(t, new(ToTestTestSuite))
}

Last posts

Tags