mirror of https://github.com/stretchr/testify.git
Specify the method of the test suite
parent
3e03dde724
commit
78cdc04039
|
@ -23,6 +23,10 @@
|
||||||
// identity that "go test" is already looking for (i.e.
|
// identity that "go test" is already looking for (i.e.
|
||||||
// func(*testing.T)).
|
// func(*testing.T)).
|
||||||
//
|
//
|
||||||
|
// Regular expression to select test suites specified command-line
|
||||||
|
// command-line argument "-run". Regular expression to select the
|
||||||
|
// methods of test suites specified command-line argument "-m".
|
||||||
|
//
|
||||||
// A crude example:
|
// A crude example:
|
||||||
// // Basic imports
|
// // Basic imports
|
||||||
// import (
|
// import (
|
||||||
|
|
|
@ -1,11 +1,16 @@
|
||||||
package suite
|
package suite
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var matchMethod = flag.String("m", "", "regular expression to select tests of the suite to run")
|
||||||
|
|
||||||
// Suite is a basic testing suite with methods for storing and
|
// Suite is a basic testing suite with methods for storing and
|
||||||
// retrieving the current *testing.T context.
|
// retrieving the current *testing.T context.
|
||||||
type Suite struct {
|
type Suite struct {
|
||||||
|
@ -35,7 +40,12 @@ func Run(t *testing.T, suite TestingSuite) {
|
||||||
tests := []testing.InternalTest{}
|
tests := []testing.InternalTest{}
|
||||||
for index := 0; index < methodFinder.NumMethod(); index++ {
|
for index := 0; index < methodFinder.NumMethod(); index++ {
|
||||||
method := methodFinder.Method(index)
|
method := methodFinder.Method(index)
|
||||||
if ok, _ := regexp.MatchString("^Test", method.Name); ok {
|
ok, err := methodFilter(method.Name)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "testify: invalid regexp for -m: %s\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if ok {
|
||||||
test := testing.InternalTest{
|
test := testing.InternalTest{
|
||||||
Name: method.Name,
|
Name: method.Name,
|
||||||
F: func(t *testing.T) {
|
F: func(t *testing.T) {
|
||||||
|
@ -44,6 +54,7 @@ func Run(t *testing.T, suite TestingSuite) {
|
||||||
if setupTestSuite, ok := suite.(SetupTestSuite); ok {
|
if setupTestSuite, ok := suite.(SetupTestSuite); ok {
|
||||||
setupTestSuite.SetupTest()
|
setupTestSuite.SetupTest()
|
||||||
}
|
}
|
||||||
|
t.Logf("Call test %q", method.Name)
|
||||||
method.Func.Call([]reflect.Value{reflect.ValueOf(suite)})
|
method.Func.Call([]reflect.Value{reflect.ValueOf(suite)})
|
||||||
if tearDownTestSuite, ok := suite.(TearDownTestSuite); ok {
|
if tearDownTestSuite, ok := suite.(TearDownTestSuite); ok {
|
||||||
tearDownTestSuite.TearDownTest()
|
tearDownTestSuite.TearDownTest()
|
||||||
|
@ -55,7 +66,7 @@ func Run(t *testing.T, suite TestingSuite) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !testing.RunTests(func(_, _ string) (bool, error) {return true, nil},
|
if !testing.RunTests(func(_, _ string) (bool, error) { return true, nil },
|
||||||
tests) {
|
tests) {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
@ -64,3 +75,12 @@ func Run(t *testing.T, suite TestingSuite) {
|
||||||
tearDownAllSuite.TearDownSuite()
|
tearDownAllSuite.TearDownSuite()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Filtering method according to set regular expression
|
||||||
|
// specified command-line argument -m
|
||||||
|
func methodFilter(name string) (bool, error) {
|
||||||
|
if ok, _ := regexp.MatchString("^Test", name); !ok {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return regexp.MatchString(*matchMethod, name)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue