【问题标题】:How to run Test Suites sequentially in ScalaTest / SBT?如何在 ScalaTest / SBT 中按顺序运行测试套件?
【发布时间】:2019-06-09 18:24:25
【问题描述】:

如何在 ScalaTest / SBT 中按顺序运行测试套件?

例如,如果我有这个测试套件 A、B 和 C,我想确保 A 的测试首先运行,然后是 B 的测试,然后是 C 的测试。

我可以在 Scalatest 或 SBT 中进行哪些配置?

谢谢。

【问题讨论】:

    标签: scala sbt scalatest


    【解决方案1】:

    尝试在测试中使用 parallelExecution := false

    【讨论】:

    • 你好拉吉。谢谢您的回答。就我的理解而言,这个命令是按顺序运行同一个测试套件的测试。
    • 为了扩展@Farah 的响应,原发帖人希望在不同的测试套件中并行运行一些测试,但同一套件中的测试要按顺序运行。
    【解决方案2】:

    根据文档http://doc.scalatest.org/1.7/org/scalatest/Suite.html

    您需要创建自己的测试套件,如下所示:

    FirstTest.scala

    import org.scalatest.{DoNotDiscover, FunSuite}
    
    @DoNotDiscover
    class FirstTest extends FunSuite {
    
      test("first test"){
        assert(1 == 1)
      }
    
    }
    

    SecondTest.scala

    import org.scalatest.{DoNotDiscover, FunSuite}
    
    @DoNotDiscover
    class SecondTest extends FunSuite{
    
      test("second test"){
        assert(2 == 2)
      }
    }
    

    MainTest.scala

    import org.scalatest.Suites
    class MainTest extends Suites (new FirstTest,new SecondTest)
    

    现在,如果你运行 sbt test 它就可以正常工作了。

    注意:@DoNotDiscover 属性是强制性的。这避免了意外行为,例如在执行已执行两个测试套件的 MainSuite 之后执行 FirstTest 和 SecondTest。

    希望对你有帮助

    【讨论】:

    • 我认为这是最好的方法,并且与您使用的构建工具(sbt、gradle、...)无关。
    【解决方案3】:

    正如 raj mehra 所说,解决方案是配置为不并行运行测试。

    Test / parallelExecution := false

    以前的 parallelExecution in Test := false 已弃用。

    这里是解释它的文档:SBT Parallel Execution

    来自它:

    和以前一样,parallelExecution in Test 控制测试是否映射到单独的任务。

    【讨论】:

      猜你喜欢
      • 2014-04-28
      • 2015-10-06
      • 2014-09-23
      • 2014-07-26
      • 1970-01-01
      • 2021-07-08
      • 2014-06-14
      • 2015-07-10
      • 2020-02-22
      相关资源
      最近更新 更多