我有一个SBT项目,其中包含一个根项目中聚合的多个子项目。我想确保测试套件是按顺序运行的,所以我已经设置了Global / concurrentRestrictions += Tags.limit(Tags.Test, 1)。
但是,我也在使用Tests.Setup()/Cleanup()来初始化每个子项目的测试套件的内存密集型资源。我面临的问题是,concurrentRestrictions对测试设置和清理没有影响:我所有的测试设置和清理逻辑都是并行运行的,而我希望它在每个测试套件之前和之后依次运行。
下面是一个示例build.sbt
import sbt._
import scala.sys.process._
lazy val testSettings = Seq(
testOptions ++= Seq(
Tests.Setup(() => {
println(s"setup for ${name.value}")
}),
Tests.Cleanup(() => {
println(s"cleanup for ${name.value}")
})
)
)
Global / libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.1.0" % "test"
)
def module(path: String) = {
Project(path, file(path))
.settings(testSettings)
}
lazy val module1 = module("module1")
lazy val module2 = module("module2")
lazy val module3 = module("module3")
lazy val root = (project in file("."))
.aggregate(module1, module2, module3)
Global / concurrentRestrictions += Tags.limit(Tags.Test, 1)以及我为每个子项目使用的虚拟测试:
import org.scalatest.matchers.should.Matchers
import org.scalatest._
class DummyTest extends FlatSpec with Matchers {
"this test" should "run a test for module 1" in {}
}运行sbt test提供了以下输出,模块化了变化莫测的并发性:
$ sbt test
[info] Loading settings for project global-plugins from plugins.sbt ...
[info] Loading global plugins from /home/jejost/.sbt/1.0/plugins
[info] Loading project definition from /home/jejost/dev/tmp/sbt-yunowork/project
[info] Loading settings for project root from build.sbt ...
[...]
setup for module1
setup for module3
setup for module2
[info] DummyTest:
[info] this test
[info] - should run a test for module 1
[info] DummyTest:
[info] this test
[info] - should run a test for module 3
[info] DummyTest:
[info] this test
[info] - should run a test for module 2
cleanup for module1
cleanup for module3
cleanup for module2
[...]当我想要的行为是:
$ sbt test
[info] Loading settings for project global-plugins from plugins.sbt ...
[info] Loading global plugins from /home/jejost/.sbt/1.0/plugins
[info] Loading project definition from /home/jejost/dev/tmp/sbt-yunowork/project
[info] Loading settings for project root from build.sbt ...
[...]
setup for module1
[info] DummyTest:
[info] this test
[info] - should run a test for module 1
cleanup for module1
setup for module3
[info] DummyTest:
[info] this test
[info] - should run a test for module 3
cleanup for module3
setup for module2
[info] DummyTest:
[info] this test
[info] - should run a test for module 2
cleanup for module2
[...]( module1、module3、module2的排序对我来说并不重要,只要安装/拆卸在每个子项目之前/之后都是连续的)
是否有可能确保这样的顺序测试设置和清理,理想情况下无需定义我自己的自定义任务?
我使用的是SBT 1.3.4。
编辑:-这已经被标记为以下问题的副本:How to run subprojects tests (including setup methods) sequentially when testing --这确实是同一个问题,但是,--接受的答案--不起作用,并且不会将任何内容更改为任务运行顺序!我试着复制另一个问题的设置,看看它是否是SBT中的回归,不幸的是OP的SBT版本太老了,无法在我的机器上成功地构建。
发布于 2021-03-12 00:00:25
与任务不同的是,默认情况下命令是按顺序执行的,因此请考虑将其降到命令级别。
addCommandAlias("sequentialtest", ";module1/test;module2/test;module3/test;")这给出了这样的结果
sbt:hello-world-scala> sequentialtest
setup for module1
[info] DummyTest:
[info] this test
[info] - should run a test for module 1
cleanup for module1
[info] Run completed in 164 milliseconds.
...
setup for module2
[info] DummyTest:
[info] this test
[info] - should run a test for module 2
cleanup for module2
[info] Run completed in 148 milliseconds.
...
setup for module3
[info] DummyTest:
[info] this test
[info] - should run a test for module 3
cleanup for module3
[info] Run completed in 154 milliseconds.
...回应评论
commands += Command.command("sequentialtest") { state =>
Project
.extract(state)
.structure
.allProjects
.iterator
.map(_.id)
.filterNot(_ == "root")
.map(id => s"$id/test;")
.mkString :: state
}https://stackoverflow.com/questions/62156158
复制相似问题