Skip to content

Instantly share code, notes, and snippets.

@warmuuh
Created April 24, 2018 12:00
Show Gist options
  • Save warmuuh/bb8819c43dfc28fd6b1290520fa7bd3a to your computer and use it in GitHub Desktop.
Save warmuuh/bb8819c43dfc28fd6b1290520fa7bd3a to your computer and use it in GitHub Desktop.
SureFireStyleReporter for scalatest
package wrm.scalatest
import org.scalatest.Reporter
import org.scalatest.events._
class SureFireStyleReporter extends Reporter{
var failedTests: List[Event] = Nil
var testRun = 0
var testPending = 0
var testFailed = 0
var testCancelled = 0
var testIgnored = 0
def printSummary(): Unit = {
if (failedTests.nonEmpty) {
println("Failed Tests: ")
failedTests foreach {
case e: TestFailed =>
println(getTestDescription(e) + " failed")
println("\t"+e.message)
case e: TestCanceled =>
println(getTestDescription(e)+ " cancelled")
println("\t"+e.message)
case e: SuiteAborted =>
println(getTestDescription(e) + " aborted")
println("\t"+e.message)
case _ =>
}
println(s"\n\nTests run: $testRun, Failures: $testFailed, Cancelled: $testFailed, Ignored: $testIgnored, Pending: $testPending")
}
}
def getTestDescription(e: Event): String = e match {
case e: TestFailed => s"${e.suiteName} ${e.testName} (${e.location.getOrElse("")})"
case e: TestCanceled => s"${e.suiteName} ${e.testName} (${e.location.getOrElse("")})"
case e: SuiteAborted => s"${e.suiteName} (${e.location.getOrElse("")})"
case _ => throw new IllegalArgumentException(s"Unexpected event type: ${e.getClass()}")
}
override def apply(event: Event): Unit = {
event match {
case e: TestStarting =>
testRun += 1
case e: TestFailed =>
failedTests = e :: failedTests
testFailed += 1
case e: TestCanceled =>
failedTests = e :: failedTests
testCancelled += 1
case e: TestPending =>
testPending += 1
case e: TestIgnored =>
testIgnored += 1
case e: SuiteAborted =>
failedTests = e :: failedTests
case _: RunCompleted =>
printSummary()
case _ =>
//skip
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment