Skip to content

Commit 9d41f80

Browse files
authored
[Core] TestCaseState should be PASSED by default (#1888)
Currently `TestCaseState` defaults to `Status.UNDEFINED` when no steps have been executed. This is odd because the implementation otherwise collects the most severe result. So the logical expectation would be that it would return `Status.PASSED` ```java @OverRide public Status getStatus() { if (stepResults.isEmpty()) { return Status.UNDEFINED; } Result mostSevereResult = max(stepResults, comparing(Result::getStatus)); return Status.valueOf(mostSevereResult.getStatus().name()); } ``` As a result the `TestCaseResultObserver` in `junit-platform-engine` and `testng` needs to check if an undefined scenario was empty or not. This means our abstraction is rather leaky. ```java if (status.is(UNDEFINED)) { if (suggestions.isEmpty()) { // Empty scenario return; } throw new UndefinedStepException(suggestions, strict); } ``` By stating that the empty test is passed by default and making `TestCaseState` default to `Status.PASSED` we resolve this complexity. As a result `Scenario.getState` will return `PASSED` rather then `UNDEFINED` prior to the execution of the first step of a scenario.
1 parent adc0479 commit 9d41f80

File tree

10 files changed

+31
-15
lines changed

10 files changed

+31
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616
### Removed
1717

1818
### Fixed
19+
* [Core] TestCaseState should be PASSED by default ([#1888](https://github.com/cucumber/cucumber-jvm/pull/1888) M.P. Korstanje)
20+
* As a result `Scenario.getState` will return `PASSED` rather then
21+
`UNDEFINED` prior to the execution of the first step of a scenario.
1922

2023
## [5.2.0] (2020-02-06)
2124

core/src/main/java/io/cucumber/core/backend/TestCaseState.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ public interface TestCaseState {
1414
Collection<String> getSourceTagNames();
1515

1616
/**
17-
* @return the <em>most severe</em> status of the Scenario's Steps.
17+
* Returns the current status of this test case.
18+
* <p>
19+
* The test case status is calculate as the most severe status of the
20+
* executed steps in the testcase so far.
21+
*
22+
* @return the current status of this test case
1823
*/
1924
Status getStatus();
2025

core/src/main/java/io/cucumber/core/runner/TestCaseState.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Collection<String> getSourceTagNames() {
3939
@Override
4040
public Status getStatus() {
4141
if (stepResults.isEmpty()) {
42-
return Status.UNDEFINED;
42+
return Status.PASSED;
4343
}
4444

4545
Result mostSevereResult = max(stepResults, comparing(Result::getStatus));

core/src/test/java/io/cucumber/core/runner/TestCaseStateResultTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ void setup() {
5959
}
6060

6161
@Test
62-
void no_steps_is_undefined() {
63-
assertThat(s.getStatus(), is(equalTo(UNDEFINED)));
62+
void no_steps_is_passed() {
63+
assertThat(s.getStatus(), is(equalTo(PASSED)));
6464
}
6565

6666
@Test

java/src/main/java/io/cucumber/java/Scenario.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public Collection<String> getSourceTagNames() {
2727
return delegate.getSourceTagNames();
2828
}
2929

30+
/**
31+
* Returns the current status of this scenario.
32+
* <p>
33+
* The scenario status is calculate as the most severe status of the
34+
* executed steps in the scenario so far.
35+
*
36+
* @return the current status of this scenario
37+
*/
3038
public Status getStatus() {
3139
return Status.valueOf(delegate.getStatus().name());
3240
}

java8/src/main/java/io/cucumber/java8/Scenario.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public Collection<String> getSourceTagNames() {
2727
return delegate.getSourceTagNames();
2828
}
2929

30+
/**
31+
* Returns the current status of this scenario.
32+
* <p>
33+
* The scenario status is calculate as the most severe status of the
34+
* executed steps in the scenario so far.
35+
*
36+
* @return the current status of this scenario
37+
*/
3038
public Status getStatus() {
3139
return Status.valueOf(delegate.getStatus().name());
3240
}

junit-platform-engine/src/main/java/io/cucumber/junit/platform/engine/TestCaseResultObserver.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,6 @@ void assertTestCasePassed() {
9999
}
100100

101101
if (status.is(UNDEFINED)) {
102-
if (suggestions.isEmpty()) {
103-
// Empty scenario
104-
return;
105-
}
106102
throw new UndefinedStepException(suggestions);
107103
}
108104

junit-platform-engine/src/test/java/io/cucumber/junit/platform/engine/TestCaseResultObserverTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ void undefined() {
221221
@Test
222222
void empty() {
223223
bus.send(new TestCaseStarted(Instant.now(), testCase));
224-
Result result = new Result(Status.UNDEFINED, Duration.ZERO, null);
224+
Result result = new Result(Status.PASSED, Duration.ZERO, null);
225225
bus.send(new TestCaseFinished(Instant.now(), testCase, result));
226226
observer.assertTestCasePassed();
227227
}

testng/src/main/java/io/cucumber/testng/TestCaseResultObserver.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,6 @@ void assertTestCasePassed() throws Throwable {
109109
}
110110

111111
if (status.is(UNDEFINED)) {
112-
if (suggestions.isEmpty()) {
113-
// Empty scenario
114-
return;
115-
}
116-
117112
throw new UndefinedStepException(suggestions, strict);
118113
}
119114

testng/src/test/java/io/cucumber/testng/TestCaseResultObserverTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static io.cucumber.plugin.event.Status.AMBIGUOUS;
2020
import static io.cucumber.plugin.event.Status.FAILED;
21+
import static io.cucumber.plugin.event.Status.PASSED;
2122
import static io.cucumber.plugin.event.Status.PENDING;
2223
import static io.cucumber.plugin.event.Status.SKIPPED;
2324
import static io.cucumber.plugin.event.Status.UNDEFINED;
@@ -142,7 +143,7 @@ public void should_not_be_skipped_for_undefined_result_in_strict_mode() {
142143
public void should_be_passed_for_empty_scenario() throws Throwable {
143144
TestCaseResultObserver resultListener = TestCaseResultObserver.observe(bus, false);
144145

145-
Result testCaseResult = new Result(UNDEFINED, ZERO, error);
146+
Result testCaseResult = new Result(PASSED, ZERO, error);
146147
bus.send(new TestCaseFinished(now(), testCase, testCaseResult));
147148

148149
resultListener.assertTestCasePassed();

0 commit comments

Comments
 (0)