Skip to content

Commit

Permalink
adopted suggestion from @tgodzik
Browse files Browse the repository at this point in the history
fail test in case of Throwable not only of NonFatal otherwise the "throwing an exception fails the test" would have just aborted the execution of the tests without indicating an error
  • Loading branch information
mzuehlke committed Apr 16, 2024
1 parent 30e97c3 commit 438a7eb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
6 changes: 4 additions & 2 deletions munit/shared/src/main/scala/munit/Assertions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,10 @@ trait Assertions extends MacroCompat.CompileErrorMacro {
fail(expectedExceptionMsg)
} catch {
case e: FailExceptionLike[_]
if !T.runtimeClass.isAssignableFrom(e.getClass()) ||
e.getMessage.contains(expectedExceptionMsg) =>
if !T.runtimeClass.isAssignableFrom(e.getClass()) =>
throw e
case e: FailExceptionLike[_]
if e.getMessage.contains(expectedExceptionMsg) =>
throw e
case NonFatal(e) =>
if (T.runtimeClass.isAssignableFrom(e.getClass())) {
Expand Down
3 changes: 1 addition & 2 deletions munit/shared/src/main/scala/munit/FunSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package munit

import scala.collection.mutable
import scala.concurrent.Future
import scala.util.control.NonFatal
import munit.internal.PlatformCompat
import scala.concurrent.duration.Duration
import scala.concurrent.duration.FiniteDuration
Expand Down Expand Up @@ -36,7 +35,7 @@ trait BaseFunSuite
try {
waitForCompletion(() => munitValueTransform(body))
} catch {
case NonFatal(e) =>
case e: Throwable =>
Future.failed(e)
}
},
Expand Down
7 changes: 6 additions & 1 deletion tests/shared/src/test/scala/munit/FailExceptionSuite.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package munit

class FailExceptionSuite extends BaseSuite {
test("throwing an exception fails the test".fail) {
throw new RuntimeException("asdf")
println("should not reach here")
}

test("assertion-error") {
val e = intercept[AssertionError] {
fail("hello world!")
}
assert(clue(e).isInstanceOf[Serializable])
}

test("assertion-error-no-exception") {
test("assertion-error-no-exception") {
try {
intercept[AssertionError] {
println("throwing no exception!")
Expand Down

0 comments on commit 438a7eb

Please sign in to comment.