File tree 2 files changed +27
-2
lines changed
kotlinx-coroutines-test/common
2 files changed +27
-2
lines changed Original file line number Diff line number Diff line change @@ -148,7 +148,8 @@ public expect class TestResult
148
148
* #### Reported exceptions
149
149
*
150
150
* Exceptions reported to the test coroutine scope via [TestCoroutineScope.reportException] will be thrown at the end.
151
- * By default, unless an explicit [TestExceptionHandler] is passed, this includes all unhandled exceptions.
151
+ * By default, unless an explicit [TestExceptionHandler] is passed, this includes all unhandled exceptions. If the test
152
+ * body also fails, the reported exceptions are suppressed by it.
152
153
*
153
154
* #### Uncompleted coroutines
154
155
*
@@ -219,7 +220,7 @@ public fun runTest(
219
220
} catch (e: UncompletedCoroutinesError ) {
220
221
// it's normal that some jobs are not completed if the test body has failed, won't clutter the output
221
222
} catch (e: Throwable ) {
222
- e.printStackTrace( )
223
+ it.addSuppressed(e )
223
224
}
224
225
throw it
225
226
}
Original file line number Diff line number Diff line change @@ -216,4 +216,28 @@ class RunTestTest {
216
216
}
217
217
}
218
218
})
219
+
220
+ /* * Tests that, when the test body fails, the reported exceptions are suppressed. */
221
+ @Test
222
+ fun testSuppressedExceptions () = testResultMap({
223
+ try {
224
+ it()
225
+ fail(" should not be reached" )
226
+ } catch (e: TestException ) {
227
+ assertEquals(" w" , e.message)
228
+ val suppressed = e.suppressedExceptions +
229
+ (e.suppressedExceptions.firstOrNull()?.suppressedExceptions ? : emptyList())
230
+ assertEquals(3 , suppressed.size)
231
+ assertEquals(" x" , suppressed[0 ].message)
232
+ assertEquals(" y" , suppressed[1 ].message)
233
+ assertEquals(" z" , suppressed[2 ].message)
234
+ }
235
+ }, {
236
+ runTest {
237
+ reportException(TestException (" x" ))
238
+ reportException(TestException (" y" ))
239
+ reportException(TestException (" z" ))
240
+ throw TestException (" w" )
241
+ }
242
+ })
219
243
}
You can’t perform that action at this time.
0 commit comments