Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add failSuite() helper to abort the entire test suite #59

Merged
merged 2 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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