OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / stretchr / testify / suite / suite_test.go
1 package suite
2
3 import (
4         "errors"
5         "io/ioutil"
6         "os"
7         "testing"
8         "time"
9
10         "github.com/stretchr/testify/assert"
11         "github.com/stretchr/testify/require"
12 )
13
14 // SuiteRequireTwice is intended to test the usage of suite.Require in two
15 // different tests
16 type SuiteRequireTwice struct{ Suite }
17
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(
23                 allTestsFilter,
24                 []testing.InternalTest{{
25                         Name: "TestSuiteRequireTwice",
26                         F: func(t *testing.T) {
27                                 suite := new(SuiteRequireTwice)
28                                 Run(t, suite)
29                         },
30                 }},
31         )
32         assert.Equal(t, false, ok)
33 }
34
35 func (s *SuiteRequireTwice) TestRequireOne() {
36         r := s.Require()
37         r.Equal(1, 2)
38 }
39
40 func (s *SuiteRequireTwice) TestRequireTwo() {
41         r := s.Require()
42         r.Equal(1, 2)
43 }
44
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
50 // type of assertion.
51 type SuiteTester struct {
52         // Include our basic suite logic.
53         Suite
54
55         // Keep counts of how many times each method is run.
56         SetupSuiteRunCount    int
57         TearDownSuiteRunCount int
58         SetupTestRunCount     int
59         TearDownTestRunCount  int
60         TestOneRunCount       int
61         TestTwoRunCount       int
62         NonTestMethodRunCount int
63
64         SuiteNameBefore []string
65         TestNameBefore  []string
66
67         SuiteNameAfter []string
68         TestNameAfter  []string
69
70         TimeBefore []time.Time
71         TimeAfter  []time.Time
72 }
73
74 type SuiteSkipTester struct {
75         // Include our basic suite logic.
76         Suite
77
78         // Keep counts of how many times each method is run.
79         SetupSuiteRunCount    int
80         TearDownSuiteRunCount int
81 }
82
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++
87 }
88
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())
93 }
94
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())
99 }
100
101 func (suite *SuiteSkipTester) SetupSuite() {
102         suite.SetupSuiteRunCount++
103         suite.T().Skip()
104 }
105
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++
110 }
111
112 func (suite *SuiteSkipTester) TearDownSuite() {
113         suite.TearDownSuiteRunCount++
114 }
115
116 // The SetupTest method will be run before every test in the suite.
117 func (suite *SuiteTester) SetupTest() {
118         suite.SetupTestRunCount++
119 }
120
121 // The TearDownTest method will be run after every test in the suite.
122 func (suite *SuiteTester) TearDownTest() {
123         suite.TearDownTestRunCount++
124 }
125
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)
135 }
136
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)
143 }
144
145 func (suite *SuiteTester) TestSkip() {
146         suite.T().Skip()
147 }
148
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++
154 }
155
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)
160         Run(t, suiteTester)
161
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.
165
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)
170
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)
175
176         assert.Contains(t, suiteTester.TestNameAfter, "TestOne")
177         assert.Contains(t, suiteTester.TestNameAfter, "TestTwo")
178         assert.Contains(t, suiteTester.TestNameAfter, "TestSkip")
179
180         assert.Contains(t, suiteTester.TestNameBefore, "TestOne")
181         assert.Contains(t, suiteTester.TestNameBefore, "TestTwo")
182         assert.Contains(t, suiteTester.TestNameBefore, "TestSkip")
183
184         for _, suiteName := range suiteTester.SuiteNameAfter {
185                 assert.Equal(t, "SuiteTester", suiteName)
186         }
187
188         for _, suiteName := range suiteTester.SuiteNameBefore {
189                 assert.Equal(t, "SuiteTester", suiteName)
190         }
191
192         for _, when := range suiteTester.TimeAfter {
193                 assert.False(t, when.IsZero())
194         }
195
196         for _, when := range suiteTester.TimeBefore {
197                 assert.False(t, when.IsZero())
198         }
199
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)
205
206         // Each test should have been run once.
207         assert.Equal(t, suiteTester.TestOneRunCount, 1)
208         assert.Equal(t, suiteTester.TestTwoRunCount, 1)
209
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)
213
214         suiteSkipTester := new(SuiteSkipTester)
215         Run(t, suiteSkipTester)
216
217         // The suite was only run once, so the SetupSuite and TearDownSuite
218         // methods should have each been run only once, even though SetupSuite
219         // called Skip()
220         assert.Equal(t, suiteSkipTester.SetupSuiteRunCount, 1)
221         assert.Equal(t, suiteSkipTester.TearDownSuiteRunCount, 1)
222
223 }
224
225 func TestSuiteGetters(t *testing.T) {
226         suite := new(SuiteTester)
227         suite.SetT(t)
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())
232 }
233
234 type SuiteLoggingTester struct {
235         Suite
236 }
237
238 func (s *SuiteLoggingTester) TestLoggingPass() {
239         s.T().Log("TESTLOGPASS")
240 }
241
242 func (s *SuiteLoggingTester) TestLoggingFail() {
243         s.T().Log("TESTLOGFAIL")
244         assert.NotNil(s.T(), nil) // expected to fail
245 }
246
247 type StdoutCapture struct {
248         oldStdout *os.File
249         readPipe  *os.File
250 }
251
252 func (sc *StdoutCapture) StartCapture() {
253         sc.oldStdout = os.Stdout
254         sc.readPipe, os.Stdout, _ = os.Pipe()
255 }
256
257 func (sc *StdoutCapture) StopCapture() (string, error) {
258         if sc.oldStdout == nil || sc.readPipe == nil {
259                 return "", errors.New("StartCapture not called before StopCapture")
260         }
261         os.Stdout.Close()
262         os.Stdout = sc.oldStdout
263         bytes, err := ioutil.ReadAll(sc.readPipe)
264         if err != nil {
265                 return "", err
266         }
267         return string(bytes), nil
268 }
269
270 func TestSuiteLogging(t *testing.T) {
271         suiteLoggingTester := new(SuiteLoggingTester)
272         capture := StdoutCapture{}
273         internalTest := testing.InternalTest{
274                 Name: "SomeTest",
275                 F: func(subT *testing.T) {
276                         Run(subT, suiteLoggingTester)
277                 },
278         }
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")
284
285         // Failed tests' output is always printed
286         assert.Contains(t, output, "TESTLOGFAIL")
287
288         if testing.Verbose() {
289                 // In verbose mode, output from successful tests is also printed
290                 assert.Contains(t, output, "TESTLOGPASS")
291         } else {
292                 assert.NotContains(t, output, "TESTLOGPASS")
293         }
294 }