suite.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package suite
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "reflect"
  7. "regexp"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. var allTestsFilter = func(_, _ string) (bool, error) { return true, nil }
  13. var matchMethod = flag.String("testify.m", "", "regular expression to select tests of the testify suite to run")
  14. // Suite is a basic testing suite with methods for storing and
  15. // retrieving the current *testing.T context.
  16. type Suite struct {
  17. *assert.Assertions
  18. require *require.Assertions
  19. t *testing.T
  20. }
  21. // T retrieves the current *testing.T context.
  22. func (suite *Suite) T() *testing.T {
  23. return suite.t
  24. }
  25. // SetT sets the current *testing.T context.
  26. func (suite *Suite) SetT(t *testing.T) {
  27. suite.t = t
  28. suite.Assertions = assert.New(t)
  29. suite.require = require.New(t)
  30. }
  31. // Require returns a require context for suite.
  32. func (suite *Suite) Require() *require.Assertions {
  33. if suite.require == nil {
  34. suite.require = require.New(suite.T())
  35. }
  36. return suite.require
  37. }
  38. // Assert returns an assert context for suite. Normally, you can call
  39. // `suite.NoError(expected, actual)`, but for situations where the embedded
  40. // methods are overridden (for example, you might want to override
  41. // assert.Assertions with require.Assertions), this method is provided so you
  42. // can call `suite.Assert().NoError()`.
  43. func (suite *Suite) Assert() *assert.Assertions {
  44. if suite.Assertions == nil {
  45. suite.Assertions = assert.New(suite.T())
  46. }
  47. return suite.Assertions
  48. }
  49. // Run takes a testing suite and runs all of the tests attached
  50. // to it.
  51. func Run(t *testing.T, suite TestingSuite) {
  52. suite.SetT(t)
  53. if setupAllSuite, ok := suite.(SetupAllSuite); ok {
  54. setupAllSuite.SetupSuite()
  55. }
  56. defer func() {
  57. if tearDownAllSuite, ok := suite.(TearDownAllSuite); ok {
  58. tearDownAllSuite.TearDownSuite()
  59. }
  60. }()
  61. methodFinder := reflect.TypeOf(suite)
  62. tests := []testing.InternalTest{}
  63. for index := 0; index < methodFinder.NumMethod(); index++ {
  64. method := methodFinder.Method(index)
  65. ok, err := methodFilter(method.Name)
  66. if err != nil {
  67. fmt.Fprintf(os.Stderr, "testify: invalid regexp for -m: %s\n", err)
  68. os.Exit(1)
  69. }
  70. if ok {
  71. test := testing.InternalTest{
  72. Name: method.Name,
  73. F: func(t *testing.T) {
  74. parentT := suite.T()
  75. suite.SetT(t)
  76. if setupTestSuite, ok := suite.(SetupTestSuite); ok {
  77. setupTestSuite.SetupTest()
  78. }
  79. if beforeTestSuite, ok := suite.(BeforeTest); ok {
  80. beforeTestSuite.BeforeTest(methodFinder.Elem().Name(), method.Name)
  81. }
  82. defer func() {
  83. if afterTestSuite, ok := suite.(AfterTest); ok {
  84. afterTestSuite.AfterTest(methodFinder.Elem().Name(), method.Name)
  85. }
  86. if tearDownTestSuite, ok := suite.(TearDownTestSuite); ok {
  87. tearDownTestSuite.TearDownTest()
  88. }
  89. suite.SetT(parentT)
  90. }()
  91. method.Func.Call([]reflect.Value{reflect.ValueOf(suite)})
  92. },
  93. }
  94. tests = append(tests, test)
  95. }
  96. }
  97. runTests(t, tests)
  98. }
  99. func runTests(t testing.TB, tests []testing.InternalTest) {
  100. r, ok := t.(runner)
  101. if !ok { // backwards compatibility with Go 1.6 and below
  102. if !testing.RunTests(allTestsFilter, tests) {
  103. t.Fail()
  104. }
  105. return
  106. }
  107. for _, test := range tests {
  108. r.Run(test.Name, test.F)
  109. }
  110. }
  111. // Filtering method according to set regular expression
  112. // specified command-line argument -m
  113. func methodFilter(name string) (bool, error) {
  114. if ok, _ := regexp.MatchString("^Test", name); !ok {
  115. return false, nil
  116. }
  117. return regexp.MatchString(*matchMethod, name)
  118. }
  119. type runner interface {
  120. Run(name string, f func(t *testing.T)) bool
  121. }