You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

412 lines
11 KiB

  1. package suite
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. // SuiteRequireTwice is intended to test the usage of suite.Require in two
  12. // different tests
  13. type SuiteRequireTwice struct{ Suite }
  14. // TestSuiteRequireTwice checks for regressions of issue #149 where
  15. // suite.requirements was not initialised in suite.SetT()
  16. // A regression would result on these tests panicking rather than failing.
  17. func TestSuiteRequireTwice(t *testing.T) {
  18. ok := testing.RunTests(
  19. allTestsFilter,
  20. []testing.InternalTest{{
  21. Name: "TestSuiteRequireTwice",
  22. F: func(t *testing.T) {
  23. suite := new(SuiteRequireTwice)
  24. Run(t, suite)
  25. },
  26. }},
  27. )
  28. assert.Equal(t, false, ok)
  29. }
  30. func (s *SuiteRequireTwice) TestRequireOne() {
  31. r := s.Require()
  32. r.Equal(1, 2)
  33. }
  34. func (s *SuiteRequireTwice) TestRequireTwo() {
  35. r := s.Require()
  36. r.Equal(1, 2)
  37. }
  38. type panickingSuite struct {
  39. Suite
  40. panicInSetupSuite bool
  41. panicInSetupTest bool
  42. panicInBeforeTest bool
  43. panicInTest bool
  44. panicInAfterTest bool
  45. panicInTearDownTest bool
  46. panicInTearDownSuite bool
  47. }
  48. func (s *panickingSuite) SetupSuite() {
  49. if s.panicInSetupSuite {
  50. panic("oops in setup suite")
  51. }
  52. }
  53. func (s *panickingSuite) SetupTest() {
  54. if s.panicInSetupTest {
  55. panic("oops in setup test")
  56. }
  57. }
  58. func (s *panickingSuite) BeforeTest(_, _ string) {
  59. if s.panicInBeforeTest {
  60. panic("oops in before test")
  61. }
  62. }
  63. func (s *panickingSuite) Test() {
  64. if s.panicInTest {
  65. panic("oops in test")
  66. }
  67. }
  68. func (s *panickingSuite) AfterTest(_, _ string) {
  69. if s.panicInAfterTest {
  70. panic("oops in after test")
  71. }
  72. }
  73. func (s *panickingSuite) TearDownTest() {
  74. if s.panicInTearDownTest {
  75. panic("oops in tear down test")
  76. }
  77. }
  78. func (s *panickingSuite) TearDownSuite() {
  79. if s.panicInTearDownSuite {
  80. panic("oops in tear down suite")
  81. }
  82. }
  83. func TestSuiteRecoverPanic(t *testing.T) {
  84. ok := true
  85. panickingTests := []testing.InternalTest{
  86. {
  87. Name: "TestPanicInSetupSuite",
  88. F: func(t *testing.T) { Run(t, &panickingSuite{panicInSetupSuite: true}) },
  89. },
  90. {
  91. Name: "TestPanicInSetupTest",
  92. F: func(t *testing.T) { Run(t, &panickingSuite{panicInSetupTest: true}) },
  93. },
  94. {
  95. Name: "TestPanicInBeforeTest",
  96. F: func(t *testing.T) { Run(t, &panickingSuite{panicInBeforeTest: true}) },
  97. },
  98. {
  99. Name: "TestPanicInTest",
  100. F: func(t *testing.T) { Run(t, &panickingSuite{panicInTest: true}) },
  101. },
  102. {
  103. Name: "TestPanicInAfterTest",
  104. F: func(t *testing.T) { Run(t, &panickingSuite{panicInAfterTest: true}) },
  105. },
  106. {
  107. Name: "TestPanicInTearDownTest",
  108. F: func(t *testing.T) { Run(t, &panickingSuite{panicInTearDownTest: true}) },
  109. },
  110. {
  111. Name: "TestPanicInTearDownSuite",
  112. F: func(t *testing.T) { Run(t, &panickingSuite{panicInTearDownSuite: true}) },
  113. },
  114. }
  115. require.NotPanics(t, func() {
  116. ok = testing.RunTests(allTestsFilter, panickingTests)
  117. })
  118. assert.False(t, ok)
  119. }
  120. // This suite is intended to store values to make sure that only
  121. // testing-suite-related methods are run. It's also a fully
  122. // functional example of a testing suite, using setup/teardown methods
  123. // and a helper method that is ignored by testify. To make this look
  124. // more like a real world example, all tests in the suite perform some
  125. // type of assertion.
  126. type SuiteTester struct {
  127. // Include our basic suite logic.
  128. Suite
  129. // Keep counts of how many times each method is run.
  130. SetupSuiteRunCount int
  131. TearDownSuiteRunCount int
  132. SetupTestRunCount int
  133. TearDownTestRunCount int
  134. TestOneRunCount int
  135. TestTwoRunCount int
  136. TestSubtestRunCount int
  137. NonTestMethodRunCount int
  138. SuiteNameBefore []string
  139. TestNameBefore []string
  140. SuiteNameAfter []string
  141. TestNameAfter []string
  142. TimeBefore []time.Time
  143. TimeAfter []time.Time
  144. }
  145. type SuiteSkipTester struct {
  146. // Include our basic suite logic.
  147. Suite
  148. // Keep counts of how many times each method is run.
  149. SetupSuiteRunCount int
  150. TearDownSuiteRunCount int
  151. }
  152. // The SetupSuite method will be run by testify once, at the very
  153. // start of the testing suite, before any tests are run.
  154. func (suite *SuiteTester) SetupSuite() {
  155. suite.SetupSuiteRunCount++
  156. }
  157. func (suite *SuiteTester) BeforeTest(suiteName, testName string) {
  158. suite.SuiteNameBefore = append(suite.SuiteNameBefore, suiteName)
  159. suite.TestNameBefore = append(suite.TestNameBefore, testName)
  160. suite.TimeBefore = append(suite.TimeBefore, time.Now())
  161. }
  162. func (suite *SuiteTester) AfterTest(suiteName, testName string) {
  163. suite.SuiteNameAfter = append(suite.SuiteNameAfter, suiteName)
  164. suite.TestNameAfter = append(suite.TestNameAfter, testName)
  165. suite.TimeAfter = append(suite.TimeAfter, time.Now())
  166. }
  167. func (suite *SuiteSkipTester) SetupSuite() {
  168. suite.SetupSuiteRunCount++
  169. suite.T().Skip()
  170. }
  171. // The TearDownSuite method will be run by testify once, at the very
  172. // end of the testing suite, after all tests have been run.
  173. func (suite *SuiteTester) TearDownSuite() {
  174. suite.TearDownSuiteRunCount++
  175. }
  176. func (suite *SuiteSkipTester) TearDownSuite() {
  177. suite.TearDownSuiteRunCount++
  178. }
  179. // The SetupTest method will be run before every test in the suite.
  180. func (suite *SuiteTester) SetupTest() {
  181. suite.SetupTestRunCount++
  182. }
  183. // The TearDownTest method will be run after every test in the suite.
  184. func (suite *SuiteTester) TearDownTest() {
  185. suite.TearDownTestRunCount++
  186. }
  187. // Every method in a testing suite that begins with "Test" will be run
  188. // as a test. TestOne is an example of a test. For the purposes of
  189. // this example, we've included assertions in the tests, since most
  190. // tests will issue assertions.
  191. func (suite *SuiteTester) TestOne() {
  192. beforeCount := suite.TestOneRunCount
  193. suite.TestOneRunCount++
  194. assert.Equal(suite.T(), suite.TestOneRunCount, beforeCount+1)
  195. suite.Equal(suite.TestOneRunCount, beforeCount+1)
  196. }
  197. // TestTwo is another example of a test.
  198. func (suite *SuiteTester) TestTwo() {
  199. beforeCount := suite.TestTwoRunCount
  200. suite.TestTwoRunCount++
  201. assert.NotEqual(suite.T(), suite.TestTwoRunCount, beforeCount)
  202. suite.NotEqual(suite.TestTwoRunCount, beforeCount)
  203. }
  204. func (suite *SuiteTester) TestSkip() {
  205. suite.T().Skip()
  206. }
  207. // NonTestMethod does not begin with "Test", so it will not be run by
  208. // testify as a test in the suite. This is useful for creating helper
  209. // methods for your tests.
  210. func (suite *SuiteTester) NonTestMethod() {
  211. suite.NonTestMethodRunCount++
  212. }
  213. func (suite *SuiteTester) TestSubtest() {
  214. suite.TestSubtestRunCount++
  215. for _, t := range []struct {
  216. testName string
  217. }{
  218. {"first"},
  219. {"second"},
  220. } {
  221. suiteT := suite.T()
  222. suite.Run(t.testName, func() {
  223. // We should get a different *testing.T for subtests, so that
  224. // go test recognizes them as proper subtests for output formatting
  225. // and running individual subtests
  226. subTestT := suite.T()
  227. suite.NotEqual(subTestT, suiteT)
  228. })
  229. suite.Equal(suiteT, suite.T())
  230. }
  231. }
  232. // TestRunSuite will be run by the 'go test' command, so within it, we
  233. // can run our suite using the Run(*testing.T, TestingSuite) function.
  234. func TestRunSuite(t *testing.T) {
  235. suiteTester := new(SuiteTester)
  236. Run(t, suiteTester)
  237. // Normally, the test would end here. The following are simply
  238. // some assertions to ensure that the Run function is working as
  239. // intended - they are not part of the example.
  240. // The suite was only run once, so the SetupSuite and TearDownSuite
  241. // methods should have each been run only once.
  242. assert.Equal(t, suiteTester.SetupSuiteRunCount, 1)
  243. assert.Equal(t, suiteTester.TearDownSuiteRunCount, 1)
  244. assert.Equal(t, len(suiteTester.SuiteNameAfter), 4)
  245. assert.Equal(t, len(suiteTester.SuiteNameBefore), 4)
  246. assert.Equal(t, len(suiteTester.TestNameAfter), 4)
  247. assert.Equal(t, len(suiteTester.TestNameBefore), 4)
  248. assert.Contains(t, suiteTester.TestNameAfter, "TestOne")
  249. assert.Contains(t, suiteTester.TestNameAfter, "TestTwo")
  250. assert.Contains(t, suiteTester.TestNameAfter, "TestSkip")
  251. assert.Contains(t, suiteTester.TestNameAfter, "TestSubtest")
  252. assert.Contains(t, suiteTester.TestNameBefore, "TestOne")
  253. assert.Contains(t, suiteTester.TestNameBefore, "TestTwo")
  254. assert.Contains(t, suiteTester.TestNameBefore, "TestSkip")
  255. assert.Contains(t, suiteTester.TestNameBefore, "TestSubtest")
  256. for _, suiteName := range suiteTester.SuiteNameAfter {
  257. assert.Equal(t, "SuiteTester", suiteName)
  258. }
  259. for _, suiteName := range suiteTester.SuiteNameBefore {
  260. assert.Equal(t, "SuiteTester", suiteName)
  261. }
  262. for _, when := range suiteTester.TimeAfter {
  263. assert.False(t, when.IsZero())
  264. }
  265. for _, when := range suiteTester.TimeBefore {
  266. assert.False(t, when.IsZero())
  267. }
  268. // There are four test methods (TestOne, TestTwo, TestSkip, and TestSubtest), so
  269. // the SetupTest and TearDownTest methods (which should be run once for
  270. // each test) should have been run four times.
  271. assert.Equal(t, suiteTester.SetupTestRunCount, 4)
  272. assert.Equal(t, suiteTester.TearDownTestRunCount, 4)
  273. // Each test should have been run once.
  274. assert.Equal(t, suiteTester.TestOneRunCount, 1)
  275. assert.Equal(t, suiteTester.TestTwoRunCount, 1)
  276. assert.Equal(t, suiteTester.TestSubtestRunCount, 1)
  277. // Methods that don't match the test method identifier shouldn't
  278. // have been run at all.
  279. assert.Equal(t, suiteTester.NonTestMethodRunCount, 0)
  280. suiteSkipTester := new(SuiteSkipTester)
  281. Run(t, suiteSkipTester)
  282. // The suite was only run once, so the SetupSuite and TearDownSuite
  283. // methods should have each been run only once, even though SetupSuite
  284. // called Skip()
  285. assert.Equal(t, suiteSkipTester.SetupSuiteRunCount, 1)
  286. assert.Equal(t, suiteSkipTester.TearDownSuiteRunCount, 1)
  287. }
  288. func TestSuiteGetters(t *testing.T) {
  289. suite := new(SuiteTester)
  290. suite.SetT(t)
  291. assert.NotNil(t, suite.Assert())
  292. assert.Equal(t, suite.Assertions, suite.Assert())
  293. assert.NotNil(t, suite.Require())
  294. assert.Equal(t, suite.require, suite.Require())
  295. }
  296. type SuiteLoggingTester struct {
  297. Suite
  298. }
  299. func (s *SuiteLoggingTester) TestLoggingPass() {
  300. s.T().Log("TESTLOGPASS")
  301. }
  302. func (s *SuiteLoggingTester) TestLoggingFail() {
  303. s.T().Log("TESTLOGFAIL")
  304. assert.NotNil(s.T(), nil) // expected to fail
  305. }
  306. type StdoutCapture struct {
  307. oldStdout *os.File
  308. readPipe *os.File
  309. }
  310. func (sc *StdoutCapture) StartCapture() {
  311. sc.oldStdout = os.Stdout
  312. sc.readPipe, os.Stdout, _ = os.Pipe()
  313. }
  314. func (sc *StdoutCapture) StopCapture() (string, error) {
  315. if sc.oldStdout == nil || sc.readPipe == nil {
  316. return "", errors.New("StartCapture not called before StopCapture")
  317. }
  318. os.Stdout.Close()
  319. os.Stdout = sc.oldStdout
  320. bytes, err := ioutil.ReadAll(sc.readPipe)
  321. if err != nil {
  322. return "", err
  323. }
  324. return string(bytes), nil
  325. }
  326. func TestSuiteLogging(t *testing.T) {
  327. suiteLoggingTester := new(SuiteLoggingTester)
  328. capture := StdoutCapture{}
  329. internalTest := testing.InternalTest{
  330. Name: "SomeTest",
  331. F: func(subT *testing.T) {
  332. Run(subT, suiteLoggingTester)
  333. },
  334. }
  335. capture.StartCapture()
  336. testing.RunTests(allTestsFilter, []testing.InternalTest{internalTest})
  337. output, err := capture.StopCapture()
  338. require.NoError(t, err, "Got an error trying to capture stdout and stderr!")
  339. require.NotEmpty(t, output, "output content must not be empty")
  340. // Failed tests' output is always printed
  341. assert.Contains(t, output, "TESTLOGFAIL")
  342. if testing.Verbose() {
  343. // In verbose mode, output from successful tests is also printed
  344. assert.Contains(t, output, "TESTLOGPASS")
  345. } else {
  346. assert.NotContains(t, output, "TESTLOGPASS")
  347. }
  348. }