Skip to content

Instantly share code, notes, and snippets.

@mikla
Last active March 7, 2017 11:31
Show Gist options
  • Select an option

  • Save mikla/81ade88338a8058efdc7a57e17482427 to your computer and use it in GitHub Desktop.

Select an option

Save mikla/81ade88338a8058efdc7a57e17482427 to your computer and use it in GitHub Desktop.
Rerun whole scalatest suite on test fail.
import org.scalatest._
import org.scalatest.events._
import org.slf4j.Logger
trait SuiteRetries extends SuiteMixin {
this: Suite =>
def logger: Logger
private def runWithCustomReporter(testName: Option[String], args: Args) = {
val rep = new CustomReporter(args.reporter, logger)
super.run(testName, args.copy(reporter = rep))
}
abstract override def run(testName: Option[String], args: Args): Status = {
val status = runWithCustomReporter(testName, args)
if (status.succeeds())
status
else
super.run(testName, args)
}
}
class CustomReporter(val aggregateReporter: Reporter, logger: Logger) extends Reporter {
override def apply(event: Event): Unit = {
event match {
case e: TestFailed =>
logger.error("FAILED: " + e.suiteName + "/" + e.testName)
e.throwable.foreach(logger.error("", _))
aggregateReporter(TestPending(e.ordinal, e.suiteName, e.suiteId, e.suiteClassName, e.testName,
e.testText, e.recordedEvents, e.duration, e.formatter, e.location, e.payload, e.threadName, e.timeStamp))
case _ => aggregateReporter.apply(event)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment