Scala command to run and clean sbt test run once in several projects

I know that I can add the setup and cleanup code in sbt for the testing phase by changing testOptions, for example:

val embedMongoTestSettings: Seq[Setting[_]] = Seq( testOptions in Test += Tests.Setup( () => createMongod()), testOptions in Test += Tests.Cleanup( () => destroyMongod()) ) 

The problem is that this is done for each project and then executed once for each project. Therefore, when I have a multiproject installed, I run several databases in this case (this will work, but I have to configure each port of the project, etc.).

Is there a way in sbt that ensures that certain steps are performed only once at any stage of testing, regardless of whether it is used for several projects, one project or one test case?

The only way I think is to manage the concurrency itself in the setup and cleanup parts, so keep a global counter that checks to see if it was first started or the last was torn down.

+5
source share
1 answer

I wrote a blog post about sequencing tasks that you might find useful.

If you want to combine tests and make sure everything is streamlined, one quick way to do this is to create a custom team. The following is an alias for the sts command:

 lazy val commonSettings = Seq( scalaVersion := "2.11.4" ) lazy val specs2Core = "org.specs2" %% "specs2-core" % "2.4.15" val startTest = taskKey[Unit]("start test") val stopTest = taskKey[Unit]("stop test") lazy val root = (project in file(".")). aggregate(app, webapp). settings(commonSettings: _*). settings(addCommandAlias("sts", ";startTest;test;stopTest"): _*). settings( startTest in ThisBuild := { println("starting server...") Thread.sleep(500) }, stopTest in ThisBuild := { println("stopping server...") Thread.sleep(500) } ) lazy val app = (project in file("app")). settings(commonSettings: _*). settings( libraryDependencies += specs2Core % Test ) lazy val webapp = (project in file("webapp")). settings(commonSettings: _*). settings( libraryDependencies += specs2Core % Test ) 

You can replace the startTest in ThisBuild and stopTest in ThisBuild as you wish. Defining these parameters at the ThisBuild level, the sts command should work both at the root level and at the level of individual subprojects.

 root> sts starting server... [success] Total time: 1 s, completed Jan 13, 2015 5:20:58 PM [info] HelloWorldSpec .... [info] Passed: Total 3, Failed 0, Errors 0, Passed 3 [success] Total time: 1 s, completed Jan 13, 2015 5:20:59 PM stopping server... [success] Total time: 1 s, completed Jan 13, 2015 5:20:59 PM root> project app [info] Set current project to app app> sts starting server... [success] Total time: 1 s, completed Jan 13, 2015 5:21:15 PM [info] HelloWorldSpec .... [info] Passed: Total 3, Failed 0, Errors 0, Passed 3 [success] Total time: 1 s, completed Jan 13, 2015 5:21:16 PM stopping server... [success] Total time: 1 s, completed Jan 13, 2015 5:21:16 PM 
+1
source

Source: https://habr.com/ru/post/1208530/


All Articles