Skip to content

Instantly share code, notes, and snippets.

@pataiji
Created May 27, 2020 01:52
Show Gist options
  • Save pataiji/e4173371155804b61f3a7230c486a194 to your computer and use it in GitHub Desktop.
Save pataiji/e4173371155804b61f3a7230c486a194 to your computer and use it in GitHub Desktop.
github.com/stretchr/testify/suite setup and teardown order
$ go test -v ./tests/example/
=== RUN TestExampleTestSuite
TestExampleTestSuite: example_test.go:14: SetupSuite
=== RUN TestExampleTestSuite/TestExample1
TestExampleTestSuite/TestExample1: example_test.go:18: SetupTest
TestExampleTestSuite/TestExample1: example_test.go:22: BeforeTest suiteName=ExampleTestSuite testName=TestExample1
TestExampleTestSuite/TestExample1: example_test.go:26: TestExample1
TestExampleTestSuite/TestExample1: example_test.go:34: AfterTest suiteName=ExampleTestSuite testName=TestExample1
TestExampleTestSuite/TestExample1: example_test.go:38: TearDownTest
=== RUN TestExampleTestSuite/TestExample2
TestExampleTestSuite/TestExample2: example_test.go:18: SetupTest
TestExampleTestSuite/TestExample2: example_test.go:22: BeforeTest suiteName=ExampleTestSuite testName=TestExample2
TestExampleTestSuite/TestExample2: example_test.go:30: TestExample2
TestExampleTestSuite/TestExample2: example_test.go:34: AfterTest suiteName=ExampleTestSuite testName=TestExample2
TestExampleTestSuite/TestExample2: example_test.go:38: TearDownTest
TestExampleTestSuite: example_test.go:42: TearDownSuite
--- PASS: TestExampleTestSuite (0.00s)
--- PASS: TestExampleTestSuite/TestExample1 (0.00s)
--- PASS: TestExampleTestSuite/TestExample2 (0.00s)
PASS
ok github.com/pataiji/golang-sample/tests/example 0.945s
package example
import (
"testing"
"github.com/stretchr/testify/suite"
)
type ExampleTestSuite struct {
suite.Suite
}
func (s *ExampleTestSuite) SetupSuite() {
s.T().Log("SetupSuite")
}
func (s *ExampleTestSuite) SetupTest() {
s.T().Log("SetupTest")
}
func (s *ExampleTestSuite) BeforeTest(suiteName, testName string) {
s.T().Logf("BeforeTest suiteName=%v testName=%v", suiteName, testName)
}
func (s *ExampleTestSuite) TestExample1() {
s.T().Log("TestExample1")
}
func (s *ExampleTestSuite) TestExample2() {
s.T().Log("TestExample2")
}
func (s *ExampleTestSuite) AfterTest(suiteName, testName string) {
s.T().Logf("AfterTest suiteName=%v testName=%v", suiteName, testName)
}
func (s *ExampleTestSuite) TearDownTest() {
s.T().Log("TearDownTest")
}
func (s *ExampleTestSuite) TearDownSuite() {
s.T().Log("TearDownSuite")
}
func TestExampleTestSuite(t *testing.T) {
suite.Run(t, new(ExampleTestSuite))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment