在多个项目上执行一次scala sbt test run setup和cleanup命令 [英] scala sbt test run setup and cleanup command once on multi project

查看:56
本文介绍了在多个项目上执行一次scala sbt test run setup和cleanup命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以通过修改testOptions在sbt中为测试阶段添加设置和清理代码,例如:

  val embedMongoTestSettings:Seq [设置[_]] = Seq(Test + = Tests.Setup(()=> createMongod())中的testOptions,测试+ = Tests.Cleanup(()=> destroyMongod())中的testOptions) 

我遇到的问题是,这是在每个项目的基础上完成的,然后对每个项目都执行一次.因此,当我设置了一个多项目时,在这种情况下,我将启动多个数据库(这可以工作,但是意味着我必须针对每个项目端口进行配置,等等.)

sbt中是否有一种方法可以确保某些步骤仅在每个测试阶段都一次运行,无论是针对多个项目,一个项目还是单个测试用例?

我认为,唯一的办法是自己在设置和清理部分中管理并发性,因此请保留一个全局计数器,以检查它是第一个启动还是最后一个拆除.

解决方案

我在

您可以随意替换ThisBuild 中 startTest的实现和ThisBuild stopTest的实现.通过在 ThisBuild 级别上定义这些设置, sts 命令应在根级别以及各个子项目级别均起作用.

  root>sts正在启动服务器...[成功]总时间:1秒,已完成2015年1月13日下午5:20:58[info] HelloWorldSpec....[info]通过:总计3,失败0,错误0,通过3[成功]总时间:1秒,已完成2015年1月13日下午5:20:59正在停止伺服器...[成功]总时间:1秒,已完成2015年1月13日下午5:20:59根>项目应用[信息]将当前项目设置为应用app>sts正在启动服务器...[成功]总时间:1秒,已完成2015年1月13日下午5:21:15[info] HelloWorldSpec....[info]通过:总计3,失败0,错误0,通过3[成功]总时间:1秒,已完成2015年1月13日下午5:21:16正在停止伺服器...[成功]总时间:1秒,已完成2015年1月13日下午5:21:16 

I know I can add setup and cleanup code in sbt for the test phase by modifying the testOptions, e.g.:

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

The problem I have is that this done on a per project basis and then done once for every project. So when I have a multi project set up, I fire up several databases in this case (which would work, but means I have to configure per project ports, etc.).

Is there a way within sbt that makes sure that certain steps are only run once per any test phase, no matter if it is for multi projects, one project or a single test case?

Only way I figure is to manage the concurrency myself in the setup and cleanup parts, so keep a global counter that checks if it is the first one started or last one torn down.

解决方案

I wrote a blog post on sequencing tasks, which you might find it useful.

If you want to aggregate tests and make sure things are sequenced, one quick way of doing that is making a custom command. The following defines a command alias called sts:

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 substitute the implementation of startTest in ThisBuild and stopTest in ThisBuild as you like. By defining these settings at the ThisBuild level, sts command should work at both the root level as well as at individual subproject level.

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

这篇关于在多个项目上执行一次scala sbt test run setup和cleanup命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆