首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sbt中的顺序测试设置/清理

问sbt中的顺序测试设置/清理
EN

Stack Overflow用户
提问于 2020-06-02 15:58:31
回答 1查看 315关注 0票数 2

我有一个SBT项目,其中包含一个根项目中聚合的多个子项目。我想确保测试套件是按顺序运行的,所以我已经设置了Global / concurrentRestrictions += Tags.limit(Tags.Test, 1)。

但是,我也在使用Tests.Setup()/Cleanup()来初始化每个子项目的测试套件的内存密集型资源。我面临的问题是,concurrentRestrictions对测试设置和清理没有影响:我所有的测试设置和清理逻辑都是并行运行的,而我希望它在每个测试套件之前和之后依次运行。

下面是一个示例build.sbt

代码语言:javascript
复制
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)

以及我为每个子项目使用的虚拟测试:

代码语言:javascript
复制
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提供了以下输出,模块化了变化莫测的并发性:

代码语言:javascript
复制
$ 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
[...]

当我想要的行为是:

代码语言:javascript
复制
$ 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版本太老了,无法在我的机器上成功地构建。

EN

回答 1

Stack Overflow用户

发布于 2021-03-12 00:00:25

与任务不同的是,默认情况下命令是按顺序执行的,因此请考虑将其降到命令级别。

代码语言:javascript
复制
addCommandAlias("sequentialtest", ";module1/test;module2/test;module3/test;")

这给出了这样的结果

代码语言:javascript
复制
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.
...

回应评论

代码语言:javascript
复制
commands += Command.command("sequentialtest") { state =>
  Project
   .extract(state)
   .structure
   .allProjects
   .iterator
   .map(_.id)
   .filterNot(_ == "root")
   .map(id => s"$id/test;")
   .mkString :: state
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62156158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档