How to run specifications sequentially

天大地大妈咪最大 提交于 2019-12-04 16:23:48

问题


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.

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?


回答1:


I suppose you are using SBT? If so, check the documentation: http://www.scala-sbt.org/release/docs/Detailed-Topics/Parallel-Execution

The relevant SBT setting is parallelExecution. Add this to your project definition:

parallelExecution in Test := false



回答2:


If you want to run single Specification in specs2 sequentially just add sequential method call at the beginning of your Specification. For example:

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 single spec! Other specs may still run in parallel.




回答3:


Meanwhile there is a better solution (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.

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.



来源:https://stackoverflow.com/questions/15145987/how-to-run-specifications-sequentially

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!