Skip to content

Commit 187f3af

Browse files
committed
New built-in, global RandomRunOrderExtension
Supports run order randomization for specifications, features or a combination of both. Relates to #1443.
1 parent 0d2d0bb commit 187f3af

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

docs/extensions.adoc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ runner {
4444

4545
See the <<parallel_execution.adoc#parallel-execution,Parallel Execution>> section for a detailed description.
4646

47+
=== Random Test Order Configuration
48+
49+
[source,groovy]
50+
----
51+
runner {
52+
randomizeSpecRunOrder true // randomize specification run order
53+
randomizeFeatureRunOrder true // randomize feature run order within each specification
54+
}
55+
----
56+
57+
See the <<#_randomize_run_order>> section for more details.
58+
4759
== Built-In Extensions
4860

4961
Most of Spock's built-in extensions are _annotation-driven_. In other words, they are triggered by annotating a
@@ -709,6 +721,24 @@ runner {
709721
}
710722
----
711723

724+
=== Randomize Run Order
725+
726+
Ideally, automated tests in general and Spock specifications in particular should be independent of each other. The same
727+
applies to feature methods within a specification. One heuristic way to gain confidence that this is indeed the case, is
728+
to randomize the order in which specifications and/or features within each specification are executed.
729+
730+
By default, Spock executes both specifications and features in deterministic order, even though users should not rely on
731+
any particular run order. You can explicitly activate run order randomization separately for specifications and features
732+
or combine both randomization modes, using the <<spock-configuration-file,Spock Configuration File>>:
733+
734+
[source,groovy]
735+
----
736+
runner {
737+
randomizeSpecRunOrder true // randomize specification run order
738+
randomizeFeatureRunOrder true // randomize feature run order within each specification
739+
}
740+
----
741+
712742
== Third-Party Extensions
713743

714744
You can find a list of third-party extensions in the https://github.com/spockframework/spock/wiki/Third-Party-Extensions[Spock Wiki].

docs/release_notes.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ include::include.adoc[]
66
== 2.4 (tbd)
77

88
* Support for setting execution order (a.k.a. run order) in `SpecInfo`, can be used by extensions
9+
* New built-in `RandomRunOrderExtension` supports run order randomization for specifications, features or a combination of both.
10+
See manual section <<extensions#_randomize_run_order,Randomize Run Order>>.
911

1012
== 2.4-M1 (2022-11-30)
1113

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* https://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package org.spockframework.runtime.extension.builtin;
16+
17+
import org.spockframework.runtime.extension.IGlobalExtension;
18+
import org.spockframework.runtime.model.FeatureInfo;
19+
import org.spockframework.runtime.model.SpecInfo;
20+
import spock.config.RunnerConfiguration;
21+
22+
import java.util.Random;
23+
24+
public class RandomRunOrderExtension implements IGlobalExtension {
25+
private static final Random RANDOM = new Random();
26+
27+
private final RunnerConfiguration config;
28+
29+
public RandomRunOrderExtension(RunnerConfiguration config) {
30+
this.config = config;
31+
}
32+
33+
@Override
34+
public void visitSpec(SpecInfo spec) {
35+
if (config.randomizeSpecRunOrder)
36+
spec.setExecutionOrder(RANDOM.nextInt());
37+
if (config.randomizeFeatureRunOrder) {
38+
for (FeatureInfo featureInfo : spec.getAllFeatures())
39+
featureInfo.setExecutionOrder(RANDOM.nextInt());
40+
}
41+
}
42+
}

spock-core/src/main/java/spock/config/RunnerConfiguration.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
* annotation some.pkg.Slow
2929
* baseClass IntegrationSpec
3030
* }
31-
* filterStackTrace true // this is the default
31+
* filterStackTrace true // this is the default
32+
* randomizeSpecRunOrder false // this is the default
33+
* randomizeFeatureRunOrder false // this is the default
3234
* }
3335
* </pre>
3436
*/
@@ -39,4 +41,6 @@ public class RunnerConfiguration {
3941
public ParallelConfiguration parallel = new ParallelConfiguration();
4042
public boolean filterStackTrace = true;
4143
public boolean optimizeRunOrder = false;
44+
public boolean randomizeSpecRunOrder = false;
45+
public boolean randomizeFeatureRunOrder = false;
4246
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
org.spockframework.runtime.extension.builtin.IncludeExcludeExtension
22
org.spockframework.runtime.extension.builtin.OptimizeRunOrderExtension
3+
org.spockframework.runtime.extension.builtin.RandomRunOrderExtension
34
org.spockframework.runtime.extension.builtin.UnrollExtension

0 commit comments

Comments
 (0)