测试时如何按顺序运行子项目测试(包括设置方法)

问题描述:

EDIT4 水平线下方的大部分文本与实际问题没有任何关系.一开始我认为分叉是问题,但事实并非如此.

EDIT4 Most of the text below the horizontal rule doesn't have anything to do with the real problem. At the beginning I thought forking is the problem, but that is not true.

我正在尝试运行聚合子项目的所有测试.在子项目中的所有测试之前,应该运行设置方法,在子项目中的测试之后,应该运行清理方法.

I'm trying to run all the tests of an aggregates subprojects. Before all the tests in a subproject a setup method should run, and after the tests in a subproject a cleanup method should run.

在运行聚合项目的测试任务时,我期望以下顺序

When running the test task for the aggregate project, I expect the following sequence

  • 调用A项目的设置方法
  • 执行项目 A 的测试
  • 调用项目 A 的清理方法
  • 项目 B 相同

但顺序是:

  • 项目A和B的设置方法被调用
  • 执行项目 A 的测试
  • 执行项目 B 的测试
  • 调用项目A和B的清理方法

可以在此处找到具有此行为的构建脚本.

A build script having this behaviour can be found here.

如何解决这个问题以获得我预期的序列?

How can I fix this problem to get my expected sequence?

我正在使用 forking 在我的子项目中运行测试.对于每个子项目,在测试之前启动一个 mongo db 并停止测试后.

I'm using forking to run the tests in my subprojects. For every subproject a mongo db is started before the tests and stopped after the tests.

一个项目中的测试按顺序运行;如果我为单个项目运行测试,这很有效.

The tests within one project run sequentially; this works well if I run the tests for a single project.

但是如果我为项目根(包含子项目的聚合)运行任务 test,我希望分叉的 jvm 依次启动,即

But if I run the task test for project root (the aggregate containing the sub projects), I want the forked jvms to be started sequentially, i.e.

  1. 项目 A 的 jvm 被派生并执行其测试
  2. 项目 B 的 jvm 被派生并执行其测试
  3. ...

但是看起来jvm是并行启动的;这不是我想要的.

But it looks like the jvms are started in parallel; this is not what I want.

我尝试了以下操作(根据文档,它应该已经设置为 1):

I tried the following (which should be set to 1 already, according to the documentation):

  concurrentRestrictions in Test := Seq(
    Tags.limit(Tags.ForkedTestGroup, 1)
  )

但是没有用.直接启动测试任务后,如下从我的设置方法打印(在打印任何测试日志之前):

But it didn't work. Directly after starting the test task, the following is printed (before any test log is printed) from my setup method:

startupDb, thread name = pool-4-thread-5 
startupDb, thread name = pool-4-thread-7 
startupDb, thread name = pool-4-thread-2 
startupDb, thread name = pool-4-thread-6 
startupDb, thread name = pool-4-thread-8
startupDb, thread name = pool-4-thread-3 
startupDb, thread name = pool-4-thread-9

这些是我的测试相关设置:

These are my test related settings:

parallelExecution in Test := false,
testOptions in Test += Tests.Setup( () => MongoTest.startupDb() ),
testOptions in Test += Tests.Cleanup( () => MongoTest.shutdownDb() ),
fork in Test := true,
concurrentRestrictions in Test := Seq(
  Tags.limit(Tags.ForkedTestGroup, 1)
)

使用分叉对我来说很重要,但方式如上所述.

It's important for me to use forking, but in the way as described above.

我在 Windows 7 上使用 sbt 0.13.0.

I'm using sbt 0.13.0 on Windows 7.

编辑我用示例构建创建了一个要点.

EDIT2文档它说:

通过设置Tags.ForkedTestGroup标签的限制来控制允许同时运行的分叉JVM的数量,默认为1

Control the number of forked JVMs allowed to run at the same time by setting the limit on Tags.ForkedTestGroup tag, which is 1 by default

所以这在理论上应该可行.这是一个错误吗?如果没有,我怎样才能做到这一点?

So this should work theoretically. Is this a bug? If not, how can I manage to do that?

EDIT3 看起来分叉不是问题.问题是所有子项目都会立即调用设置方法.

EDIT3 It looks like forking is not the problem. The problem is that the setup methods are called immediately for all subprojects.

嗯.琐碎的答案转换为评论.听起来您问的问题与另一个问题中已解决的问题相同(请参阅评论).答案似乎是

Hmm. Trivial answer convert to comment. It sounds like you are asking the same question that got solved in the other question (see comments). The answer seems to be

parallelExecution in ThisBuild := false

我发现了另一篇建议的博文

and I found another blog post that suggested

parallelExecution in Global := false

成为答案.全局"答案还建议关闭并行执行之类的事情,但这可能不如运行测试重要.

to be the answer. The "Global" answer also suggested that would turn off parallel execution for things like compilation but that's probably less important than running tests.