Debian Codegraph @

fixture_test.go

raw symbols SCIP
1 // Tests for the behavior of the test fixture system.
2
3 package check_test
4
5 import (
6 . "github.com/pingcap/check"
7 )
8
9 // -----------------------------------------------------------------------
10 // Fixture test suite.
11
12 type FixtureS struct{}
13
14 var fixtureS = Suite(&FixtureS{})
15
16 func (s *FixtureS) TestCountSuite(c *C) {
17 suitesRun += 1
18 }
19
20 // -----------------------------------------------------------------------
21 // Basic fixture ordering verification.
22
23 func (s *FixtureS) TestOrder(c *C) {
24 helper := FixtureHelper{}
25 Run(&helper, nil)
26 c.Check(helper.calls[0], Equals, "SetUpSuite")
27 c.Check(helper.calls[1], Equals, "SetUpTest")
28 c.Check(helper.calls[2], Equals, "Test1")
29 c.Check(helper.calls[3], Equals, "TearDownTest")
30 c.Check(helper.calls[4], Equals, "SetUpTest")
31 c.Check(helper.calls[5], Equals, "Test2")
32 c.Check(helper.calls[6], Equals, "TearDownTest")
33 c.Check(helper.calls[7], Equals, "TearDownSuite")
34 c.Check(len(helper.calls), Equals, 8)
35 }
36
37 // -----------------------------------------------------------------------
38 // Check the behavior when panics occur within tests and fixtures.
39
40 func (s *FixtureS) TestPanicOnTest(c *C) {
41 helper := FixtureHelper{panicOn: "Test1"}
42 output := String{}
43 Run(&helper, &RunConf{Output: &output})
44 c.Check(helper.calls[0], Equals, "SetUpSuite")
45 c.Check(helper.calls[1], Equals, "SetUpTest")
46 c.Check(helper.calls[2], Equals, "Test1")
47 c.Check(helper.calls[3], Equals, "TearDownTest")
48 c.Check(helper.calls[4], Equals, "SetUpTest")
49 c.Check(helper.calls[5], Equals, "Test2")
50 c.Check(helper.calls[6], Equals, "TearDownTest")
51 c.Check(helper.calls[7], Equals, "TearDownSuite")
52 c.Check(len(helper.calls), Equals, 8)
53
54 expected := "^\n-+\n" +
55 "PANIC: check_test\\.go:[0-9]+: FixtureHelper.Test1\n\n" +
56 "\\.\\.\\. Panic: Test1 \\(PC=[xA-F0-9]+\\)\n\n" +
57 ".+:[0-9]+\n" +
58 " in (go)?panic\n" +
59 ".*check_test.go:[0-9]+\n" +
60 " in FixtureHelper.trace\n" +
61 ".*check_test.go:[0-9]+\n" +
62 " in FixtureHelper.Test1\n" +
63 "(.|\n)*$"
64
65 c.Check(output.value, Matches, expected)
66 }
67
68 func (s *FixtureS) TestPanicOnSetUpTest(c *C) {
69 helper := FixtureHelper{panicOn: "SetUpTest"}
70 output := String{}
71 Run(&helper, &RunConf{Output: &output})
72 c.Check(helper.calls[0], Equals, "SetUpSuite")
73 c.Check(helper.calls[1], Equals, "SetUpTest")
74 c.Check(helper.calls[2], Equals, "TearDownTest")
75 c.Check(helper.calls[3], Equals, "TearDownSuite")
76 c.Check(len(helper.calls), Equals, 4)
77
78 expected := "^\n-+\n" +
79 "PANIC: check_test\\.go:[0-9]+: " +
80 "FixtureHelper\\.SetUpTest\n\n" +
81 "\\.\\.\\. Panic: SetUpTest \\(PC=[xA-F0-9]+\\)\n\n" +
82 ".+:[0-9]+\n" +
83 " in (go)?panic\n" +
84 ".*check_test.go:[0-9]+\n" +
85 " in FixtureHelper.trace\n" +
86 ".*check_test.go:[0-9]+\n" +
87 " in FixtureHelper.SetUpTest\n" +
88 "(.|\n)*" +
89 "\n-+\n" +
90 "PANIC: check_test\\.go:[0-9]+: " +
91 "FixtureHelper\\.Test1\n\n" +
92 "\\.\\.\\. Panic: Fixture has panicked " +
93 "\\(see related PANIC\\)\n$"
94
95 c.Check(output.value, Matches, expected)
96 }
97
98 func (s *FixtureS) TestPanicOnTearDownTest(c *C) {
99 helper := FixtureHelper{panicOn: "TearDownTest"}
100 output := String{}
101 Run(&helper, &RunConf{Output: &output})
102 c.Check(helper.calls[0], Equals, "SetUpSuite")
103 c.Check(helper.calls[1], Equals, "SetUpTest")
104 c.Check(helper.calls[2], Equals, "Test1")
105 c.Check(helper.calls[3], Equals, "TearDownTest")
106 c.Check(helper.calls[4], Equals, "TearDownSuite")
107 c.Check(len(helper.calls), Equals, 5)
108
109 expected := "^\n-+\n" +
110 "PANIC: check_test\\.go:[0-9]+: " +
111 "FixtureHelper.TearDownTest\n\n" +
112 "\\.\\.\\. Panic: TearDownTest \\(PC=[xA-F0-9]+\\)\n\n" +
113 ".+:[0-9]+\n" +
114 " in (go)?panic\n" +
115 ".*check_test.go:[0-9]+\n" +
116 " in FixtureHelper.trace\n" +
117 ".*check_test.go:[0-9]+\n" +
118 " in FixtureHelper.TearDownTest\n" +
119 "(.|\n)*" +
120 "\n-+\n" +
121 "PANIC: check_test\\.go:[0-9]+: " +
122 "FixtureHelper\\.Test1\n\n" +
123 "\\.\\.\\. Panic: Fixture has panicked " +
124 "\\(see related PANIC\\)\n$"
125
126 c.Check(output.value, Matches, expected)
127 }
128
129 func (s *FixtureS) TestPanicOnSetUpSuite(c *C) {
130 helper := FixtureHelper{panicOn: "SetUpSuite"}
131 output := String{}
132 Run(&helper, &RunConf{Output: &output})
133 c.Check(helper.calls[0], Equals, "SetUpSuite")
134 c.Check(helper.calls[1], Equals, "TearDownSuite")
135 c.Check(len(helper.calls), Equals, 2)
136
137 expected := "^\n-+\n" +
138 "PANIC: check_test\\.go:[0-9]+: " +
139 "FixtureHelper.SetUpSuite\n\n" +
140 "\\.\\.\\. Panic: SetUpSuite \\(PC=[xA-F0-9]+\\)\n\n" +
141 ".+:[0-9]+\n" +
142 " in (go)?panic\n" +
143 ".*check_test.go:[0-9]+\n" +
144 " in FixtureHelper.trace\n" +
145 ".*check_test.go:[0-9]+\n" +
146 " in FixtureHelper.SetUpSuite\n" +
147 "(.|\n)*$"
148
149 c.Check(output.value, Matches, expected)
150 }
151
152 func (s *FixtureS) TestPanicOnTearDownSuite(c *C) {
153 helper := FixtureHelper{panicOn: "TearDownSuite"}
154 output := String{}
155 Run(&helper, &RunConf{Output: &output})
156 c.Check(helper.calls[0], Equals, "SetUpSuite")
157 c.Check(helper.calls[1], Equals, "SetUpTest")
158 c.Check(helper.calls[2], Equals, "Test1")
159 c.Check(helper.calls[3], Equals, "TearDownTest")
160 c.Check(helper.calls[4], Equals, "SetUpTest")
161 c.Check(helper.calls[5], Equals, "Test2")
162 c.Check(helper.calls[6], Equals, "TearDownTest")
163 c.Check(helper.calls[7], Equals, "TearDownSuite")
164 c.Check(len(helper.calls), Equals, 8)
165
166 expected := "^\n-+\n" +
167 "PANIC: check_test\\.go:[0-9]+: " +
168 "FixtureHelper.TearDownSuite\n\n" +
169 "\\.\\.\\. Panic: TearDownSuite \\(PC=[xA-F0-9]+\\)\n\n" +
170 ".+:[0-9]+\n" +
171 " in (go)?panic\n" +
172 ".*check_test.go:[0-9]+\n" +
173 " in FixtureHelper.trace\n" +
174 ".*check_test.go:[0-9]+\n" +
175 " in FixtureHelper.TearDownSuite\n" +
176 "(.|\n)*$"
177
178 c.Check(output.value, Matches, expected)
179 }
180
181 // -----------------------------------------------------------------------
182 // A wrong argument on a test or fixture will produce a nice error.
183
184 func (s *FixtureS) TestPanicOnWrongTestArg(c *C) {
185 helper := WrongTestArgHelper{}
186 output := String{}
187 Run(&helper, &RunConf{Output: &output})
188 c.Check(helper.calls[0], Equals, "SetUpSuite")
189 c.Check(helper.calls[1], Equals, "SetUpTest")
190 c.Check(helper.calls[2], Equals, "TearDownTest")
191 c.Check(helper.calls[3], Equals, "SetUpTest")
192 c.Check(helper.calls[4], Equals, "Test2")
193 c.Check(helper.calls[5], Equals, "TearDownTest")
194 c.Check(helper.calls[6], Equals, "TearDownSuite")
195 c.Check(len(helper.calls), Equals, 7)
196
197 expected := "^\n-+\n" +
198 "PANIC: fixture_test\\.go:[0-9]+: " +
199 "WrongTestArgHelper\\.Test1\n\n" +
200 "\\.\\.\\. Panic: WrongTestArgHelper\\.Test1 argument " +
201 "should be \\*check\\.C\n"
202
203 c.Check(output.value, Matches, expected)
204 }
205
206 func (s *FixtureS) TestPanicOnWrongSetUpTestArg(c *C) {
207 helper := WrongSetUpTestArgHelper{}
208 output := String{}
209 Run(&helper, &RunConf{Output: &output})
210 c.Check(len(helper.calls), Equals, 0)
211
212 expected :=
213 "^\n-+\n" +
214 "PANIC: fixture_test\\.go:[0-9]+: " +
215 "WrongSetUpTestArgHelper\\.SetUpTest\n\n" +
216 "\\.\\.\\. Panic: WrongSetUpTestArgHelper\\.SetUpTest argument " +
217 "should be \\*check\\.C\n"
218
219 c.Check(output.value, Matches, expected)
220 }
221
222 func (s *FixtureS) TestPanicOnWrongSetUpSuiteArg(c *C) {
223 helper := WrongSetUpSuiteArgHelper{}
224 output := String{}
225 Run(&helper, &RunConf{Output: &output})
226 c.Check(len(helper.calls), Equals, 0)
227
228 expected :=
229 "^\n-+\n" +
230 "PANIC: fixture_test\\.go:[0-9]+: " +
231 "WrongSetUpSuiteArgHelper\\.SetUpSuite\n\n" +
232 "\\.\\.\\. Panic: WrongSetUpSuiteArgHelper\\.SetUpSuite argument " +
233 "should be \\*check\\.C\n"
234
235 c.Check(output.value, Matches, expected)
236 }
237
238 // -----------------------------------------------------------------------
239 // Nice errors also when tests or fixture have wrong arg count.
240
241 func (s *FixtureS) TestPanicOnWrongTestArgCount(c *C) {
242 helper := WrongTestArgCountHelper{}
243 output := String{}
244 Run(&helper, &RunConf{Output: &output})
245 c.Check(helper.calls[0], Equals, "SetUpSuite")
246 c.Check(helper.calls[1], Equals, "SetUpTest")
247 c.Check(helper.calls[2], Equals, "TearDownTest")
248 c.Check(helper.calls[3], Equals, "SetUpTest")
249 c.Check(helper.calls[4], Equals, "Test2")
250 c.Check(helper.calls[5], Equals, "TearDownTest")
251 c.Check(helper.calls[6], Equals, "TearDownSuite")
252 c.Check(len(helper.calls), Equals, 7)
253
254 expected := "^\n-+\n" +
255 "PANIC: fixture_test\\.go:[0-9]+: " +
256 "WrongTestArgCountHelper\\.Test1\n\n" +
257 "\\.\\.\\. Panic: WrongTestArgCountHelper\\.Test1 argument " +
258 "should be \\*check\\.C\n"
259
260 c.Check(output.value, Matches, expected)
261 }
262
263 func (s *FixtureS) TestPanicOnWrongSetUpTestArgCount(c *C) {
264 helper := WrongSetUpTestArgCountHelper{}
265 output := String{}
266 Run(&helper, &RunConf{Output: &output})
267 c.Check(len(helper.calls), Equals, 0)
268
269 expected :=
270 "^\n-+\n" +
271 "PANIC: fixture_test\\.go:[0-9]+: " +
272 "WrongSetUpTestArgCountHelper\\.SetUpTest\n\n" +
273 "\\.\\.\\. Panic: WrongSetUpTestArgCountHelper\\.SetUpTest argument " +
274 "should be \\*check\\.C\n"
275
276 c.Check(output.value, Matches, expected)
277 }
278
279 func (s *FixtureS) TestPanicOnWrongSetUpSuiteArgCount(c *C) {
280 helper := WrongSetUpSuiteArgCountHelper{}
281 output := String{}
282 Run(&helper, &RunConf{Output: &output})
283 c.Check(len(helper.calls), Equals, 0)
284
285 expected :=
286 "^\n-+\n" +
287 "PANIC: fixture_test\\.go:[0-9]+: " +
288 "WrongSetUpSuiteArgCountHelper\\.SetUpSuite\n\n" +
289 "\\.\\.\\. Panic: WrongSetUpSuiteArgCountHelper" +
290 "\\.SetUpSuite argument should be \\*check\\.C\n"
291
292 c.Check(output.value, Matches, expected)
293 }
294
295 // -----------------------------------------------------------------------
296 // Helper test suites with wrong function arguments.
297
298 type WrongTestArgHelper struct {
299 FixtureHelper
300 }
301
302 func (s *WrongTestArgHelper) Test1(t int) {
303 }
304
305 type WrongSetUpTestArgHelper struct {
306 FixtureHelper
307 }
308
309 func (s *WrongSetUpTestArgHelper) SetUpTest(t int) {
310 }
311
312 type WrongSetUpSuiteArgHelper struct {
313 FixtureHelper
314 }
315
316 func (s *WrongSetUpSuiteArgHelper) SetUpSuite(t int) {
317 }
318
319 type WrongTestArgCountHelper struct {
320 FixtureHelper
321 }
322
323 func (s *WrongTestArgCountHelper) Test1(c *C, i int) {
324 }
325
326 type WrongSetUpTestArgCountHelper struct {
327 FixtureHelper
328 }
329
330 func (s *WrongSetUpTestArgCountHelper) SetUpTest(c *C, i int) {
331 }
332
333 type WrongSetUpSuiteArgCountHelper struct {
334 FixtureHelper
335 }
336
337 func (s *WrongSetUpSuiteArgCountHelper) SetUpSuite(c *C, i int) {
338 }
339
340 // -----------------------------------------------------------------------
341 // Ensure fixture doesn't run without tests.
342
343 type NoTestsHelper struct {
344 hasRun bool
345 }
346
347 func (s *NoTestsHelper) SetUpSuite(c *C) {
348 s.hasRun = true
349 }
350
351 func (s *NoTestsHelper) TearDownSuite(c *C) {
352 s.hasRun = true
353 }
354
355 func (s *FixtureS) TestFixtureDoesntRunWithoutTests(c *C) {
356 helper := NoTestsHelper{}
357 output := String{}
358 Run(&helper, &RunConf{Output: &output})
359 c.Check(helper.hasRun, Equals, false)
360 }
361
362 // -----------------------------------------------------------------------
363 // Verify that checks and assertions work correctly inside the fixture.
364
365 type FixtureCheckHelper struct {
366 fail string
367 completed bool
368 }
369
370 func (s *FixtureCheckHelper) SetUpSuite(c *C) {
371 switch s.fail {
372 case "SetUpSuiteAssert":
373 c.Assert(false, Equals, true)
374 case "SetUpSuiteCheck":
375 c.Check(false, Equals, true)
376 }
377 s.completed = true
378 }
379
380 func (s *FixtureCheckHelper) SetUpTest(c *C) {
381 switch s.fail {
382 case "SetUpTestAssert":
383 c.Assert(false, Equals, true)
384 case "SetUpTestCheck":
385 c.Check(false, Equals, true)
386 }
387 s.completed = true
388 }
389
390 func (s *FixtureCheckHelper) Test(c *C) {
391 // Do nothing.
392 }
393
394 func (s *FixtureS) TestSetUpSuiteCheck(c *C) {
395 helper := FixtureCheckHelper{fail: "SetUpSuiteCheck"}
396 output := String{}
397 Run(&helper, &RunConf{Output: &output})
398 c.Assert(output.value, Matches,
399 "\n---+\n"+
400 "FAIL: fixture_test\\.go:[0-9]+: "+
401 "FixtureCheckHelper\\.SetUpSuite\n\n"+
402 "fixture_test\\.go:[0-9]+:\n"+
403 " c\\.Check\\(false, Equals, true\\)\n"+
404 "\\.+ obtained bool = false\n"+
405 "\\.+ expected bool = true\n\n")
406 c.Assert(helper.completed, Equals, true)
407 }
408
409 func (s *FixtureS) TestSetUpSuiteAssert(c *C) {
410 helper := FixtureCheckHelper{fail: "SetUpSuiteAssert"}
411 output := String{}
412 Run(&helper, &RunConf{Output: &output})
413 c.Assert(output.value, Matches,
414 "\n---+\n"+
415 "FAIL: fixture_test\\.go:[0-9]+: "+
416 "FixtureCheckHelper\\.SetUpSuite\n\n"+
417 "fixture_test\\.go:[0-9]+:\n"+
418 " c\\.Assert\\(false, Equals, true\\)\n"+
419 "\\.+ obtained bool = false\n"+
420 "\\.+ expected bool = true\n\n")
421 c.Assert(helper.completed, Equals, false)
422 }
423
424 // -----------------------------------------------------------------------
425 // Verify that logging within SetUpTest() persists within the test log itself.
426
427 type FixtureLogHelper struct {
428 c *C
429 }
430
431 func (s *FixtureLogHelper) SetUpTest(c *C) {
432 s.c = c
433 c.Log("1")
434 }
435
436 func (s *FixtureLogHelper) Test(c *C) {
437 c.Log("2")
438 s.c.Log("3")
439 c.Log("4")
440 c.Fail()
441 }
442
443 func (s *FixtureLogHelper) TearDownTest(c *C) {
444 s.c.Log("5")
445 }
446
447 func (s *FixtureS) TestFixtureLogging(c *C) {
448 helper := FixtureLogHelper{}
449 output := String{}
450 Run(&helper, &RunConf{Output: &output})
451 c.Assert(output.value, Matches,
452 "\n---+\n"+
453 "FAIL: fixture_test\\.go:[0-9]+: "+
454 "FixtureLogHelper\\.Test\n\n"+
455 "1\n2\n3\n4\n5\n")
456 }
457
458 // -----------------------------------------------------------------------
459 // Skip() within fixture methods.
460
461 func (s *FixtureS) TestSkipSuite(c *C) {
462 helper := FixtureHelper{skip: true, skipOnN: 0}
463 output := String{}
464 result := Run(&helper, &RunConf{Output: &output})
465 c.Assert(output.value, Equals, "")
466 c.Assert(helper.calls[0], Equals, "SetUpSuite")
467 c.Assert(helper.calls[1], Equals, "TearDownSuite")
468 c.Assert(len(helper.calls), Equals, 2)
469 c.Assert(result.Skipped, Equals, 2)
470 }
471
472 func (s *FixtureS) TestSkipTest(c *C) {
473 helper := FixtureHelper{skip: true, skipOnN: 1}
474 output := String{}
475 result := Run(&helper, &RunConf{Output: &output})
476 c.Assert(helper.calls[0], Equals, "SetUpSuite")
477 c.Assert(helper.calls[1], Equals, "SetUpTest")
478 c.Assert(helper.calls[2], Equals, "SetUpTest")
479 c.Assert(helper.calls[3], Equals, "Test2")
480 c.Assert(helper.calls[4], Equals, "TearDownTest")
481 c.Assert(helper.calls[5], Equals, "TearDownSuite")
482 c.Assert(len(helper.calls), Equals, 6)
483 c.Assert(result.Skipped, Equals, 1)
484 }