-
Notifications
You must be signed in to change notification settings - Fork 17.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testing: add -shuffle=off|on|N to alter the execution order of tests …
…and benchmarks This CL adds a new flag to the testing package and the go test command which randomizes the execution order for tests and benchmarks. This can be useful for identifying unwanted dependencies between test or benchmark functions. The flag is off by default. If `-shuffle` is set to `on` then the system clock will be used as the seed value. If `-shuffle` is set to an integer N, then N will be used as the seed value. In both cases, the seed will be reported for failed runs so that they can reproduced later on. Fixes #28592 Change-Id: I62e7dfae5f63f97a0cbd7830ea844d9f7beac335 Reviewed-on: https://go-review.googlesource.com/c/go/+/310033 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Trust: Bryan C. Mills <bcmills@google.com>
- Loading branch information
1 parent
e51246c
commit cbb3f09
Showing
10 changed files
with
249 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
# Shuffle order of tests and benchmarks | ||
|
||
# Run tests | ||
go test -v foo_test.go | ||
! stdout '-test.shuffle ' | ||
stdout '(?s)TestOne(.*)TestTwo(.*)TestThree' | ||
|
||
go test -v -shuffle=off foo_test.go | ||
! stdout '-test.shuffle ' | ||
stdout '(?s)TestOne(.*)TestTwo(.*)TestThree' | ||
|
||
go test -v -shuffle=42 foo_test.go | ||
stdout '^-test.shuffle 42' | ||
stdout '(?s)TestThree(.*)TestOne(.*)TestTwo' | ||
|
||
go test -v -shuffle=43 foo_test.go | ||
stdout '^-test.shuffle 43' | ||
stdout '(?s)TestThree(.*)TestTwo(.*)TestOne' | ||
|
||
go test -v -shuffle=44 foo_test.go | ||
stdout '^-test.shuffle 44' | ||
stdout '(?s)TestOne(.*)TestThree(.*)TestTwo' | ||
|
||
go test -v -shuffle=0 foo_test.go | ||
stdout '^-test.shuffle 0' | ||
stdout '(?s)TestTwo(.*)TestOne(.*)TestThree' | ||
|
||
go test -v -shuffle -1 foo_test.go | ||
stdout '^-test.shuffle -1' | ||
stdout '(?s)TestThree(.*)TestOne(.*)TestTwo' | ||
|
||
go test -v -shuffle=on foo_test.go | ||
stdout '^-test.shuffle ' | ||
stdout '(?s)=== RUN TestOne(.*)--- PASS: TestOne' | ||
stdout '(?s)=== RUN TestTwo(.*)--- PASS: TestTwo' | ||
stdout '(?s)=== RUN TestThree(.*)--- PASS: TestThree' | ||
|
||
|
||
# Run tests and benchmarks | ||
go test -v -bench=. foo_test.go | ||
! stdout '-test.shuffle ' | ||
stdout '(?s)TestOne(.*)TestTwo(.*)TestThree(.*)BenchmarkOne(.*)BenchmarkTwo(.*)BenchmarkThree' | ||
|
||
go test -v -bench=. -shuffle=off foo_test.go | ||
! stdout '-test.shuffle ' | ||
stdout '(?s)TestOne(.*)TestTwo(.*)TestThree(.*)BenchmarkOne(.*)BenchmarkTwo(.*)BenchmarkThree' | ||
|
||
go test -v -bench=. -shuffle=42 foo_test.go | ||
stdout '^-test.shuffle 42' | ||
stdout '(?s)TestThree(.*)TestOne(.*)TestTwo(.*)BenchmarkThree(.*)BenchmarkOne(.*)BenchmarkTwo' | ||
|
||
go test -v -bench=. -shuffle=43 foo_test.go | ||
stdout '^-test.shuffle 43' | ||
stdout '(?s)TestThree(.*)TestTwo(.*)TestOne(.*)BenchmarkThree(.*)BenchmarkOne(.*)BenchmarkTwo' | ||
|
||
go test -v -bench=. -shuffle=44 foo_test.go | ||
stdout '^-test.shuffle 44' | ||
stdout '(?s)TestOne(.*)TestThree(.*)TestTwo(.*)BenchmarkTwo(.*)BenchmarkOne(.*)BenchmarkThree' | ||
|
||
go test -v -bench=. -shuffle=0 foo_test.go | ||
stdout '^-test.shuffle 0' | ||
stdout '(?s)TestTwo(.*)TestOne(.*)TestThree(.*)BenchmarkThree(.*)BenchmarkOne(.*)BenchmarkTwo' | ||
|
||
go test -v -bench=. -shuffle -1 foo_test.go | ||
stdout '^-test.shuffle -1' | ||
stdout '(?s)TestThree(.*)TestOne(.*)TestTwo(.*)BenchmarkOne(.*)BenchmarkThree(.*)BenchmarkTwo' | ||
|
||
go test -v -bench=. -shuffle=on foo_test.go | ||
stdout '^-test.shuffle ' | ||
stdout '(?s)=== RUN TestOne(.*)--- PASS: TestOne' | ||
stdout '(?s)=== RUN TestTwo(.*)--- PASS: TestTwo' | ||
stdout '(?s)=== RUN TestThree(.*)--- PASS: TestThree' | ||
stdout -count=2 'BenchmarkOne' | ||
stdout -count=2 'BenchmarkTwo' | ||
stdout -count=2 'BenchmarkThree' | ||
|
||
|
||
# When running go test -count=N, each of the N runs distinct runs should maintain the same | ||
# shuffled order of these tests. | ||
go test -v -shuffle=43 -count=4 foo_test.go | ||
stdout '^-test.shuffle 43' | ||
stdout '(?s)TestThree(.*)TestTwo(.*)TestOne(.*)TestThree(.*)TestTwo(.*)TestOne(.*)TestThree(.*)TestTwo(.*)TestOne(.*)TestThree(.*)TestTwo(.*)TestOne' | ||
|
||
go test -v -bench=. -shuffle=44 -count=2 foo_test.go | ||
stdout '^-test.shuffle 44' | ||
stdout '(?s)TestOne(.*)TestThree(.*)TestTwo(.*)TestOne(.*)TestThree(.*)TestTwo(.*)BenchmarkTwo(.*)BenchmarkOne(.*)BenchmarkThree(.*)' | ||
|
||
|
||
# The feature should work with test binaries as well | ||
go test -c | ||
exec ./m.test -test.shuffle=off | ||
! stdout '^-test.shuffle ' | ||
|
||
exec ./m.test -test.shuffle=on | ||
stdout '^-test.shuffle ' | ||
|
||
exec ./m.test -test.v -test.bench=. -test.shuffle=0 foo_test.go | ||
stdout '^-test.shuffle 0' | ||
stdout '(?s)TestTwo(.*)TestOne(.*)TestThree(.*)BenchmarkThree(.*)BenchmarkOne(.*)BenchmarkTwo' | ||
|
||
exec ./m.test -test.v -test.bench=. -test.shuffle=123 foo_test.go | ||
stdout '^-test.shuffle 123' | ||
stdout '(?s)TestThree(.*)TestOne(.*)TestTwo(.*)BenchmarkThree(.*)BenchmarkTwo(.*)BenchmarkOne' | ||
|
||
exec ./m.test -test.v -test.bench=. -test.shuffle=-1 foo_test.go | ||
stdout '^-test.shuffle -1' | ||
stdout '(?s)TestThree(.*)TestOne(.*)TestTwo(.*)BenchmarkOne(.*)BenchmarkThree(.*)BenchmarkTwo' | ||
|
||
exec ./m.test -test.v -test.bench=. -test.shuffle=44 -test.count=2 foo_test.go | ||
stdout '^-test.shuffle 44' | ||
stdout '(?s)TestOne(.*)TestThree(.*)TestTwo(.*)TestOne(.*)TestThree(.*)TestTwo(.*)BenchmarkTwo(.*)BenchmarkOne(.*)BenchmarkThree(.*)' | ||
|
||
|
||
# Negative testcases for invalid input | ||
! go test -shuffle -count=2 | ||
stderr 'invalid value "-count=2" for flag -shuffle: -shuffle argument must be "on", "off", or an int64: strconv.ParseInt: parsing "-count=2": invalid syntax' | ||
|
||
! go test -shuffle= | ||
stderr '(?s)invalid value "" for flag -shuffle: -shuffle argument must be "on", "off", or an int64: strconv.ParseInt: parsing "": invalid syntax' | ||
|
||
! go test -shuffle=' ' | ||
stderr '(?s)invalid value " " for flag -shuffle: -shuffle argument must be "on", "off", or an int64: strconv.ParseInt: parsing " ": invalid syntax' | ||
|
||
! go test -shuffle=true | ||
stderr 'invalid value "true" for flag -shuffle: -shuffle argument must be "on", "off", or an int64: strconv.ParseInt: parsing "true": invalid syntax' | ||
|
||
! go test -shuffle='abc' | ||
stderr 'invalid value "abc" for flag -shuffle: -shuffle argument must be "on", "off", or an int64: strconv.ParseInt: parsing "abc": invalid syntax' | ||
|
||
-- go.mod -- | ||
module m | ||
|
||
go 1.16 | ||
-- foo_test.go -- | ||
package foo | ||
|
||
import "testing" | ||
|
||
func TestOne(t *testing.T) {} | ||
func TestTwo(t *testing.T) {} | ||
func TestThree(t *testing.T) {} | ||
|
||
func BenchmarkOne(b *testing.B) {} | ||
func BenchmarkTwo(b *testing.B) {} | ||
func BenchmarkThree(b *testing.B) {} | ||
|
||
-- foo.go -- | ||
package foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2021 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package rand | ||
|
||
func Int31nForTest(r *Rand, n int32) int32 { | ||
return r.int31n(n) | ||
} | ||
|
||
func GetNormalDistributionParameters() (float64, [128]uint32, [128]float32, [128]float32) { | ||
return rn, kn, wn, fn | ||
} | ||
|
||
func GetExponentialDistributionParameters() (float64, [256]uint32, [256]float32, [256]float32) { | ||
return re, ke, we, fe | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters