Skip to content

Commit 71fd7a1

Browse files
committed
[JUnit] Make no step notifications the default.
Make the default for the JUnit module not to issue step notifications. Step notifications can be turn on with the --step-notification option to the Junit module.
1 parent 3857145 commit 71fd7a1

File tree

4 files changed

+165
-28
lines changed

4 files changed

+165
-28
lines changed

junit/src/main/java/cucumber/runtime/junit/JUnitOptions.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class JUnitOptions {
1313
private static String optionsText;
1414

1515
private boolean filenameCompatibleNames = false;
16-
private boolean stepNotifications = true;
16+
private boolean stepNotifications = false;
1717

1818
/**
1919
* Create a new instance from a list of options, for example:

junit/src/main/resources/cucumber/api/junit/OPTIONS.txt

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ JUnit Options:
77
names for certain can be used as file names.
88
For instance gradle will use these names in
99
the file names of the JUnit xml report files.
10-
--[no-]step-notifications By default steps are included in notifications
11-
and descriptions. When interacting with cucumber
12-
in an IDE it is nice to see the step executions.
13-
However presenting step executions as tests to
14-
various reporters such as surefire results in
15-
strange test counts and weird reports.
10+
--[no-]step-notifications By default steps are not included in
11+
notifications and descriptions. This aligns
12+
test case in the Cucumber-JVM domain
13+
(Scenarios) with the test case in the JUnit
14+
domain (the leafs in the description tree),
15+
and works better with the report files of
16+
the notification listeners like maven surefire
17+
or gradle.

junit/src/test/java/cucumber/runtime/junit/FeatureRunnerTest.java

+129-8
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@
2020
import java.util.Set;
2121

2222
import static java.util.Arrays.asList;
23+
import static org.junit.Assert.assertEquals;
2324
import static org.junit.Assert.assertTrue;
2425
import static org.mockito.Matchers.argThat;
2526
import static org.mockito.Mockito.inOrder;
2627
import static org.mockito.Mockito.mock;
28+
import static org.mockito.Mockito.times;
2729

2830
public class FeatureRunnerTest {
2931

3032
@Test
31-
public void should_call_formatter_for_two_scenarios_with_background() throws Throwable {
33+
public void should_not_issue_notification_for_steps_by_default_two_scenarios_with_background() throws Throwable {
3234
CucumberFeature feature = TestPickleBuilder.parseFeature("path/test.feature", "" +
3335
"Feature: feature name\n" +
3436
" Background: background\n" +
@@ -43,19 +45,85 @@ public void should_call_formatter_for_two_scenarios_with_background() throws Thr
4345

4446
InOrder order = inOrder(notifier);
4547

48+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("scenario_1 name(feature name)")));
49+
order.verify(notifier, times(3)).fireTestAssumptionFailed(argThat(new FailureMatcher("scenario_1 name(feature name)")));
50+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("scenario_1 name(feature name)")));
51+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("scenario_2 name(feature name)")));
52+
order.verify(notifier, times(2)).fireTestAssumptionFailed(argThat(new FailureMatcher("scenario_2 name(feature name)")));
53+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("scenario_2 name(feature name)")));
54+
}
55+
56+
@Test
57+
public void should_not_issue_notification_for_steps_by_default_scenario_outline_with_two_examples_table_and_background() throws Throwable {
58+
CucumberFeature feature = TestPickleBuilder.parseFeature("path/test.feature", "" +
59+
"Feature: feature name\n" +
60+
" Background: background\n" +
61+
" Given first step\n" +
62+
" Scenario Outline: scenario outline name\n" +
63+
" When <x> step\n" +
64+
" Then <y> step\n" +
65+
" Examples: examples 1 name\n" +
66+
" | x | y |\n" +
67+
" | second | third |\n" +
68+
" | second | third |\n" +
69+
" Examples: examples 2 name\n" +
70+
" | x | y |\n" +
71+
" | second | third |\n");
72+
73+
RunNotifier notifier = runFeatureWithNotifier(feature);
74+
75+
InOrder order = inOrder(notifier);
76+
77+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("scenario outline name(feature name)")));
78+
order.verify(notifier, times(3)).fireTestAssumptionFailed(argThat(new FailureMatcher("scenario outline name(feature name)")));
79+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("scenario outline name(feature name)")));
80+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("scenario outline name(feature name)")));
81+
order.verify(notifier, times(3)).fireTestAssumptionFailed(argThat(new FailureMatcher("scenario outline name(feature name)")));
82+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("scenario outline name(feature name)")));
83+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("scenario outline name(feature name)")));
84+
order.verify(notifier, times(3)).fireTestAssumptionFailed(argThat(new FailureMatcher("scenario outline name(feature name)")));
85+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("scenario outline name(feature name)")));
86+
}
87+
88+
@Test
89+
public void step_notification_can_be_turned_on_two_scenarios_with_background() throws Throwable {
90+
CucumberFeature feature = TestPickleBuilder.parseFeature("path/test.feature", "" +
91+
"Feature: feature name\n" +
92+
" Background: background\n" +
93+
" Given first step\n" +
94+
" Scenario: scenario_1 name\n" +
95+
" When second step\n" +
96+
" Then third step\n" +
97+
" Scenario: scenario_2 name\n" +
98+
" Then another second step\n");
99+
100+
RunNotifier notifier = runFeatureWithNotifier(feature, "--step-notifications");
101+
102+
InOrder order = inOrder(notifier);
103+
46104
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("scenario_1 name")));
105+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("first step(scenario_1 name)")));
47106
order.verify(notifier).fireTestAssumptionFailed(argThat(new FailureMatcher("first step(scenario_1 name)")));
107+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("first step(scenario_1 name)")));
108+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("second step(scenario_1 name)")));
48109
order.verify(notifier).fireTestAssumptionFailed(argThat(new FailureMatcher("second step(scenario_1 name)")));
110+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("second step(scenario_1 name)")));
111+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("third step(scenario_1 name)")));
49112
order.verify(notifier).fireTestAssumptionFailed(argThat(new FailureMatcher("third step(scenario_1 name)")));
113+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("third step(scenario_1 name)")));
50114
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("scenario_1 name")));
51115
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("scenario_2 name")));
116+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("first step(scenario_2 name)")));
52117
order.verify(notifier).fireTestAssumptionFailed(argThat(new FailureMatcher("first step(scenario_2 name)")));
118+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("first step(scenario_2 name)")));
119+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("another second step(scenario_2 name)")));
53120
order.verify(notifier).fireTestAssumptionFailed(argThat(new FailureMatcher("another second step(scenario_2 name)")));
121+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("another second step(scenario_2 name)")));
54122
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("scenario_2 name")));
55123
}
56124

57125
@Test
58-
public void should_call_formatter_for_scenario_outline_with_two_examples_table_and_background() throws Throwable {
126+
public void step_notification_can_be_turned_on_scenario_outline_with_two_examples_table_and_background() throws Throwable {
59127
CucumberFeature feature = TestPickleBuilder.parseFeature("path/test.feature", "" +
60128
"Feature: feature name\n" +
61129
" Background: background\n" +
@@ -71,29 +139,47 @@ public void should_call_formatter_for_scenario_outline_with_two_examples_table_a
71139
" | x | y |\n" +
72140
" | second | third |\n");
73141

74-
RunNotifier notifier = runFeatureWithNotifier(feature);
142+
RunNotifier notifier = runFeatureWithNotifier(feature, "--step-notifications");
75143

76144
InOrder order = inOrder(notifier);
77145

78146
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("scenario outline name")));
147+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("first step(scenario outline name)")));
79148
order.verify(notifier).fireTestAssumptionFailed(argThat(new FailureMatcher("first step(scenario outline name)")));
149+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("first step(scenario outline name)")));
150+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("second step(scenario outline name)")));
80151
order.verify(notifier).fireTestAssumptionFailed(argThat(new FailureMatcher("second step(scenario outline name)")));
152+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("second step(scenario outline name)")));
153+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("third step(scenario outline name)")));
81154
order.verify(notifier).fireTestAssumptionFailed(argThat(new FailureMatcher("third step(scenario outline name)")));
155+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("third step(scenario outline name)")));
82156
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("scenario outline name")));
83157
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("scenario outline name")));
158+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("first step(scenario outline name)")));
84159
order.verify(notifier).fireTestAssumptionFailed(argThat(new FailureMatcher("first step(scenario outline name)")));
160+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("first step(scenario outline name)")));
161+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("second step(scenario outline name)")));
85162
order.verify(notifier).fireTestAssumptionFailed(argThat(new FailureMatcher("second step(scenario outline name)")));
163+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("second step(scenario outline name)")));
164+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("third step(scenario outline name)")));
86165
order.verify(notifier).fireTestAssumptionFailed(argThat(new FailureMatcher("third step(scenario outline name)")));
166+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("third step(scenario outline name)")));
87167
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("scenario outline name")));
88168
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("scenario outline name")));
169+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("first step(scenario outline name)")));
89170
order.verify(notifier).fireTestAssumptionFailed(argThat(new FailureMatcher("first step(scenario outline name)")));
171+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("first step(scenario outline name)")));
172+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("second step(scenario outline name)")));
90173
order.verify(notifier).fireTestAssumptionFailed(argThat(new FailureMatcher("second step(scenario outline name)")));
174+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("second step(scenario outline name)")));
175+
order.verify(notifier).fireTestStarted(argThat(new DescriptionMatcher("third step(scenario outline name)")));
91176
order.verify(notifier).fireTestAssumptionFailed(argThat(new FailureMatcher("third step(scenario outline name)")));
177+
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("third step(scenario outline name)")));
92178
order.verify(notifier).fireTestFinished(argThat(new DescriptionMatcher("scenario outline name")));
93179
}
94180

95-
private RunNotifier runFeatureWithNotifier(CucumberFeature cucumberFeature) throws InitializationError {
96-
FeatureRunner runner = createFeatureRunner(cucumberFeature);
181+
private RunNotifier runFeatureWithNotifier(CucumberFeature cucumberFeature, String... options) throws InitializationError {
182+
FeatureRunner runner = createFeatureRunner(cucumberFeature, options);
97183
RunNotifier notifier = mock(RunNotifier.class);
98184
runner.run(notifier);
99185
return notifier;
@@ -116,7 +202,7 @@ private FeatureRunner createFeatureRunner(CucumberFeature cucumberFeature, JUnit
116202

117203

118204
@Test
119-
public void shouldPopulateDescriptionsWithStableUniqueIds() throws Exception {
205+
public void should_populate_descriptions_with_stable_unique_ids() throws Exception {
120206
CucumberFeature cucumberFeature = TestPickleBuilder.parseFeature("path/test.feature", "" +
121207
"Feature: feature name\n" +
122208
" Background:\n" +
@@ -146,7 +232,7 @@ public void shouldPopulateDescriptionsWithStableUniqueIds() throws Exception {
146232
}
147233

148234
@Test
149-
public void shouldNotCreateStepDescriptions() throws Exception {
235+
public void should_not_create_step_descriptions_by_default() throws Exception {
150236
CucumberFeature cucumberFeature = TestPickleBuilder.parseFeature("path/test.feature", "" +
151237
"Feature: feature name\n" +
152238
" Background:\n" +
@@ -165,7 +251,7 @@ public void shouldNotCreateStepDescriptions() throws Exception {
165251

166252
);
167253

168-
FeatureRunner runner = createFeatureRunner(cucumberFeature, "--no-step-notifications");
254+
FeatureRunner runner = createFeatureRunner(cucumberFeature);
169255

170256
Description feature = runner.getDescription();
171257
Description scenarioA = feature.getChildren().get(0);
@@ -180,6 +266,41 @@ public void shouldNotCreateStepDescriptions() throws Exception {
180266
assertTrue(scenarioC2.getChildren().isEmpty());
181267
}
182268

269+
@Test
270+
public void step_descriptions_can_be_turned_on() throws Exception {
271+
CucumberFeature cucumberFeature = TestPickleBuilder.parseFeature("path/test.feature", "" +
272+
"Feature: feature name\n" +
273+
" Background:\n" +
274+
" Given background step\n" +
275+
" Scenario: A\n" +
276+
" Then scenario name\n" +
277+
" Scenario: B\n" +
278+
" Then scenario name\n" +
279+
" Scenario Outline: C\n" +
280+
" Then scenario <name>\n" +
281+
" Examples:\n" +
282+
" | name |\n" +
283+
" | C |\n" +
284+
" | D |\n" +
285+
" | E |\n"
286+
287+
);
288+
289+
FeatureRunner runner = createFeatureRunner(cucumberFeature, "--step-notifications");
290+
291+
Description feature = runner.getDescription();
292+
Description scenarioA = feature.getChildren().get(0);
293+
assertEquals(2, scenarioA.getChildren().size());
294+
Description scenarioB = feature.getChildren().get(1);
295+
assertEquals(2, scenarioB.getChildren().size());
296+
Description scenarioC0 = feature.getChildren().get(2);
297+
assertEquals(2, scenarioC0.getChildren().size());
298+
Description scenarioC1 = feature.getChildren().get(3);
299+
assertEquals(2, scenarioC1.getChildren().size());
300+
Description scenarioC2 = feature.getChildren().get(4);
301+
assertEquals(2, scenarioC2.getChildren().size());
302+
}
303+
183304
private static void assertDescriptionIsUnique(Description description, Set<Description> descriptions) {
184305
// Note: JUnit uses the the serializable parameter as the unique id when comparing Descriptions
185306
assertTrue(descriptions.add(description));

0 commit comments

Comments
 (0)