scala 如何按顺序运行规范
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15145987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run specifications sequentially
提问by Jeriho
I want to create few specifications that interoperate with database.
我想创建一些与数据库互操作的规范。
class DocumentSpec extends mutable.Specification with BeforeAfterExample {
sequential
def before() = {createDB()}
def after() = {dropDB()}
// examples
// ...
}
Database is created and dropped before and after every example (which is executed sequentially). Everithing works as expected until there is only one spec that works with database. Because specifications are executed parallel, they interfere and fail.
在每个示例之前和之后创建和删除数据库(按顺序执行)。Everithing 按预期工作,直到只有一个规范适用于数据库。因为规范是并行执行的,所以它们会干扰并失败。
I hope that I'm able to avoid this by instructing specs2 to run tests with side effects sequentially while keeping side effect free tests to run in parallel. Is it possible?
我希望我能够通过指示 specs2 依次运行带有副作用的测试,同时保持无副作用的测试并行运行来避免这种情况。是否可以?
回答by Régis Jean-Gilles
I suppose you are using SBT? If so, check the documentation: http://www.scala-sbt.org/release/docs/Detailed-Topics/Parallel-Execution
我想你正在使用 SBT?如果是这样,请查看文档:http: //www.scala-sbt.org/release/docs/Detailed-Topics/Parallel-Execution
The relevant SBT setting is parallelExecution. Add this to your project definition:
相关的 SBT 设置是parallelExecution. 将此添加到您的项目定义中:
parallelExecution in Test := false
回答by Ivan Mushketyk
If you want to run single Specification in specs2 sequentially just add sequentialmethod call at the beginning of your Specification. For example:
如果您想在 specs2 中按顺序运行单个规范,只需sequential在规范的开头添加方法调用。例如:
class MyTest extends Specification {
// Set sequential execution
sequential
// This tests will be executed sequentially
"my test" should {
"add numbers" in {
(1 + 1) should be equalTo 2
}
"multiply numbers" in {
(2 * 2) should be equalTo 4
}
}
}
UPDATE:As @jsears correctly mentioned in the comments, this will make tests to run sequentially in a singlespec! Other specs may still run in parallel.
更新:正如@jsears 在评论中正确提到的那样,这将使测试在单个规范中按顺序运行!其他规范可能仍会并行运行。
回答by pme
Meanwhile there is a better solution (http://www.scala-sbt.org/release/docs/Parallel-Execution.html):
同时有一个更好的解决方案(http://www.scala-sbt.org/release/docs/Parallel-Execution.html):
sbt 0.12.0 introduces a general infrastructure for restricting task concurrency beyond the usual ordering declarations.
sbt 0.12.0 引入了一个通用基础结构,用于限制超出通常排序声明的任务并发。
This configuration will run all tests sequential, also if they are in subprojects:
此配置将按顺序运行所有测试,如果它们在子项目中也是如此:
concurrentRestrictions in Global := Seq(
Tags.limit(Tags.CPU, 2),
Tags.limit(Tags.Network, 10),
Tags.limit(Tags.Test, 1),
Tags.limitAll( 15 )
)
I haven't tested if this can be overridden by each sub-project, so the sub-project can run its tests in parallel.
我还没有测试这是否可以被每个子项目覆盖,所以子项目可以并行运行它的测试。

