diff --git a/suite/doc.go b/suite/doc.go index b333e52..8f9a27b 100644 --- a/suite/doc.go +++ b/suite/doc.go @@ -23,6 +23,10 @@ // identity that "go test" is already looking for (i.e. // 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: // // Basic imports // import ( diff --git a/suite/suite.go b/suite/suite.go index 284f924..e738deb 100644 --- a/suite/suite.go +++ b/suite/suite.go @@ -1,11 +1,16 @@ package suite import ( + "flag" + "fmt" + "os" "reflect" "regexp" "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 // retrieving the current *testing.T context. type Suite struct { @@ -35,7 +40,12 @@ func Run(t *testing.T, suite TestingSuite) { tests := []testing.InternalTest{} for index := 0; index < methodFinder.NumMethod(); 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{ Name: method.Name, F: func(t *testing.T) { @@ -44,6 +54,7 @@ func Run(t *testing.T, suite TestingSuite) { if setupTestSuite, ok := suite.(SetupTestSuite); ok { setupTestSuite.SetupTest() } + t.Logf("Call test %q", method.Name) method.Func.Call([]reflect.Value{reflect.ValueOf(suite)}) if tearDownTestSuite, ok := suite.(TearDownTestSuite); ok { 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) { t.Fail() } @@ -64,3 +75,12 @@ func Run(t *testing.T, suite TestingSuite) { 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) +}