Skip to content

Commit 2b1dc4f

Browse files
dgryskideadprogram
authored andcommitted
testing: add -test.shuffle to order randomize test and benchmark order
1 parent 0244bed commit 2b1dc4f

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

compileopts/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ type TestConfig struct {
550550
BenchRegexp string
551551
BenchTime string
552552
BenchMem bool
553+
Shuffle string
553554
}
554555

555556
// filterTags removes predefined build tags for a target if a conflicting option

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ func Test(pkgName string, stdout, stderr io.Writer, options *compileopts.Options
251251
if testConfig.Count != nil && *testConfig.Count != 1 {
252252
flags = append(flags, "-test.count="+strconv.Itoa(*testConfig.Count))
253253
}
254+
if testConfig.Shuffle != "" {
255+
flags = append(flags, "-test.shuffle="+testConfig.Shuffle)
256+
}
254257

255258
logToStdout := testConfig.Verbose || testConfig.BenchRegexp != ""
256259

@@ -1442,6 +1445,7 @@ func main() {
14421445
flag.StringVar(&testConfig.BenchRegexp, "bench", "", "bench: regexp of benchmarks to run")
14431446
flag.StringVar(&testConfig.BenchTime, "benchtime", "", "run each benchmark for duration `d`")
14441447
flag.BoolVar(&testConfig.BenchMem, "benchmem", false, "show memory stats for benchmarks")
1448+
flag.StringVar(&testConfig.Shuffle, "shuffle", "", "shuffle the order the tests and benchmarks run")
14451449
}
14461450

14471451
// Early command processing, before commands are interpreted by the Go flag

src/testing/testing.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import (
1515
"fmt"
1616
"io"
1717
"io/fs"
18+
"math/rand"
1819
"os"
20+
"strconv"
1921
"strings"
2022
"time"
2123
"unicode"
@@ -28,6 +30,7 @@ var (
2830
flagShort bool
2931
flagRunRegexp string
3032
flagSkipRegexp string
33+
flagShuffle string
3134
flagCount int
3235
)
3336

@@ -44,6 +47,7 @@ func Init() {
4447
flag.BoolVar(&flagShort, "test.short", false, "short: run smaller test suite to save time")
4548
flag.StringVar(&flagRunRegexp, "test.run", "", "run: regexp of tests to run")
4649
flag.StringVar(&flagSkipRegexp, "test.skip", "", "skip: regexp of tests to run")
50+
flag.StringVar(&flagShuffle, "test.shuffle", "off", "shuffle: off, on, <numeric-seed>")
4751

4852
flag.IntVar(&flagCount, "test.count", 1, "run each test or benchmark `count` times")
4953

@@ -479,6 +483,27 @@ type testDeps interface {
479483
MatchString(pat, str string) (bool, error)
480484
}
481485

486+
func (m *M) shuffle() error {
487+
var n int64
488+
489+
if flagShuffle == "on" {
490+
n = time.Now().UnixNano()
491+
} else {
492+
var err error
493+
n, err = strconv.ParseInt(flagShuffle, 10, 64)
494+
if err != nil {
495+
m.exitCode = 2
496+
return fmt.Errorf(`testing: -shuffle should be "off", "on", or a valid integer: %v`, err)
497+
}
498+
}
499+
500+
fmt.Println("-test.shuffle", n)
501+
rng := rand.New(rand.NewSource(n))
502+
rng.Shuffle(len(m.Tests), func(i, j int) { m.Tests[i], m.Tests[j] = m.Tests[j], m.Tests[i] })
503+
rng.Shuffle(len(m.Benchmarks), func(i, j int) { m.Benchmarks[i], m.Benchmarks[j] = m.Benchmarks[j], m.Benchmarks[i] })
504+
return nil
505+
}
506+
482507
// Run runs the tests. It returns an exit code to pass to os.Exit.
483508
func (m *M) Run() (code int) {
484509
defer func() {
@@ -489,6 +514,13 @@ func (m *M) Run() (code int) {
489514
flag.Parse()
490515
}
491516

517+
if flagShuffle != "off" {
518+
if err := m.shuffle(); err != nil {
519+
fmt.Fprintln(os.Stderr, err)
520+
return
521+
}
522+
}
523+
492524
testRan, testOk := runTests(m.deps.MatchString, m.Tests)
493525
if !testRan && *matchBenchmarks == "" {
494526
fmt.Fprintln(os.Stderr, "testing: warning: no tests to run")

0 commit comments

Comments
 (0)