在stb测试任务期间运行设置/拆卸任务

时间:2017-03-26 16:17:34

标签: scala unit-testing sbt

我尝试配置sbt以在开始时运行设置任务,并在myProject / test命令结束时执行拆除任务。

我的build.sbt在这里:

name := "ch-2"

version := "1.0"

libraryDependencies += "org.specs2" % "specs2_2.10" % "1.14" % "test"

lazy val common = (
  Project("common", file("common")).
    settings()
  )

lazy val subPro = (
  Project("sub", file("subA")).settings(
  ).dependsOn(common)
    settings(libraryDependencies += "org.specs2" % "specs2_2.10" % "1.14" % "test" )
  )

val startS = taskKey[Unit]("Start")
val stopS = taskKey[Unit]("Stop")
startS := { println("Running start")}
stopS := { println("Running stop")}

testOptions in Test in subPro += Tests.Setup { () => startS.value }
testOptions in Test in subPro += Tests.Cleanup { () => stopS.value }

实际的虚拟测试类在这里:

import org.specs2.mutable.Specification
/**
  * Created by jk on 26.3.2017.
  */
object FooSpec extends Specification {
  "The TEST method" should {
    "blaa blaa 1" in {
      println("test 1 running...")
      true
    }
    "blaa blaa 2" in {
      println("test 2 running...")
      true
    }
  }
}

当我运行project sub的测试时,我得到以下输出:

> sub/test
Running stop
Running start
[info] Updating {file:/home/jk/workspace/sbt-in-action/ch2/}sub...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Compiling 1 Scala source to /home/jk/workspace/sbt-in-action/ch2/subA/target/scala-2.10/test-classes...
test 1 running...
test 2 running...
[info] FooSpec
[info] 
[info] The TEST method should
[info] + blaa blaa 1
[info] + blaa blaa 2
[info]  
[info]  
[info] Total for specification FooSpec
[info] Finished in 18 ms
[info] 2 examples, 0 failure, 0 error
[info] 
[info] Passed: Total 2, Failed 0, Errors 0, Passed 2
[success] Total time: 3 s, completed Mar 26, 2017 7:09:34 PM

为什么在编译完成之前就停止任务运行以及如何修复它以便在运行所有测试用例后运行它(尽管是测试用例的结果)?

此外,启动任务应在成功编译后但在第一个测试用例之前运行。如何解决这些问题?

1 个答案:

答案 0 :(得分:0)

如何做到这样呢?

def startS(): Unit = { println("Running start")}
def stopS(): Unit = { println("Running stop")}

testOptions in Test in subPro += Tests.Setup { () => startS() }
testOptions in Test in subPro += Tests.Cleanup { () => stopS() }