Skip to content

proposal: testing: expose -test.shuffle seed in a public function. #66623

Description

@ucirello

Proposal Details

#28592 added support for go test -shuffle off|on|seed.

#61256 debates the need to redo the seed when executing tests -count times.

This proposal would expose to the tests the seed used on invocation so as to enable reproducible tests.

Currently, this workaround is possible:

func TestMain(m *testing.M) {
	flag.Parse()
	shuffleFlag := flag.Lookup("test.shuffle")
	if shuffleFlag != nil {
		shuffle := shuffleFlag.Value.String()
		if shuffle != "on" && shuffle != "off" && shuffle != ""  {
			n, err = strconv.ParseInt(shuffle, 10, 64)
			if err != nil {
				fmt.Fprintln(os.Stderr, `testing: -shuffle should be "off", "on", or a valid integer:`, err)
				os.Exit(2)
				return
			}
			rand.Seed(n)
		}
	}
	os.Exit(m.Run())
}

This workaround has two problems. First problem, it relies on a deprecated interface (math/rand.Seed). Second problem, it will not share the same seed with -test.shuffle on.

Thus this proposal modifies testing to add this call.

// ShuffleSeed reports the environment random seed.
func ShuffleSeed() int64

With this interface, one can use:

$ go test -shuffle 12389761 -run=TestRandomnessSensitive # specific seed that causes a problem to happen

Code:

func TestRandomnessSensitive(t *testing.T) {
        rng := rand.New(rand.NewSource(testing.ShuffleSeed()))
        _ = rng // use RNG that will cause the issue.
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions