mardi 2 juin 2020

Sequential test setup/cleanup in sbt

I have an SBT project with multiple subprojects aggregated within a root project. I want to make sure that test suites are ran sequentially, so I have set Global / concurrentRestrictions += Tags.limit(Tags.Test, 1).

However, I am also using Tests.Setup()/Cleanup() to initialise memory-heavy resources for each subproject's test suite. The problem I'm facing is that concurrentRestrictions has no effect on test setup and cleanup: all of my test setup and cleanup logic is being ran in parallel, when I would like it to run sequentially before and after each test suite.

Here is a sample 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)

Running sbt test gives the following output, modulo the vagaries of concurrency:

$ 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 module2
setup for module3
cleanup for module1
cleanup for module2
cleanup for module3
[...]

when the behaviour I want is this:

$ 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
// tests for module 1 are ran here
cleanup for module1
setup for module2
// tests for module 2 are ran here
cleanup for module2
setup for module3
// tests for module 3 are ran here
cleanup for module3
[...]

Is it possible to ensure sequential test setup and cleanup like this, ideally without defining my own custom task?

Aucun commentaire:

Enregistrer un commentaire