如何为集成测试设置分叉JVM的数量?

时间:2015-12-20 14:14:33

标签: scala sbt

以下内容将我的所有集成测试设置为在单独的分叉JVM中运行,这些JVM扩展为How to fork the jvm for each test in sbt

fork in IntegrationTest := true,
testForkedParallel in IntegrationTest := true,
testGrouping in IntegrationTest <<= (
  definedTests in IntegrationTest,
  baseDirectory in IntegrationTest,
  javaOptions in IntegrationTest,
  outputStrategy in IntegrationTest,
  envVars in IntegrationTest,
  javaHome in IntegrationTest,
  connectInput in IntegrationTest
).map { (tests, base, options, strategy, env, javaHomeDir, connectIn) =>
  val opts = ForkOptions(
    bootJars = Nil,
    javaHome = javaHomeDir,
    connectInput = connectIn,
    outputStrategy = strategy,
    runJVMOptions = options,
    workingDirectory = Some(base),
    envVars = env
  )
  tests.map { test =>
    Tests.Group(test.name, Seq(test), Tests.SubProcess(opts))
  }
}

但它一次只能运行一个。这并不奇怪,因为根据http://www.scala-sbt.org/0.13/docs/Testing.html Tags.ForkedTestGroup默认为1。

但是,我尝试自定义Tags.ForkedTestGroup - 一次启用多个JVM - 将被忽略。这是我尝试过的(每个人都独立):

concurrentRestrictions in Global -= Tags.limit(Tags.ForkedTestGroup, 1)

concurrentRestrictions in Global += Tags.limit(Tags.ForkedTestGroup, 4)

concurrentRestrictions in Global := Seq(Tags.limitAll(4))

那么如何获得多个分叉JVM来运行测试呢?

1 个答案:

答案 0 :(得分:1)

问题是SBT错误,标记了错误的任务。标记的并不是运行分叉JVM的任务。

解决方法是

inConfig(Test)(Seq(
  tags in test += Tags.ForkedTestGroup -> 1,
  tags in testOnly += Tags.ForkedTestGroup -> 1,
  tags in testQuick += Tags.ForkedTestGroup -> 1
))

然后您的并发限制将按预期工作。

concurrentRestrictions in Global += Tags.limit(Tags.ForkedTestGroup, 4)