10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
14 // SuiteRequireTwice is intended to test the usage of suite.Require in two
16 type SuiteRequireTwice struct{ Suite }
18 // TestSuiteRequireTwice checks for regressions of issue #149 where
19 // suite.requirements was not initialised in suite.SetT()
20 // A regression would result on these tests panicking rather than failing.
21 func TestSuiteRequireTwice(t *testing.T) {
22 ok := testing.RunTests(
24 []testing.InternalTest{{
25 Name: "TestSuiteRequireTwice",
26 F: func(t *testing.T) {
27 suite := new(SuiteRequireTwice)
32 assert.Equal(t, false, ok)
35 func (s *SuiteRequireTwice) TestRequireOne() {
40 func (s *SuiteRequireTwice) TestRequireTwo() {
45 // This suite is intended to store values to make sure that only
46 // testing-suite-related methods are run. It's also a fully
47 // functional example of a testing suite, using setup/teardown methods
48 // and a helper method that is ignored by testify. To make this look
49 // more like a real world example, all tests in the suite perform some
51 type SuiteTester struct {
52 // Include our basic suite logic.
55 // Keep counts of how many times each method is run.
56 SetupSuiteRunCount int
57 TearDownSuiteRunCount int
59 TearDownTestRunCount int
62 NonTestMethodRunCount int
64 SuiteNameBefore []string
65 TestNameBefore []string
67 SuiteNameAfter []string
68 TestNameAfter []string
70 TimeBefore []time.Time
74 type SuiteSkipTester struct {
75 // Include our basic suite logic.
78 // Keep counts of how many times each method is run.
79 SetupSuiteRunCount int
80 TearDownSuiteRunCount int
83 // The SetupSuite method will be run by testify once, at the very
84 // start of the testing suite, before any tests are run.
85 func (suite *SuiteTester) SetupSuite() {
86 suite.SetupSuiteRunCount++
89 func (suite *SuiteTester) BeforeTest(suiteName, testName string) {
90 suite.SuiteNameBefore = append(suite.SuiteNameBefore, suiteName)
91 suite.TestNameBefore = append(suite.TestNameBefore, testName)
92 suite.TimeBefore = append(suite.TimeBefore, time.Now())
95 func (suite *SuiteTester) AfterTest(suiteName, testName string) {
96 suite.SuiteNameAfter = append(suite.SuiteNameAfter, suiteName)
97 suite.TestNameAfter = append(suite.TestNameAfter, testName)
98 suite.TimeAfter = append(suite.TimeAfter, time.Now())
101 func (suite *SuiteSkipTester) SetupSuite() {
102 suite.SetupSuiteRunCount++
106 // The TearDownSuite method will be run by testify once, at the very
107 // end of the testing suite, after all tests have been run.
108 func (suite *SuiteTester) TearDownSuite() {
109 suite.TearDownSuiteRunCount++
112 func (suite *SuiteSkipTester) TearDownSuite() {
113 suite.TearDownSuiteRunCount++
116 // The SetupTest method will be run before every test in the suite.
117 func (suite *SuiteTester) SetupTest() {
118 suite.SetupTestRunCount++
121 // The TearDownTest method will be run after every test in the suite.
122 func (suite *SuiteTester) TearDownTest() {
123 suite.TearDownTestRunCount++
126 // Every method in a testing suite that begins with "Test" will be run
127 // as a test. TestOne is an example of a test. For the purposes of
128 // this example, we've included assertions in the tests, since most
129 // tests will issue assertions.
130 func (suite *SuiteTester) TestOne() {
131 beforeCount := suite.TestOneRunCount
132 suite.TestOneRunCount++
133 assert.Equal(suite.T(), suite.TestOneRunCount, beforeCount+1)
134 suite.Equal(suite.TestOneRunCount, beforeCount+1)
137 // TestTwo is another example of a test.
138 func (suite *SuiteTester) TestTwo() {
139 beforeCount := suite.TestTwoRunCount
140 suite.TestTwoRunCount++
141 assert.NotEqual(suite.T(), suite.TestTwoRunCount, beforeCount)
142 suite.NotEqual(suite.TestTwoRunCount, beforeCount)
145 func (suite *SuiteTester) TestSkip() {
149 // NonTestMethod does not begin with "Test", so it will not be run by
150 // testify as a test in the suite. This is useful for creating helper
151 // methods for your tests.
152 func (suite *SuiteTester) NonTestMethod() {
153 suite.NonTestMethodRunCount++
156 // TestRunSuite will be run by the 'go test' command, so within it, we
157 // can run our suite using the Run(*testing.T, TestingSuite) function.
158 func TestRunSuite(t *testing.T) {
159 suiteTester := new(SuiteTester)
162 // Normally, the test would end here. The following are simply
163 // some assertions to ensure that the Run function is working as
164 // intended - they are not part of the example.
166 // The suite was only run once, so the SetupSuite and TearDownSuite
167 // methods should have each been run only once.
168 assert.Equal(t, suiteTester.SetupSuiteRunCount, 1)
169 assert.Equal(t, suiteTester.TearDownSuiteRunCount, 1)
171 assert.Equal(t, len(suiteTester.SuiteNameAfter), 3)
172 assert.Equal(t, len(suiteTester.SuiteNameBefore), 3)
173 assert.Equal(t, len(suiteTester.TestNameAfter), 3)
174 assert.Equal(t, len(suiteTester.TestNameBefore), 3)
176 assert.Contains(t, suiteTester.TestNameAfter, "TestOne")
177 assert.Contains(t, suiteTester.TestNameAfter, "TestTwo")
178 assert.Contains(t, suiteTester.TestNameAfter, "TestSkip")
180 assert.Contains(t, suiteTester.TestNameBefore, "TestOne")
181 assert.Contains(t, suiteTester.TestNameBefore, "TestTwo")
182 assert.Contains(t, suiteTester.TestNameBefore, "TestSkip")
184 for _, suiteName := range suiteTester.SuiteNameAfter {
185 assert.Equal(t, "SuiteTester", suiteName)
188 for _, suiteName := range suiteTester.SuiteNameBefore {
189 assert.Equal(t, "SuiteTester", suiteName)
192 for _, when := range suiteTester.TimeAfter {
193 assert.False(t, when.IsZero())
196 for _, when := range suiteTester.TimeBefore {
197 assert.False(t, when.IsZero())
200 // There are three test methods (TestOne, TestTwo, and TestSkip), so
201 // the SetupTest and TearDownTest methods (which should be run once for
202 // each test) should have been run three times.
203 assert.Equal(t, suiteTester.SetupTestRunCount, 3)
204 assert.Equal(t, suiteTester.TearDownTestRunCount, 3)
206 // Each test should have been run once.
207 assert.Equal(t, suiteTester.TestOneRunCount, 1)
208 assert.Equal(t, suiteTester.TestTwoRunCount, 1)
210 // Methods that don't match the test method identifier shouldn't
211 // have been run at all.
212 assert.Equal(t, suiteTester.NonTestMethodRunCount, 0)
214 suiteSkipTester := new(SuiteSkipTester)
215 Run(t, suiteSkipTester)
217 // The suite was only run once, so the SetupSuite and TearDownSuite
218 // methods should have each been run only once, even though SetupSuite
220 assert.Equal(t, suiteSkipTester.SetupSuiteRunCount, 1)
221 assert.Equal(t, suiteSkipTester.TearDownSuiteRunCount, 1)
225 func TestSuiteGetters(t *testing.T) {
226 suite := new(SuiteTester)
228 assert.NotNil(t, suite.Assert())
229 assert.Equal(t, suite.Assertions, suite.Assert())
230 assert.NotNil(t, suite.Require())
231 assert.Equal(t, suite.require, suite.Require())
234 type SuiteLoggingTester struct {
238 func (s *SuiteLoggingTester) TestLoggingPass() {
239 s.T().Log("TESTLOGPASS")
242 func (s *SuiteLoggingTester) TestLoggingFail() {
243 s.T().Log("TESTLOGFAIL")
244 assert.NotNil(s.T(), nil) // expected to fail
247 type StdoutCapture struct {
252 func (sc *StdoutCapture) StartCapture() {
253 sc.oldStdout = os.Stdout
254 sc.readPipe, os.Stdout, _ = os.Pipe()
257 func (sc *StdoutCapture) StopCapture() (string, error) {
258 if sc.oldStdout == nil || sc.readPipe == nil {
259 return "", errors.New("StartCapture not called before StopCapture")
262 os.Stdout = sc.oldStdout
263 bytes, err := ioutil.ReadAll(sc.readPipe)
267 return string(bytes), nil
270 func TestSuiteLogging(t *testing.T) {
271 suiteLoggingTester := new(SuiteLoggingTester)
272 capture := StdoutCapture{}
273 internalTest := testing.InternalTest{
275 F: func(subT *testing.T) {
276 Run(subT, suiteLoggingTester)
279 capture.StartCapture()
280 testing.RunTests(allTestsFilter, []testing.InternalTest{internalTest})
281 output, err := capture.StopCapture()
282 require.NoError(t, err, "Got an error trying to capture stdout and stderr!")
283 require.NotEmpty(t, output, "output content must not be empty")
285 // Failed tests' output is always printed
286 assert.Contains(t, output, "TESTLOGFAIL")
288 if testing.Verbose() {
289 // In verbose mode, output from successful tests is also printed
290 assert.Contains(t, output, "TESTLOGPASS")
292 assert.NotContains(t, output, "TESTLOGPASS")