suite_test.go 8.5 KB

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