2017-03-26 37 views
0

我尝试配置sbt以在开始时运行安装任务,并在myProject/test命令结束时执行拆卸任务。在stb测试任务期间运行setup/teardown任务

我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 
    } 
    } 
} 

当我运行项目的子测试,我得到以下的输出:

> 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 

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

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

+0

为什么FooSpec是一个对象而不是类的任何原因? – Chrs

+0

我刚刚从示例中(它出于某种原因是对象)复制它以使示例代码正在运行。 – user4955663

回答

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() } 
+0

另外,我认为这里的讨论解释了为什么它不像您使用任务时期望的那样工作:http://www.scala-sbt.org/0.13/docs/Custom-Settings.html#Execution+ +任务的语义+ – Chrs

+0

感谢您的链接。它至少部分解释了这个问题(“调用任务的值方法不会被严格评估”)。 – user4955663