suite_test.go 6.7 KB

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