Run takes a testing suite and runs all of the tests attached to it.
(t *testing.T, suite TestingSuite)
| 124 | // Run takes a testing suite and runs all of the tests attached |
| 125 | // to it. |
| 126 | func Run(t *testing.T, suite TestingSuite) { |
| 127 | defer recoverAndFailOnPanic(t) |
| 128 | |
| 129 | suite.SetT(t) |
| 130 | suite.SetS(suite) |
| 131 | |
| 132 | var stats *SuiteInformation |
| 133 | if _, ok := suite.(WithStats); ok { |
| 134 | stats = newSuiteInformation() |
| 135 | } |
| 136 | |
| 137 | var tests []test |
| 138 | methodFinder := reflect.TypeOf(suite) |
| 139 | suiteName := methodFinder.Elem().Name() |
| 140 | |
| 141 | var matchMethodRE *regexp.Regexp |
| 142 | if *matchMethod != "" { |
| 143 | var err error |
| 144 | matchMethodRE, err = regexp.Compile(*matchMethod) |
| 145 | if err != nil { |
| 146 | fmt.Fprintf(os.Stderr, "testify: invalid regexp for -m: %s\n", err) |
| 147 | os.Exit(1) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | for i := 0; i < methodFinder.NumMethod(); i++ { |
| 152 | method := methodFinder.Method(i) |
| 153 | |
| 154 | if !strings.HasPrefix(method.Name, "Test") { |
| 155 | continue |
| 156 | } |
| 157 | // Apply -testify.m filter |
| 158 | if matchMethodRE != nil && !matchMethodRE.MatchString(method.Name) { |
| 159 | continue |
| 160 | } |
| 161 | |
| 162 | test := test{ |
| 163 | name: method.Name, |
| 164 | run: func(t *testing.T) { |
| 165 | parentT := suite.T() |
| 166 | suite.SetT(t) |
| 167 | defer recoverAndFailOnPanic(t) |
| 168 | defer func() { |
| 169 | t.Helper() |
| 170 | |
| 171 | r := recover() |
| 172 | |
| 173 | stats.end(method.Name, !t.Failed() && r == nil) |
| 174 | |
| 175 | if afterTestSuite, ok := suite.(AfterTest); ok { |
| 176 | afterTestSuite.AfterTest(suiteName, method.Name) |
| 177 | } |
| 178 | |
| 179 | if tearDownTestSuite, ok := suite.(TearDownTestSuite); ok { |
| 180 | tearDownTestSuite.TearDownTest() |
| 181 | } |
| 182 | |
| 183 | suite.SetT(parentT) |