run_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // These tests verify the test running logic.
  2. package check_test
  3. import (
  4. "errors"
  5. . "gopkg.in/check.v1"
  6. "os"
  7. "sync"
  8. )
  9. var runnerS = Suite(&RunS{})
  10. type RunS struct{}
  11. func (s *RunS) TestCountSuite(c *C) {
  12. suitesRun += 1
  13. }
  14. // -----------------------------------------------------------------------
  15. // Tests ensuring result counting works properly.
  16. func (s *RunS) TestSuccess(c *C) {
  17. output := String{}
  18. result := Run(&SuccessHelper{}, &RunConf{Output: &output})
  19. c.Check(result.Succeeded, Equals, 1)
  20. c.Check(result.Failed, Equals, 0)
  21. c.Check(result.Skipped, Equals, 0)
  22. c.Check(result.Panicked, Equals, 0)
  23. c.Check(result.FixturePanicked, Equals, 0)
  24. c.Check(result.Missed, Equals, 0)
  25. c.Check(result.RunError, IsNil)
  26. }
  27. func (s *RunS) TestFailure(c *C) {
  28. output := String{}
  29. result := Run(&FailHelper{}, &RunConf{Output: &output})
  30. c.Check(result.Succeeded, Equals, 0)
  31. c.Check(result.Failed, Equals, 1)
  32. c.Check(result.Skipped, Equals, 0)
  33. c.Check(result.Panicked, Equals, 0)
  34. c.Check(result.FixturePanicked, Equals, 0)
  35. c.Check(result.Missed, Equals, 0)
  36. c.Check(result.RunError, IsNil)
  37. }
  38. func (s *RunS) TestFixture(c *C) {
  39. output := String{}
  40. result := Run(&FixtureHelper{}, &RunConf{Output: &output})
  41. c.Check(result.Succeeded, Equals, 2)
  42. c.Check(result.Failed, Equals, 0)
  43. c.Check(result.Skipped, Equals, 0)
  44. c.Check(result.Panicked, Equals, 0)
  45. c.Check(result.FixturePanicked, Equals, 0)
  46. c.Check(result.Missed, Equals, 0)
  47. c.Check(result.RunError, IsNil)
  48. }
  49. func (s *RunS) TestPanicOnTest(c *C) {
  50. output := String{}
  51. helper := &FixtureHelper{panicOn: "Test1"}
  52. result := Run(helper, &RunConf{Output: &output})
  53. c.Check(result.Succeeded, Equals, 1)
  54. c.Check(result.Failed, Equals, 0)
  55. c.Check(result.Skipped, Equals, 0)
  56. c.Check(result.Panicked, Equals, 1)
  57. c.Check(result.FixturePanicked, Equals, 0)
  58. c.Check(result.Missed, Equals, 0)
  59. c.Check(result.RunError, IsNil)
  60. }
  61. func (s *RunS) TestPanicOnSetUpTest(c *C) {
  62. output := String{}
  63. helper := &FixtureHelper{panicOn: "SetUpTest"}
  64. result := Run(helper, &RunConf{Output: &output})
  65. c.Check(result.Succeeded, Equals, 0)
  66. c.Check(result.Failed, Equals, 0)
  67. c.Check(result.Skipped, Equals, 0)
  68. c.Check(result.Panicked, Equals, 0)
  69. c.Check(result.FixturePanicked, Equals, 1)
  70. c.Check(result.Missed, Equals, 2)
  71. c.Check(result.RunError, IsNil)
  72. }
  73. func (s *RunS) TestPanicOnSetUpSuite(c *C) {
  74. output := String{}
  75. helper := &FixtureHelper{panicOn: "SetUpSuite"}
  76. result := Run(helper, &RunConf{Output: &output})
  77. c.Check(result.Succeeded, Equals, 0)
  78. c.Check(result.Failed, Equals, 0)
  79. c.Check(result.Skipped, Equals, 0)
  80. c.Check(result.Panicked, Equals, 0)
  81. c.Check(result.FixturePanicked, Equals, 1)
  82. c.Check(result.Missed, Equals, 2)
  83. c.Check(result.RunError, IsNil)
  84. }
  85. // -----------------------------------------------------------------------
  86. // Check result aggregation.
  87. func (s *RunS) TestAdd(c *C) {
  88. result := &Result{
  89. Succeeded: 1,
  90. Skipped: 2,
  91. Failed: 3,
  92. Panicked: 4,
  93. FixturePanicked: 5,
  94. Missed: 6,
  95. ExpectedFailures: 7,
  96. }
  97. result.Add(&Result{
  98. Succeeded: 10,
  99. Skipped: 20,
  100. Failed: 30,
  101. Panicked: 40,
  102. FixturePanicked: 50,
  103. Missed: 60,
  104. ExpectedFailures: 70,
  105. })
  106. c.Check(result.Succeeded, Equals, 11)
  107. c.Check(result.Skipped, Equals, 22)
  108. c.Check(result.Failed, Equals, 33)
  109. c.Check(result.Panicked, Equals, 44)
  110. c.Check(result.FixturePanicked, Equals, 55)
  111. c.Check(result.Missed, Equals, 66)
  112. c.Check(result.ExpectedFailures, Equals, 77)
  113. c.Check(result.RunError, IsNil)
  114. }
  115. // -----------------------------------------------------------------------
  116. // Check the Passed() method.
  117. func (s *RunS) TestPassed(c *C) {
  118. c.Assert((&Result{}).Passed(), Equals, true)
  119. c.Assert((&Result{Succeeded: 1}).Passed(), Equals, true)
  120. c.Assert((&Result{Skipped: 1}).Passed(), Equals, true)
  121. c.Assert((&Result{Failed: 1}).Passed(), Equals, false)
  122. c.Assert((&Result{Panicked: 1}).Passed(), Equals, false)
  123. c.Assert((&Result{FixturePanicked: 1}).Passed(), Equals, false)
  124. c.Assert((&Result{Missed: 1}).Passed(), Equals, false)
  125. c.Assert((&Result{RunError: errors.New("!")}).Passed(), Equals, false)
  126. }
  127. // -----------------------------------------------------------------------
  128. // Check that result printing is working correctly.
  129. func (s *RunS) TestPrintSuccess(c *C) {
  130. result := &Result{Succeeded: 5}
  131. c.Check(result.String(), Equals, "OK: 5 passed")
  132. }
  133. func (s *RunS) TestPrintFailure(c *C) {
  134. result := &Result{Failed: 5}
  135. c.Check(result.String(), Equals, "OOPS: 0 passed, 5 FAILED")
  136. }
  137. func (s *RunS) TestPrintSkipped(c *C) {
  138. result := &Result{Skipped: 5}
  139. c.Check(result.String(), Equals, "OK: 0 passed, 5 skipped")
  140. }
  141. func (s *RunS) TestPrintExpectedFailures(c *C) {
  142. result := &Result{ExpectedFailures: 5}
  143. c.Check(result.String(), Equals, "OK: 0 passed, 5 expected failures")
  144. }
  145. func (s *RunS) TestPrintPanicked(c *C) {
  146. result := &Result{Panicked: 5}
  147. c.Check(result.String(), Equals, "OOPS: 0 passed, 5 PANICKED")
  148. }
  149. func (s *RunS) TestPrintFixturePanicked(c *C) {
  150. result := &Result{FixturePanicked: 5}
  151. c.Check(result.String(), Equals, "OOPS: 0 passed, 5 FIXTURE-PANICKED")
  152. }
  153. func (s *RunS) TestPrintMissed(c *C) {
  154. result := &Result{Missed: 5}
  155. c.Check(result.String(), Equals, "OOPS: 0 passed, 5 MISSED")
  156. }
  157. func (s *RunS) TestPrintAll(c *C) {
  158. result := &Result{Succeeded: 1, Skipped: 2, ExpectedFailures: 3,
  159. Panicked: 4, FixturePanicked: 5, Missed: 6}
  160. c.Check(result.String(), Equals,
  161. "OOPS: 1 passed, 2 skipped, 3 expected failures, 4 PANICKED, "+
  162. "5 FIXTURE-PANICKED, 6 MISSED")
  163. }
  164. func (s *RunS) TestPrintRunError(c *C) {
  165. result := &Result{Succeeded: 1, Failed: 1,
  166. RunError: errors.New("Kaboom!")}
  167. c.Check(result.String(), Equals, "ERROR: Kaboom!")
  168. }
  169. // -----------------------------------------------------------------------
  170. // Verify that the method pattern flag works correctly.
  171. func (s *RunS) TestFilterTestName(c *C) {
  172. helper := FixtureHelper{}
  173. output := String{}
  174. runConf := RunConf{Output: &output, Filter: "Test[91]"}
  175. Run(&helper, &runConf)
  176. c.Check(helper.calls[0], Equals, "SetUpSuite")
  177. c.Check(helper.calls[1], Equals, "SetUpTest")
  178. c.Check(helper.calls[2], Equals, "Test1")
  179. c.Check(helper.calls[3], Equals, "TearDownTest")
  180. c.Check(helper.calls[4], Equals, "TearDownSuite")
  181. c.Check(len(helper.calls), Equals, 5)
  182. }
  183. func (s *RunS) TestFilterTestNameWithAll(c *C) {
  184. helper := FixtureHelper{}
  185. output := String{}
  186. runConf := RunConf{Output: &output, Filter: ".*"}
  187. Run(&helper, &runConf)
  188. c.Check(helper.calls[0], Equals, "SetUpSuite")
  189. c.Check(helper.calls[1], Equals, "SetUpTest")
  190. c.Check(helper.calls[2], Equals, "Test1")
  191. c.Check(helper.calls[3], Equals, "TearDownTest")
  192. c.Check(helper.calls[4], Equals, "SetUpTest")
  193. c.Check(helper.calls[5], Equals, "Test2")
  194. c.Check(helper.calls[6], Equals, "TearDownTest")
  195. c.Check(helper.calls[7], Equals, "TearDownSuite")
  196. c.Check(len(helper.calls), Equals, 8)
  197. }
  198. func (s *RunS) TestFilterSuiteName(c *C) {
  199. helper := FixtureHelper{}
  200. output := String{}
  201. runConf := RunConf{Output: &output, Filter: "FixtureHelper"}
  202. Run(&helper, &runConf)
  203. c.Check(helper.calls[0], Equals, "SetUpSuite")
  204. c.Check(helper.calls[1], Equals, "SetUpTest")
  205. c.Check(helper.calls[2], Equals, "Test1")
  206. c.Check(helper.calls[3], Equals, "TearDownTest")
  207. c.Check(helper.calls[4], Equals, "SetUpTest")
  208. c.Check(helper.calls[5], Equals, "Test2")
  209. c.Check(helper.calls[6], Equals, "TearDownTest")
  210. c.Check(helper.calls[7], Equals, "TearDownSuite")
  211. c.Check(len(helper.calls), Equals, 8)
  212. }
  213. func (s *RunS) TestFilterSuiteNameAndTestName(c *C) {
  214. helper := FixtureHelper{}
  215. output := String{}
  216. runConf := RunConf{Output: &output, Filter: "FixtureHelper\\.Test2"}
  217. Run(&helper, &runConf)
  218. c.Check(helper.calls[0], Equals, "SetUpSuite")
  219. c.Check(helper.calls[1], Equals, "SetUpTest")
  220. c.Check(helper.calls[2], Equals, "Test2")
  221. c.Check(helper.calls[3], Equals, "TearDownTest")
  222. c.Check(helper.calls[4], Equals, "TearDownSuite")
  223. c.Check(len(helper.calls), Equals, 5)
  224. }
  225. func (s *RunS) TestFilterAllOut(c *C) {
  226. helper := FixtureHelper{}
  227. output := String{}
  228. runConf := RunConf{Output: &output, Filter: "NotFound"}
  229. Run(&helper, &runConf)
  230. c.Check(len(helper.calls), Equals, 0)
  231. }
  232. func (s *RunS) TestRequirePartialMatch(c *C) {
  233. helper := FixtureHelper{}
  234. output := String{}
  235. runConf := RunConf{Output: &output, Filter: "est"}
  236. Run(&helper, &runConf)
  237. c.Check(len(helper.calls), Equals, 8)
  238. }
  239. func (s *RunS) TestFilterError(c *C) {
  240. helper := FixtureHelper{}
  241. output := String{}
  242. runConf := RunConf{Output: &output, Filter: "]["}
  243. result := Run(&helper, &runConf)
  244. c.Check(result.String(), Equals,
  245. "ERROR: Bad filter expression: error parsing regexp: missing closing ]: `[`")
  246. c.Check(len(helper.calls), Equals, 0)
  247. }
  248. // -----------------------------------------------------------------------
  249. // Verify that List works correctly.
  250. func (s *RunS) TestListFiltered(c *C) {
  251. names := List(&FixtureHelper{}, &RunConf{Filter: "1"})
  252. c.Assert(names, DeepEquals, []string{
  253. "FixtureHelper.Test1",
  254. })
  255. }
  256. func (s *RunS) TestList(c *C) {
  257. names := List(&FixtureHelper{}, &RunConf{})
  258. c.Assert(names, DeepEquals, []string{
  259. "FixtureHelper.Test1",
  260. "FixtureHelper.Test2",
  261. })
  262. }
  263. // -----------------------------------------------------------------------
  264. // Verify that verbose mode prints tests which pass as well.
  265. func (s *RunS) TestVerboseMode(c *C) {
  266. helper := FixtureHelper{}
  267. output := String{}
  268. runConf := RunConf{Output: &output, Verbose: true}
  269. Run(&helper, &runConf)
  270. expected := "PASS: check_test\\.go:[0-9]+: FixtureHelper\\.Test1\t *[.0-9]+s\n" +
  271. "PASS: check_test\\.go:[0-9]+: FixtureHelper\\.Test2\t *[.0-9]+s\n"
  272. c.Assert(output.value, Matches, expected)
  273. }
  274. func (s *RunS) TestVerboseModeWithFailBeforePass(c *C) {
  275. helper := FixtureHelper{panicOn: "Test1"}
  276. output := String{}
  277. runConf := RunConf{Output: &output, Verbose: true}
  278. Run(&helper, &runConf)
  279. expected := "(?s).*PANIC.*\n-+\n" + // Should have an extra line.
  280. "PASS: check_test\\.go:[0-9]+: FixtureHelper\\.Test2\t *[.0-9]+s\n"
  281. c.Assert(output.value, Matches, expected)
  282. }
  283. // -----------------------------------------------------------------------
  284. // Verify the stream output mode. In this mode there's no output caching.
  285. type StreamHelper struct {
  286. l2 sync.Mutex
  287. l3 sync.Mutex
  288. }
  289. func (s *StreamHelper) SetUpSuite(c *C) {
  290. c.Log("0")
  291. }
  292. func (s *StreamHelper) Test1(c *C) {
  293. c.Log("1")
  294. s.l2.Lock()
  295. s.l3.Lock()
  296. go func() {
  297. s.l2.Lock() // Wait for "2".
  298. c.Log("3")
  299. s.l3.Unlock()
  300. }()
  301. }
  302. func (s *StreamHelper) Test2(c *C) {
  303. c.Log("2")
  304. s.l2.Unlock()
  305. s.l3.Lock() // Wait for "3".
  306. c.Fail()
  307. c.Log("4")
  308. }
  309. func (s *RunS) TestStreamMode(c *C) {
  310. helper := &StreamHelper{}
  311. output := String{}
  312. runConf := RunConf{Output: &output, Stream: true}
  313. Run(helper, &runConf)
  314. expected := "START: run_test\\.go:[0-9]+: StreamHelper\\.SetUpSuite\n0\n" +
  315. "PASS: run_test\\.go:[0-9]+: StreamHelper\\.SetUpSuite\t *[.0-9]+s\n\n" +
  316. "START: run_test\\.go:[0-9]+: StreamHelper\\.Test1\n1\n" +
  317. "PASS: run_test\\.go:[0-9]+: StreamHelper\\.Test1\t *[.0-9]+s\n\n" +
  318. "START: run_test\\.go:[0-9]+: StreamHelper\\.Test2\n2\n3\n4\n" +
  319. "FAIL: run_test\\.go:[0-9]+: StreamHelper\\.Test2\n\n"
  320. c.Assert(output.value, Matches, expected)
  321. }
  322. type StreamMissHelper struct{}
  323. func (s *StreamMissHelper) SetUpSuite(c *C) {
  324. c.Log("0")
  325. c.Fail()
  326. }
  327. func (s *StreamMissHelper) Test1(c *C) {
  328. c.Log("1")
  329. }
  330. func (s *RunS) TestStreamModeWithMiss(c *C) {
  331. helper := &StreamMissHelper{}
  332. output := String{}
  333. runConf := RunConf{Output: &output, Stream: true}
  334. Run(helper, &runConf)
  335. expected := "START: run_test\\.go:[0-9]+: StreamMissHelper\\.SetUpSuite\n0\n" +
  336. "FAIL: run_test\\.go:[0-9]+: StreamMissHelper\\.SetUpSuite\n\n" +
  337. "START: run_test\\.go:[0-9]+: StreamMissHelper\\.Test1\n" +
  338. "MISS: run_test\\.go:[0-9]+: StreamMissHelper\\.Test1\n\n"
  339. c.Assert(output.value, Matches, expected)
  340. }
  341. // -----------------------------------------------------------------------
  342. // Verify that that the keep work dir request indeed does so.
  343. type WorkDirSuite struct {}
  344. func (s *WorkDirSuite) Test(c *C) {
  345. c.MkDir()
  346. }
  347. func (s *RunS) TestKeepWorkDir(c *C) {
  348. output := String{}
  349. runConf := RunConf{Output: &output, Verbose: true, KeepWorkDir: true}
  350. result := Run(&WorkDirSuite{}, &runConf)
  351. c.Assert(result.String(), Matches, ".*\nWORK=" + result.WorkDir)
  352. stat, err := os.Stat(result.WorkDir)
  353. c.Assert(err, IsNil)
  354. c.Assert(stat.IsDir(), Equals, true)
  355. }