Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test iterations vary based on the -short flag #103

Open
armfazh opened this issue Apr 14, 2020 · 0 comments
Open

Test iterations vary based on the -short flag #103

armfazh opened this issue Apr 14, 2020 · 0 comments

Comments

@armfazh
Copy link
Contributor

armfazh commented Apr 14, 2020

Not an issue that needs to be addressed right now, but it can be quite nice for things like this is to allow the iterations to vary based on the -short flag. For example:

func NumTrials() {
    if testing.Short() {
        return 16
    }
    return 1 << 8 // or more
}

If you want to get even more fancy, something I've done before is to also define custom -long and -stress flags. So you can do:

// Custom test command line flags.
var (
	long   = flag.Bool("long", false, "enable long running tests")
	stress = flag.Bool("stress", false, "enable stress tests (implies -long)")
)

// timeallowed returns how long a single test is allowed to take.
func timeallowed() time.Duration {
	switch {
	case testing.Short():
		return time.Second / 10
	case *long:
		return 30 * time.Second
	case *stress:
		return 2 * time.Minute
	default:
		return time.Second
	}
}

// Repeat the given trial function. The duration is controlled by custom
// command-line flags. The trial function returns whether it wants to continue
// testing.
//
//	-short		run for less time than usual
//	-long		allow more time
//	-stress		run for an extremely long time
func Repeat(t *testing.T, trial func(t *testing.T) bool) {
	start := time.Now()
	d := timeallowed()
	n := 1
	for time.Since(start) < d && trial(t) {
		n++
	}
	t.Logf("%d trials in %s", n, time.Since(start))
}

I usually put something like this in an internal/test package. Then you wrap your test in a test.Repeat(...) call and it will run until the time limit.

Originally posted by @mmcloughlin in #102

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant