Run benchmarks matching the regular expression.
By default, no benchmarks run.
+ -test.example pattern
+ Run examples matching the regular expression.
+ By default, all examples run, but if -test.run is set,
+ no examples are run.
+
-test.cpuprofile cpu.out
Write a CPU profile to the specified file before exiting.
-benchtime=1: passes -test.benchtime to test
-cpu="": passes -test.cpu to test
-cpuprofile="": passes -test.cpuprofile to test
+ -example="": passes -test.example to test
-memprofile="": passes -test.memprofile to test
-memprofilerate=0: passes -test.memprofilerate to test
-parallel=0: passes -test.parallel to test
{name: "benchtime", passToTest: true},
{name: "cpu", passToTest: true},
{name: "cpuprofile", passToTest: true},
+ {name: "example", passToTest: true},
{name: "memprofile", passToTest: true},
{name: "memprofilerate", passToTest: true},
{name: "parallel", passToTest: true},
import (
"bytes"
+ "flag"
"fmt"
"io"
"os"
"time"
)
+var matchExamples = flag.String("test.example", "", "regular expression to select examples to run")
+
type InternalExample struct {
Name string
F func()
Output string
}
-func RunExamples(examples []InternalExample) (ok bool) {
+func RunExamples(matchString func(pat, str string) (bool, error), examples []InternalExample) (ok bool) {
+ if *match != "" {
+ return // Don't run examples if testing is restricted: we're debugging.
+ }
ok = true
var eg InternalExample
stdout, stderr := os.Stdout, os.Stderr
for _, eg = range examples {
+ matched, err := matchString(*matchExamples, eg.Name)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "testing: invalid regexp for -test.example: %s\n", err)
+ os.Exit(1)
+ }
+ if !matched {
+ continue
+ }
if *chatty {
fmt.Printf("=== RUN: %s\n", eg.Name)
}
before()
startAlarm()
testOk := RunTests(matchString, tests)
- exampleOk := RunExamples(examples)
+ exampleOk := RunExamples(matchString, examples)
if !testOk || !exampleOk {
fmt.Println("FAIL")
os.Exit(1)