Skip to content

Commit 1a790ce

Browse files
committed
Break the build if beforeAllthrows exception
1 parent e75c16c commit 1a790ce

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

src/main/java/co/helmethair/scalatest/runtime/Executor.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,29 @@ private void executeSuite(TestDescriptor test, JUnitReporter reporter) {
7474
List<ScalatestTestDescriptor> tests = children.stream().filter(c -> c instanceof ScalatestTestDescriptor)
7575
.map(c -> (ScalatestTestDescriptor) c).collect(Collectors.toList());
7676

77-
Set<TestDescriptor> nonTests = new HashSet<>(children);
78-
nonTests.removeAll(tests);
77+
Set<TestDescriptor> subSuites = new HashSet<>(children);
78+
subSuites.removeAll(tests);
7979

80+
subSuites.stream()
81+
.sorted(Comparator.comparing(TestDescriptor::getDisplayName))
82+
.forEach(c -> executeTest(c, reporter));
83+
84+
boolean suitExecutedOk = true;
8085
if (!tests.isEmpty()) {
81-
runScalatests((ScalatestSuiteDescriptor) test,
86+
suitExecutedOk = runScalatests((ScalatestSuiteDescriptor) test,
8287
tests.stream()
8388
.sorted(Comparator.comparing(TestDescriptor::getDisplayName))
8489
.collect(Collectors.toList()),
8590
reporter);
8691
}
8792

88-
nonTests.stream()
89-
.sorted(Comparator.comparing(TestDescriptor::getDisplayName))
90-
.forEach(c -> executeTest(c, reporter));
91-
92-
reporter.getJunitListener().executionFinished(test, TestExecutionResult.successful());
93+
if (suitExecutedOk){
94+
// if exception is thrown during suit execution (init, before/after all) we should not report a SUCCESS
95+
reporter.getJunitListener().executionFinished(test, TestExecutionResult.successful());
96+
}
9397
}
9498

95-
private void runScalatests(ScalatestSuiteDescriptor containingSuite, List<ScalatestTestDescriptor> tests, JUnitReporter reporter) {
99+
private boolean runScalatests(ScalatestSuiteDescriptor containingSuite, List<ScalatestTestDescriptor> tests, JUnitReporter reporter) {
96100

97101
Suite scalasuite = containingSuite.getScalasuite();
98102

@@ -125,6 +129,7 @@ private void runScalatests(ScalatestSuiteDescriptor containingSuite, List<Scalat
125129
try {
126130
Status status = scalasuite.run(Option.apply(null), args);
127131
status.waitUntilCompleted();
132+
return true;
128133
} catch (Throwable e) {
129134
if (e instanceof InstantiationException || e instanceof IllegalAccessException) {
130135
reporter.apply(suiteAborted(args.tracker().nextOrdinal(), e, Resources.cannotInstantiateSuite(e.getMessage()), scalasuite));
@@ -139,6 +144,7 @@ private void runScalatests(ScalatestSuiteDescriptor containingSuite, List<Scalat
139144
throw e;
140145
}
141146
}
147+
return false;
142148
}
143149
}
144150

src/test/java/co/helmethair/scalatest/BeforeAfterTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,22 @@ void afterAllFailedTest() {
117117
verifyTestSuccessReported("[engine:scalatest]/[suite:tests.FailInAfterAllTest]/[test:test 2]", listener);
118118
verifyTestFailReportedWith("[engine:scalatest]/[suite:tests.FailInAfterAllTest]", listener, null);
119119
}
120+
121+
@Test
122+
void beforeAllThrowsExceptionTest() {
123+
EngineDiscoveryRequest discoveryRequest = createClassDiscoveryRequest("tests.ExceptionBeforeAllTest");
124+
TestDescriptor discoveredTests = engine.discover(discoveryRequest, engineId);
125+
TestEngineExecutionListener listener = spy(new TestEngineExecutionListener());
126+
ExecutionRequest executionRequest = new ExecutionRequest(discoveredTests, listener, null);
127+
128+
Map<String, Integer> calls = new HashMap<String, Integer>() {{
129+
put("beforeAll begin", 1);
130+
put("afterEach", 0);
131+
put("runs", 0);
132+
put("runs again", 0);
133+
}};
134+
135+
verifyTestExecuteCode(calls, () -> engine.execute(executionRequest));
136+
verifyTestFailReportedWith("[engine:scalatest]/[suite:tests.ExceptionBeforeAllTest]", listener, null);
137+
}
120138
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package tests
2+
3+
import co.helmethair.scalatest.helper.RegisterCall
4+
import org.scalatest.BeforeAndAfterAll
5+
import org.scalatest.funspec.AnyFunSpec
6+
7+
class ExceptionBeforeAllTest extends AnyFunSpec with RegisterCall with BeforeAndAfterAll {
8+
9+
override def beforeAll(): Unit = {
10+
super.beforeAll()
11+
register("beforeAll begin")
12+
throw new Exception("before all faliled")
13+
}
14+
15+
describe("BeforeAndAfterAll") {
16+
it("runs") {
17+
register("runs")
18+
}
19+
20+
it("runs again") {
21+
register("runs again")
22+
}
23+
}
24+
25+
override def afterAll(): Unit = {
26+
register("afterAll")
27+
}
28+
}

0 commit comments

Comments
 (0)