【问题标题】:ScalaTest: Running a test 50 timesScalaTest:运行测试 50 次
【发布时间】:2018-01-15 21:58:11
【问题描述】:

有没有一种简单的方法可以在 ScalaTest 中实现容错?我希望运行相同的测试 50 次并给它一个可容忍的误差范围,例如10%。

在上述情况下,只有 50 次测试中有 45 次成功,测试才会通过。

【问题讨论】:

    标签: scala scalatest


    【解决方案1】:

    做到这一点的最佳方法是覆盖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

    【讨论】:

      【解决方案2】:

      这是对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 次。

      【讨论】:

        【解决方案3】:

        Scala-check 可能是一个很好的解决方案。 http://www.scalacheck.org/

        【讨论】:

          猜你喜欢
          • 2014-05-24
          • 2013-03-23
          • 2017-04-13
          • 2014-07-26
          • 1970-01-01
          • 2017-09-25
          • 2019-04-29
          • 2017-05-22
          • 2019-12-18
          相关资源
          最近更新 更多