diff --git a/munit/shared/src/main/scala/munit/FailExceptionLike.scala b/munit/shared/src/main/scala/munit/FailExceptionLike.scala index c7bfa19c..cd849d7e 100644 --- a/munit/shared/src/main/scala/munit/FailExceptionLike.scala +++ b/munit/shared/src/main/scala/munit/FailExceptionLike.scala @@ -16,6 +16,7 @@ package munit trait FailExceptionLike[T <: AssertionError] extends Serializable { self: AssertionError => def withMessage(message: String): T + def updateMessage(f: String => String): T = withMessage(f(getMessage)) def location: Location def isStackTracesEnabled: Boolean } diff --git a/munit/shared/src/main/scala/munit/TestTransforms.scala b/munit/shared/src/main/scala/munit/TestTransforms.scala index 880b5c41..ca7f7ccc 100644 --- a/munit/shared/src/main/scala/munit/TestTransforms.scala +++ b/munit/shared/src/main/scala/munit/TestTransforms.scala @@ -78,4 +78,39 @@ trait TestTransforms { this: BaseFunSuite => } ) + /** + * Optionally augment a failure with additional information. + * + * Failures that are not `FailExceptionLike` subclasses will be wrapped, if needed. + */ + def munitAppendToFailureMessage( + buildSuffix: Test => Option[String] + ): TestTransform = + new TestTransform( + "failureSuffix", + { t => + t.withBodyMap( + _.transformCompat { + case s @ Success(_) => s + case f @ Failure(exception) => + buildSuffix(t).fold(f) { suffix => + def append(existing: String): String = + if (existing.endsWith("\n")) s"$existing$suffix\n" + else s"$existing\n$suffix" + + Failure(Exceptions.rootCause(exception) match { + case fel: FailExceptionLike[_] => fel.updateMessage(append) + case e => + new FailException( + message = append(e.getMessage), + cause = e, + isStackTracesEnabled = false, + location = t.location + ) + }) + } + }(munitExecutionContext) + ) + } + ) } diff --git a/tests/shared/src/main/scala/munit/TestTransformFrameworkSuite.scala b/tests/shared/src/main/scala/munit/TestTransformFrameworkSuite.scala index 0cd7ac25..17767d75 100644 --- a/tests/shared/src/main/scala/munit/TestTransformFrameworkSuite.scala +++ b/tests/shared/src/main/scala/munit/TestTransformFrameworkSuite.scala @@ -2,14 +2,46 @@ package munit class TestTransformFrameworkSuite extends munit.FunSuite { override val munitTestTransforms: List[TestTransform] = List( - new TestTransform("ok", test => test.withName(test.name + "-ok")) + new TestTransform( + "ok", + test => + if (test.name == "hello") test.withName(test.name + "-ok") else test + ), + munitAppendToFailureMessage(t => + if (t.name.startsWith("suffix")) Some("==> extra info") else None + ) ) test("hello") {} + + test("suffix-success") {} + test("suffix-fail") { + fail("boom") + } + test("suffix-assertEquals") { + assertEquals(0, 1) + } } object TestTransformFrameworkSuite extends FrameworkTest( classOf[TestTransformFrameworkSuite], """|==> success munit.TestTransformFrameworkSuite.hello-ok + |==> success munit.TestTransformFrameworkSuite.suffix-success + |==> failure munit.TestTransformFrameworkSuite.suffix-fail - /scala/munit/TestTransformFrameworkSuite.scala:19 boom + |18: test("suffix-fail") { + |19: fail("boom") + |20: } + |==> extra info + |==> failure munit.TestTransformFrameworkSuite.suffix-assertEquals - /scala/munit/TestTransformFrameworkSuite.scala:22 + |21: test("suffix-assertEquals") { + |22: assertEquals(0, 1) + |23: } + |values are not the same + |=> Obtained + |0 + |=> Diff (- obtained, + expected) + |-0 + |+1 + |==> extra info |""".stripMargin )