fixture_test.go 14 KB

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