Skip to content

Commit a55d049

Browse files
author
Adrian Baker
committed
Skip test step execution if --dry-run is specified (#1219)
1 parent a3f847b commit a55d049

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

core/src/main/java/cucumber/api/TestCase.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public TestCase(List<TestStep> testSteps, PickleEvent pickleEvent) {
1919
this.pickleEvent = pickleEvent;
2020
}
2121

22-
public void run(EventBus bus) {
23-
boolean skipNextStep = false;
22+
public void run(EventBus bus, boolean dryRun) {
23+
boolean skipNextStep = dryRun;
2424
Long startTime = bus.getTime();
2525
bus.send(new TestCaseStarted(startTime, this));
2626
ScenarioImpl scenarioResult = new ScenarioImpl(bus, pickleEvent);

core/src/main/java/cucumber/runner/Runner.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void runUnreportedStep(String featurePath, String language, String stepNa
7777
public void runPickle(PickleEvent pickle) {
7878
buildBackendWorlds(); // Java8 step definitions will be added to the glue here
7979
TestCase testCase = createTestCaseForPickle(pickle);
80-
testCase.run(bus);
80+
testCase.run(bus, runtimeOptions.isDryRun());
8181
disposeBackendWorlds();
8282
}
8383

core/src/test/java/cucumber/api/TestCaseTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void run_wraps_execute_in_test_case_started_and_finished_events() throws
3333
when(testStep.run(eq(bus), eq(language), isA(Scenario.class), anyBoolean())).thenReturn(resultWithStatus(Result.Type.UNDEFINED));
3434

3535
TestCase testCase = new TestCase(Arrays.asList(testStep), pickleEvent());
36-
testCase.run(bus);
36+
testCase.run(bus, false);
3737

3838
InOrder order = inOrder(bus, testStep);
3939
order.verify(bus).send(isA(TestCaseStarted.class));
@@ -51,7 +51,7 @@ public void run_all_steps() throws Throwable {
5151
when(testStep2.run(eq(bus), eq(language), isA(Scenario.class), anyBoolean())).thenReturn(resultWithStatus(Result.Type.PASSED));
5252

5353
TestCase testCase = new TestCase(Arrays.asList(testStep1, testStep2), pickleEvent());
54-
testCase.run(bus);
54+
testCase.run(bus, false);
5555

5656
InOrder order = inOrder(testStep1, testStep2);
5757
order.verify(testStep1).run(eq(bus), eq(language), isA(Scenario.class), eq(false));
@@ -68,7 +68,7 @@ public void skip_steps_after_the_first_non_passed_result() throws Throwable {
6868
when(testStep2.run(eq(bus), eq(language), isA(Scenario.class), anyBoolean())).thenReturn(resultWithStatus(Result.Type.SKIPPED));
6969

7070
TestCase testCase = new TestCase(Arrays.asList(testStep1, testStep2), pickleEvent());
71-
testCase.run(bus);
71+
testCase.run(bus, false);
7272

7373
InOrder order = inOrder(testStep1, testStep2);
7474
order.verify(testStep1).run(eq(bus), eq(language), isA(Scenario.class), eq(false));

core/src/test/java/cucumber/runner/RunnerTest.java

+22-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class RunnerTest {
4040
private final Backend backend = mock(Backend.class);
4141
private final Runtime runtime = createRuntime(backend);
4242
private final Runner runner = runtime.getRunner();
43-
43+
4444
@Test
4545
public void hooks_execute_when_world_exist() throws Throwable {
4646
HookDefinition beforeHook = addBeforeHook(runtime);
@@ -61,7 +61,7 @@ public void steps_are_skipped_after_failure() throws Throwable {
6161
doThrow(RuntimeException.class).when(failingBeforeHook).execute(Matchers.<Scenario>any());
6262
StepDefinition stepDefinition = mock(StepDefinition.class);
6363

64-
runner.runPickle(createPickleEventMatchingStepDefinitions(asList(stepDefinition)));
64+
runner.runPickle(createPickleEventMatchingStepDefinitions(asList(stepDefinition), runtime));
6565

6666
InOrder inOrder = inOrder(failingBeforeHook, stepDefinition);
6767
inOrder.verify(failingBeforeHook).execute(Matchers.<Scenario>any());
@@ -83,6 +83,25 @@ public void hooks_execute_also_after_failure() throws Throwable {
8383
inOrder.verify(afterHook).execute(Matchers.<Scenario>any());
8484
}
8585

86+
@Test
87+
public void steps_are_not_executed_on_dry_run() throws Throwable {
88+
// Step without dry-run flag should be executed once
89+
{
90+
StepDefinition stepDefinition = mock(StepDefinition.class);
91+
Runtime runtime = createRuntime(backend);
92+
runtime.getRunner().runPickle(createPickleEventMatchingStepDefinitions(asList(stepDefinition), runtime));
93+
verify(stepDefinition).execute(Matchers.anyString(), Matchers.<Object[]>any());
94+
}
95+
96+
// Same step with dry-run flag should not be executred
97+
{
98+
StepDefinition stepDefinition = mock(StepDefinition.class);
99+
Runtime dryRuntime = createRuntime(backend, "--dry-run");
100+
dryRuntime.getRunner().runPickle(createPickleEventMatchingStepDefinitions(asList(stepDefinition), dryRuntime));
101+
verify(stepDefinition, never()).execute(Matchers.anyString(), Matchers.<Object[]>any());
102+
}
103+
}
104+
86105
@Test
87106
public void hooks_not_executed_in_dry_run_mode() throws Throwable {
88107
Runtime runtime = createRuntime(backend, "--dry-run");
@@ -141,7 +160,7 @@ private PickleEvent createEmptyPickleEvent() {
141160
return new PickleEvent("uri", new Pickle(NAME, ENGLISH, NO_STEPS, NO_TAGS, MOCK_LOCATIONS));
142161
}
143162

144-
private PickleEvent createPickleEventMatchingStepDefinitions(List<StepDefinition> stepDefinitions) {
163+
private PickleEvent createPickleEventMatchingStepDefinitions(List<StepDefinition> stepDefinitions, Runtime runtime) {
145164
List<PickleStep> steps = new ArrayList<PickleStep>(stepDefinitions.size());
146165
int i = 0;
147166
for (StepDefinition stepDefinition : stepDefinitions) {

0 commit comments

Comments
 (0)