From 7d37b5c962954410bcd7a71ff3a77c79514056d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Wed, 6 Mar 2024 01:24:45 +0100 Subject: [PATCH 1/2] suite: refactor methodFilter Use strings.HasPrefix instead of a /^Test/ regexp (compiled on every call). --- suite/suite.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/suite/suite.go b/suite/suite.go index 8a5c0534e..f582b3536 100644 --- a/suite/suite.go +++ b/suite/suite.go @@ -7,6 +7,7 @@ import ( "reflect" "regexp" "runtime/debug" + "strings" "sync" "testing" "time" @@ -219,7 +220,7 @@ func Run(t *testing.T, suite TestingSuite) { // 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 { + if !strings.HasPrefix(name, "Test") { return false, nil } return regexp.MatchString(*matchMethod, name) From bc7459ec38128532ff32f23cfab4ea0b725210f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Tue, 27 May 2025 15:36:15 +0200 Subject: [PATCH 2/2] suite: faster filtering of methods (-testify.m) Refactor filtering of methods in suite.Run: the regexp given via -testify.m flag is compiled just once, out of the loop. --- suite/suite.go | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/suite/suite.go b/suite/suite.go index f582b3536..1b19be3bc 100644 --- a/suite/suite.go +++ b/suite/suite.go @@ -138,16 +138,24 @@ func Run(t *testing.T, suite TestingSuite) { methodFinder := reflect.TypeOf(suite) suiteName := methodFinder.Elem().Name() - for i := 0; i < methodFinder.NumMethod(); i++ { - method := methodFinder.Method(i) - - ok, err := methodFilter(method.Name) + var matchMethodRE *regexp.Regexp + if *matchMethod != "" { + var err error + matchMethodRE, err = regexp.Compile(*matchMethod) if err != nil { fmt.Fprintf(os.Stderr, "testify: invalid regexp for -m: %s\n", err) os.Exit(1) } + } + + for i := 0; i < methodFinder.NumMethod(); i++ { + method := methodFinder.Method(i) - if !ok { + if !strings.HasPrefix(method.Name, "Test") { + continue + } + // Apply -testify.m filter + if matchMethodRE != nil && !matchMethodRE.MatchString(method.Name) { continue } @@ -217,15 +225,6 @@ func Run(t *testing.T, suite TestingSuite) { runTests(t, tests) } -// Filtering method according to set regular expression -// specified command-line argument -m -func methodFilter(name string) (bool, error) { - if !strings.HasPrefix(name, "Test") { - return false, nil - } - return regexp.MatchString(*matchMethod, name) -} - func runTests(t *testing.T, tests []test) { if len(tests) == 0 { t.Log("warning: no tests to run")