1
0

suite_test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package suite
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. // SuiteRequireTwice is intended to test the usage of suite.Require in two
  11. // different tests
  12. type SuiteRequireTwice struct{ Suite }
  13. // TestSuiteRequireTwice checks for regressions of issue #149 where
  14. // suite.requirements was not initialised in suite.SetT()
  15. // A regression would result on these tests panicking rather than failing.
  16. func TestSuiteRequireTwice(t *testing.T) {
  17. ok := testing.RunTests(
  18. func(_, _ string) (bool, error) { return true, nil },
  19. []testing.InternalTest{{
  20. Name: "TestSuiteRequireTwice",
  21. F: func(t *testing.T) {
  22. suite := new(SuiteRequireTwice)
  23. Run(t, suite)
  24. },
  25. }},
  26. )
  27. assert.Equal(t, false, ok)
  28. }
  29. func (s *SuiteRequireTwice) TestRequireOne() {
  30. r := s.Require()
  31. r.Equal(1, 2)
  32. }
  33. func (s *SuiteRequireTwice) TestRequireTwo() {
  34. r := s.Require()
  35. r.Equal(1, 2)
  36. }
  37. // This suite is intended to store values to make sure that only
  38. // testing-suite-related methods are run. It's also a fully
  39. // functional example of a testing suite, using setup/teardown methods
  40. // and a helper method that is ignored by testify. To make this look
  41. // more like a real world example, all tests in the suite perform some
  42. // type of assertion.
  43. type SuiteTester struct {
  44. // Include our basic suite logic.
  45. Suite
  46. // Keep counts of how many times each method is run.
  47. SetupSuiteRunCount int
  48. TearDownSuiteRunCount int
  49. SetupTestRunCount int
  50. TearDownTestRunCount int
  51. TestOneRunCount int
  52. TestTwoRunCount int
  53. NonTestMethodRunCount int
  54. SuiteNameBefore []string
  55. TestNameBefore []string
  56. SuiteNameAfter []string
  57. TestNameAfter []string
  58. TimeBefore []time.Time
  59. TimeAfter []time.Time
  60. }
  61. type SuiteSkipTester struct {
  62. // Include our basic suite logic.
  63. Suite
  64. // Keep counts of how many times each method is run.
  65. SetupSuiteRunCount int
  66. TearDownSuiteRunCount int
  67. }
  68. // The SetupSuite method will be run by testify once, at the very
  69. // start of the testing suite, before any tests are run.
  70. func (suite *SuiteTester) SetupSuite() {
  71. suite.SetupSuiteRunCount++
  72. }
  73. func (suite *SuiteTester) BeforeTest(suiteName, testName string) {
  74. suite.SuiteNameBefore = append(suite.SuiteNameBefore, suiteName)
  75. suite.TestNameBefore = append(suite.TestNameBefore, testName)
  76. suite.TimeBefore = append(suite.TimeBefore, time.Now())
  77. }
  78. func (suite *SuiteTester) AfterTest(suiteName, testName string) {
  79. suite.SuiteNameAfter = append(suite.SuiteNameAfter, suiteName)
  80. suite.TestNameAfter = append(suite.TestNameAfter, testName)
  81. suite.TimeAfter = append(suite.TimeAfter, time.Now())
  82. }
  83. func (suite *SuiteSkipTester) SetupSuite() {
  84. suite.SetupSuiteRunCount++
  85. suite.T().Skip()
  86. }
  87. // The TearDownSuite method will be run by testify once, at the very
  88. // end of the testing suite, after all tests have been run.
  89. func (suite *SuiteTester) TearDownSuite() {
  90. suite.TearDownSuiteRunCount++
  91. }
  92. func (suite *SuiteSkipTester) TearDownSuite() {
  93. suite.TearDownSuiteRunCount++
  94. }
  95. // The SetupTest method will be run before every test in the suite.
  96. func (suite *SuiteTester) SetupTest() {
  97. suite.SetupTestRunCount++
  98. }
  99. // The TearDownTest method will be run after every test in the suite.
  100. func (suite *SuiteTester) TearDownTest() {
  101. suite.TearDownTestRunCount++
  102. }
  103. // Every method in a testing suite that begins with "Test" will be run
  104. // as a test. TestOne is an example of a test. For the purposes of
  105. // this example, we've included assertions in the tests, since most
  106. // tests will issue assertions.
  107. func (suite *SuiteTester) TestOne() {
  108. beforeCount := suite.TestOneRunCount
  109. suite.TestOneRunCount++
  110. assert.Equal(suite.T(), suite.TestOneRunCount, beforeCount+1)
  111. suite.Equal(suite.TestOneRunCount, beforeCount+1)
  112. }
  113. // TestTwo is another example of a test.
  114. func (suite *SuiteTester) TestTwo() {
  115. beforeCount := suite.TestTwoRunCount
  116. suite.TestTwoRunCount++
  117. assert.NotEqual(suite.T(), suite.TestTwoRunCount, beforeCount)
  118. suite.NotEqual(suite.TestTwoRunCount, beforeCount)
  119. }
  120. func (suite *SuiteTester) TestSkip() {
  121. suite.T().Skip()
  122. }
  123. // NonTestMethod does not begin with "Test", so it will not be run by
  124. // testify as a test in the suite. This is useful for creating helper
  125. // methods for your tests.
  126. func (suite *SuiteTester) NonTestMethod() {
  127. suite.NonTestMethodRunCount++
  128. }
  129. // TestRunSuite will be run by the 'go test' command, so within it, we
  130. // can run our suite using the Run(*testing.T, TestingSuite) function.
  131. func TestRunSuite(t *testing.T) {
  132. suiteTester := new(SuiteTester)
  133. Run(t, suiteTester)
  134. // Normally, the test would end here. The following are simply
  135. // some assertions to ensure that the Run function is working as
  136. // intended - they are not part of the example.
  137. // The suite was only run once, so the SetupSuite and TearDownSuite
  138. // methods should have each been run only once.
  139. assert.Equal(t, suiteTester.SetupSuiteRunCount, 1)
  140. assert.Equal(t, suiteTester.TearDownSuiteRunCount, 1)
  141. assert.Equal(t, len(suiteTester.SuiteNameAfter), 3)
  142. assert.Equal(t, len(suiteTester.SuiteNameBefore), 3)
  143. assert.Equal(t, len(suiteTester.TestNameAfter), 3)
  144. assert.Equal(t, len(suiteTester.TestNameBefore), 3)
  145. assert.Contains(t, suiteTester.TestNameAfter, "TestOne")
  146. assert.Contains(t, suiteTester.TestNameAfter, "TestTwo")
  147. assert.Contains(t, suiteTester.TestNameAfter, "TestSkip")
  148. assert.Contains(t, suiteTester.TestNameBefore, "TestOne")
  149. assert.Contains(t, suiteTester.TestNameBefore, "TestTwo")
  150. assert.Contains(t, suiteTester.TestNameBefore, "TestSkip")
  151. for _, suiteName := range suiteTester.SuiteNameAfter {
  152. assert.Equal(t, "SuiteTester", suiteName)
  153. }
  154. for _, suiteName := range suiteTester.SuiteNameBefore {
  155. assert.Equal(t, "SuiteTester", suiteName)
  156. }
  157. for _, when := range suiteTester.TimeAfter {
  158. assert.False(t, when.IsZero())
  159. }
  160. for _, when := range suiteTester.TimeBefore {
  161. assert.False(t, when.IsZero())
  162. }
  163. // There are three test methods (TestOne, TestTwo, and TestSkip), so
  164. // the SetupTest and TearDownTest methods (which should be run once for
  165. // each test) should have been run three times.
  166. assert.Equal(t, suiteTester.SetupTestRunCount, 3)
  167. assert.Equal(t, suiteTester.TearDownTestRunCount, 3)
  168. // Each test should have been run once.
  169. assert.Equal(t, suiteTester.TestOneRunCount, 1)
  170. assert.Equal(t, suiteTester.TestTwoRunCount, 1)
  171. // Methods that don't match the test method identifier shouldn't
  172. // have been run at all.
  173. assert.Equal(t, suiteTester.NonTestMethodRunCount, 0)
  174. suiteSkipTester := new(SuiteSkipTester)
  175. Run(t, suiteSkipTester)
  176. // The suite was only run once, so the SetupSuite and TearDownSuite
  177. // methods should have each been run only once, even though SetupSuite
  178. // called Skip()
  179. assert.Equal(t, suiteSkipTester.SetupSuiteRunCount, 1)
  180. assert.Equal(t, suiteSkipTester.TearDownSuiteRunCount, 1)
  181. }
  182. func TestSuiteGetters(t *testing.T) {
  183. suite := new(SuiteTester)
  184. suite.SetT(t)
  185. assert.NotNil(t, suite.Assert())
  186. assert.Equal(t, suite.Assertions, suite.Assert())
  187. assert.NotNil(t, suite.Require())
  188. assert.Equal(t, suite.require, suite.Require())
  189. }
  190. type SuiteLoggingTester struct {
  191. Suite
  192. }
  193. func (s *SuiteLoggingTester) TestLoggingPass() {
  194. s.T().Log("TESTLOGPASS")
  195. }
  196. func (s *SuiteLoggingTester) TestLoggingFail() {
  197. s.T().Log("TESTLOGFAIL")
  198. assert.NotNil(s.T(), nil) // expected to fail
  199. }
  200. type StdoutCapture struct {
  201. oldStdout *os.File
  202. readPipe *os.File
  203. }
  204. func (sc *StdoutCapture) StartCapture() {
  205. sc.oldStdout = os.Stdout
  206. sc.readPipe, os.Stdout, _ = os.Pipe()
  207. }
  208. func (sc *StdoutCapture) StopCapture() (string, error) {
  209. if sc.oldStdout == nil || sc.readPipe == nil {
  210. return "", errors.New("StartCapture not called before StopCapture")
  211. }
  212. os.Stdout.Close()
  213. os.Stdout = sc.oldStdout
  214. bytes, err := ioutil.ReadAll(sc.readPipe)
  215. if err != nil {
  216. return "", err
  217. }
  218. return string(bytes), nil
  219. }
  220. func TestSuiteLogging(t *testing.T) {
  221. testT := testing.T{}
  222. suiteLoggingTester := new(SuiteLoggingTester)
  223. capture := StdoutCapture{}
  224. capture.StartCapture()
  225. Run(&testT, suiteLoggingTester)
  226. output, err := capture.StopCapture()
  227. assert.Nil(t, err, "Got an error trying to capture stdout!")
  228. // Failed tests' output is always printed
  229. assert.Contains(t, output, "TESTLOGFAIL")
  230. if testing.Verbose() {
  231. // In verbose mode, output from successful tests is also printed
  232. assert.Contains(t, output, "TESTLOGPASS")
  233. } else {
  234. assert.NotContains(t, output, "TESTLOGPASS")
  235. }
  236. }