Created
February 28, 2020 01:40
-
-
Save jcallin/e4aad523175fab960dbe690b75d401c8 to your computer and use it in GitHub Desktop.
ScalaTest Retry a test over and over
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.scalatest.tagobjects.Retryable | |
import org.scalatest.{ Canceled, Failed, Outcome, Retries, Succeeded } | |
class SomeTestSuite extends FunSpec with Retries { | |
val retries = 1000 | |
override def withFixture(test: NoArgTest): Outcome = { | |
if (isRetryable(test)) withFixture(test, retries) else super.withFixture(test) | |
} | |
// Run a test 1000 times, error on any failure | |
// Swap the second case code up to the first one and pass through the second case result to retry on failure | |
def withFixture(test: NoArgTest, count: Int): Outcome = { | |
val outcome = super.withFixture(test) | |
outcome match { | |
case Failed(_) | Canceled(_) => throw new Exception(s"failed at $count try") | |
case _ => if (count == 1) super.withFixture(test) else withFixture(test, count - 1) | |
} | |
} | |
it("should run this test a number of times", Retryable) { | |
1 shouldBe 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment