Navigation Menu

Skip to content

Commit

Permalink
Allow to change tests order.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Jan 13, 2017
1 parent 34bdb29 commit 2505c51
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/gophers/placehold/main-load.go
Expand Up @@ -39,5 +39,5 @@ func main() {
panic(err)
}

exitCode = r.Load(nil, l, runner.FailStep)
exitCode = r.Load(nil, 0, l, runner.FailStep)
}
2 changes: 1 addition & 1 deletion examples/gophers/placehold/main-test.go
Expand Up @@ -33,5 +33,5 @@ func main() {
r := runner.New("", log.New(os.Stderr, "", 0))
r.Add("TestBasic", placehold.TestBasic, 1)

exitCode = r.Test(nil)
exitCode = r.Test(nil, 0)
}
4 changes: 2 additions & 2 deletions gophers/cmd/import.go
Expand Up @@ -64,14 +64,14 @@ func main() {
{{- end }}
{{ if not .Load }}
exitCode = r.Test(nil)
exitCode = r.Test(nil, 0)
{{ else }}
l, err := runner.NewStepLoader(5, 10, 1, 1 * time.Second)
if err != nil {
panic(err)
}
exitCode = r.Load(nil, l, runner.FailStep)
exitCode = r.Load(nil, 0, l, runner.FailStep)
{{ end -}}
}
`))
Expand Down
28 changes: 22 additions & 6 deletions gophers/runner/runner.go
Expand Up @@ -146,10 +146,24 @@ func run(test TestFunc, l *log.Logger) state {
return <-result
}

// Test runs tests matching regexp in random order.
func (r *Runner) Test(re *regexp.Regexp) int {
func shuffle(n int, seed int64) []int {
if seed == 0 {
res := make([]int, n)
for i := 0; i < n; i++ {
res[i] = i
}
return res
}

return rand.New(rand.NewSource(seed)).Perm(n)
}

// Test runs tests matching regexp in order defined by seed.
// If seed is 0, tests are run sequentially.
// Otherwise, seed defines random order of tests.
func (r *Runner) Test(re *regexp.Regexp, seed int64) int {
var failedTests, skippedTests []string
for _, p := range rand.Perm(len(r.tests)) {
for _, p := range shuffle(len(r.tests), seed) {
test := r.tests[p]
if re != nil && !re.MatchString(test.name) {
continue
Expand Down Expand Up @@ -273,11 +287,13 @@ func (r *Runner) load(test *addedTest, loader Loader, failMode FailMode) (worstO
}
}

// Load runs tests matching regexp in random order in load test mode.
func (r *Runner) Load(re *regexp.Regexp, loader Loader, failMode FailMode) int {
// Test runs tests matching regexp in order defined by seed in load test mode.
// If seed is 0, tests are run sequentially.
// Otherwise, seed defines random order of tests.
func (r *Runner) Load(re *regexp.Regexp, seed int64, loader Loader, failMode FailMode) int {
reporter := newReporter(r.l)
var failedTests, skippedTests []string
for _, p := range rand.Perm(len(r.tests)) {
for _, p := range shuffle(len(r.tests), seed) {
test := r.tests[p]
if re != nil && !re.MatchString(test.name) {
continue
Expand Down

0 comments on commit 2505c51

Please sign in to comment.