Skip to content

Commit

Permalink
Merge pull request #59 from daron666/add-fail-suite-helper
Browse files Browse the repository at this point in the history
 Add failSuite() helper to abort the entire test suite
  • Loading branch information
olafurpg authored Feb 17, 2020
2 parents 1301010 + cd02012 commit cb87436
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
10 changes: 10 additions & 0 deletions munit/shared/src/main/scala/munit/Assertions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ trait Assertions extends MacroCompat.CompileErrorMacro {
)
}

def failSuite(
message: String,
clues: Clues = new Clues(Nil)
)(implicit loc: Location): Nothing = {
throw new FailSuiteException(
munitFilterAnsi(munitLines.formatLine(loc, message, clues)),
loc
)
}

private val munitCapturedClues: mutable.ListBuffer[Clue[_]] =
mutable.ListBuffer.empty
def munitCaptureClues[T](thunk: => T): (T, Clues) =
Expand Down
6 changes: 6 additions & 0 deletions munit/shared/src/main/scala/munit/FailSuiteException.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package munit

class FailSuiteException(
override val message: String,
override val location: Location
) extends FailException(message, location)
14 changes: 14 additions & 0 deletions munit/shared/src/main/scala/munit/MUnitRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MUnitRunner(val cls: Class[_ <: Suite], newInstance: () => Suite)
private val suiteDescription = Description.createSuiteDescription(cls)
private implicit val ec: ExecutionContext = suite.munitExecutionContext
@volatile private var filter: Filter = Filter.ALL
@volatile private var suiteAborted: Boolean = false
val descriptions: mutable.Map[suite.Test, Description] =
mutable.Map.empty[suite.Test, Description]
val testNames: mutable.Set[String] = mutable.Set.empty[String]
Expand Down Expand Up @@ -200,6 +201,16 @@ class MUnitRunner(val cls: Class[_ <: Suite], newInstance: () => Suite)
if (!filter.shouldRun(description)) {
return Future.successful(false)
}
if (suiteAborted) {
notifier.fireTestAssumptionFailed(
new Failure(
description,
new FailSuiteException("Suite has been aborted", test.location)
)
)
return Future.successful(false)
}

notifier.fireTestStarted(description)
val onError: PartialFunction[Throwable, Future[Unit]] = {
case ex: AssumptionViolatedException =>
Expand All @@ -211,6 +222,9 @@ class MUnitRunner(val cls: Class[_ <: Suite], newInstance: () => Suite)
ex match {
case _: AssumptionViolatedException =>
notifier.fireTestAssumptionFailed(failure)
case _: FailSuiteException =>
suiteAborted = true
notifier.fireTestFailure(failure)
case _ =>
notifier.fireTestFailure(failure)
}
Expand Down
25 changes: 25 additions & 0 deletions tests/shared/src/main/scala/munit/FailSuiteFrameworkSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package munit

class FailSuiteFrameworkSuite extends FunSuite {
test("pass") {
// println("pass")
}
test("fail") {
failSuite("Oops, can not do anything.")
}
test(name = "not gonna run") {
// println("not pass")
}
}

object FailSuiteFrameworkSuite
extends FrameworkTest(
classOf[FailSuiteFrameworkSuite],
"""|==> success munit.FailSuiteFrameworkSuite.pass
|==> failure munit.FailSuiteFrameworkSuite.fail - /scala/munit/FailSuiteFrameworkSuite.scala:8 Oops, can not do anything.
|7: test("fail") {
|8: failSuite("Oops, can not do anything.")
|9: }
|==> skipped munit.FailSuiteFrameworkSuite.not gonna run - Suite has been aborted
|""".stripMargin
)
1 change: 1 addition & 0 deletions tests/shared/src/test/scala/munit/FrameworkSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class FrameworkSuite extends BaseFrameworkSuite {
CiOnlyFrameworkSuite,
DiffProductFrameworkSuite,
FailFrameworkSuite,
FailSuiteFrameworkSuite,
TestNameFrameworkSuite,
ScalaVersionFrameworkSuite,
FixtureFrameworkSuite,
Expand Down

0 comments on commit cb87436

Please sign in to comment.