Browse files

testing: add argument to list tests, benchmarks, and examples

Some large testing/build systems require some form of test discovery before
running tests.  This usually allows for analytics, history, and stats on a per
tests basis.  Typically these systems are meant used in multi-language
environments and the original source code is not known or available.

This adds a -test.list option which takes a regular expression as an
argument. Any tests, benchmarks, or examples that match that regular
expression will be printed, one per line, to stdout and then the program
will exit.

Since subtests are named/discovered at run time this will only show
top-level tests names and is a known limitation.

Fixes #17209

Change-Id: I7e607f5f4f084d623a1cae88a1f70e7d92b7f13e
Reviewed-by: Marcel van Lohuizen <>
Run-TryBot: Marcel van Lohuizen <>
TryBot-Result: Gobot Gobot <>
  • Loading branch information...
nemith authored and mpvl committed Apr 20, 2017
1 parent 6e2c4bc commit ba8ff87dbeb87813a4604e36adb609b1e8fcb7be
Showing with 39 additions and 1 deletion.
  1. +7 −1 src/cmd/go/internal/test/test.go
  2. +3 −0 src/cmd/go/internal/test/testflag.go
  3. +29 −0 src/testing/testing.go
@@ -184,6 +184,11 @@ const testFlag2 = `
benchmarks should be executed. The default is the current value
-list regexp
List tests, benchmarks, or examples matching the regular expression.
No tests, benchmarks or examples will be run. This will only
list top-level tests. No subtest or subbenchmarks will be shown.
-parallel n
Allow parallel execution of test functions that call t.Parallel.
The value of this flag is the maximum number of tests to run
@@ -400,6 +405,7 @@ var (
testTimeout string // -timeout flag
testArgs []string
testBench bool
testList bool
testStreamOutput bool // show output as it is generated
testShowPass bool // show passing output
@@ -447,7 +453,7 @@ func runTest(cmd *base.Command, args []string) {
// show passing test output (after buffering) with -v flag.
// must buffer because tests are running in parallel, and
// otherwise the output will get mixed.
testShowPass = testV
testShowPass = testV || testList
// stream test output (no buffering) when no package has
// been given on the command line (implicit current directory)
@@ -42,6 +42,7 @@ var testFlagDefn = []*cmdflag.Defn{
{Name: "coverprofile", PassToTest: true},
{Name: "cpu", PassToTest: true},
{Name: "cpuprofile", PassToTest: true},
{Name: "list", PassToTest: true},
{Name: "memprofile", PassToTest: true},
{Name: "memprofilerate", PassToTest: true},
{Name: "blockprofile", PassToTest: true},
@@ -145,6 +146,8 @@ func testFlags(args []string) (packageNames, passToTest []string) {
case "bench":
// record that we saw the flag; don't care about the value
testBench = true
case "list":
testList = true
case "timeout":
testTimeout = value
case "blockprofile", "cpuprofile", "memprofile", "mutexprofile":
@@ -252,6 +252,7 @@ var (
chatty = flag.Bool("test.v", false, "verbose: print additional output")
count = flag.Uint("test.count", 1, "run tests and benchmarks `n` times")
coverProfile = flag.String("test.coverprofile", "", "write a coverage profile to `file`")
matchList = flag.String("test.list", "", "list tests, examples, and benchmarch maching `regexp` then exit")
match = flag.String("", "", "run only tests and examples matching `regexp`")
memProfile = flag.String("test.memprofile", "", "write a memory profile to `file`")
memProfileRate = flag.Int("test.memprofilerate", 0, "set memory profiling `rate` (see runtime.MemProfileRate)")
@@ -907,6 +908,11 @@ func (m *M) Run() int {
if len(*matchList) != 0 {
listTests(m.deps.MatchString, m.tests, m.benchmarks, m.examples)
return 0
@@ -946,6 +952,29 @@ func (t *T) report() {
func listTests(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) {
if _, err := matchString(*matchList, "non-empty"); err != nil {
fmt.Fprintf(os.Stderr, "testing: invalid regexp in -test.list (%q): %s\n", *matchList, err)
for _, test := range tests {
if ok, _ := matchString(*matchList, test.Name); ok {
for _, bench := range benchmarks {
if ok, _ := matchString(*matchList, bench.Name); ok {
for _, example := range examples {
if ok, _ := matchString(*matchList, example.Name); ok && example.Output != "" {
// An internal function but exported because it is cross-package; part of the implementation
// of the "go test" command.
func RunTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ok bool) {

0 comments on commit ba8ff87

Please sign in to comment.