【问题标题】:scalatest Flatspec: Timeout for entire classscalatest Flatspec:整个班级的超时
【发布时间】:2017-11-29 18:59:36
【问题描述】:

使用 scalatest Flatspec 和 TimeLimits 特征,我可以为一行代码设置超时,如下所示:

import org.scalatest.time.SpanSugar._
import org.scalatest.concurrent.TimeLimits
import org.scalatest.FlatSpec

class MyTestClass extends Flatspec with TimeLimits {
  "My thread" must "complete on time" in { 
    failAfter(100 millis) { infiniteLoop() }
    // I have also tried cancelAfter
  }
}

这应该由于超时而失败。但是,当我在 Intellij 中运行此测试时,它会永远运行。

此外,我不想为每个方法重写超时,而是想为整个类配置一次。 PatienceConfig 声称可以这样做,但它似乎没有做任何事情。测试仍然会永远运行。

import org.scalatest.FlatSpec
import org.scalatest.time.{Millis, Span}
import org.scalatest.concurrent.{Eventually, IntegrationPatience}

class MyTestClass extends Flatspec with Eventually with IntegrationPatience {
 implicit val defaultPatience = PatienceConfig(timeout=Span(100, Millis))

 "My thread" must "complete on time" in { 
   inifiniteLoop()
 }
}

【问题讨论】:

    标签: scala testing scalatest


    【解决方案1】:

    我自己寻找解决方案。来了一个交叉this 答案,它对我有用。 我正在使用 flatspec,添加了特征 TimeLimitedTests

    with TimeLimitedTests
    

    然后在代码中我为每个测试写了我的限制val timeLimit: Span = Span(2000, Millis) 这是由 trait 定义的(我们正在覆盖它)。

    最后它没有工作,直到我按照 Rumoku 在引用的答案中的建议覆盖了中断器

    override val defaultTestInterruptor: Interruptor = new Interruptor {
    override def apply(testThread: Thread): Unit = {
      println("Kindly die")
      testThread.stop() // deprecated. unsafe. do not use
    }}
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 2015-08-29
      • 2012-05-28
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 2016-06-06
      相关资源
      最近更新 更多