是否有一種簡單的方法可以在ScalaTest中實現容錯?我希望運行相同的測試50次,並給出一個可容忍的誤差範圍,例如10%。ScalaTest:運行測試50次
在上述情況下,只有50個測試中的45個測試成功,測試纔會通過。
是否有一種簡單的方法可以在ScalaTest中實現容錯?我希望運行相同的測試50次,並給出一個可容忍的誤差範圍,例如10%。ScalaTest:運行測試50次
在上述情況下,只有50個測試中的45個測試成功,測試纔會通過。
要做到這一點,最好的方法是重寫withFixture
並重新運行失敗的測試,使用任何算法在您的特定情況下都有意義。爲了獲得靈感,我建議你看看ScalaTest本身的Retries
特質。該Scaladoc是在這裏:
http://doc.scalatest.org/2.1.0/index.html#org.scalatest.Retries
爲Retries
實際的源代碼是在這裏:
https://github.com/scalatest/scalatest/blob/master/src/main/scala/org/scalatest/Retries.scala
斯卡拉檢查可能是一個很好的解決方案。 http://www.scalacheck.org/
這裏是一個除了Bill Venners提出的解決方案。我需要爲閃爍/不穩定測試執行一些重試。
val retries = 4
override def withFixture(test: NoArgTest) = {
if (isRetryable(test)) withFixture(test, retries) else super.withFixture(test)
}
def withFixture(test: NoArgTest, count: Int): Outcome = {
val outcome = super.withFixture(test)
outcome match {
case Failed(_) | Canceled(_) => if (count == 1) super.withFixture(test) else withFixture(test, count - 1)
case other => other
}
}
擴展測試類重試(with Retries
)中並用taggedAs Retryable
每個測試。需要時的這種測試將重試4次。