【问题标题】:Running setup/teardown tasks during stb test task在 stb 测试任务期间运行设置/拆卸任务
【发布时间】:2017-03-28 19:10:53
【问题描述】:

我尝试将 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
    }
  }
}

当我为项目 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

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

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

【问题讨论】:

  • 为什么 FooSpec 是一个对象而不是一个类?
  • 我只是从一个示例中复制它(由于某种原因它是对象)以运行示例代码。

标签: scala unit-testing sbt


【解决方案1】:

换成这样怎么样?

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

【讨论】:

  • 另外,我认为这里的讨论解释了为什么它不能像您使用任务时所期望的那样工作:scala-sbt.org/0.13/docs/…
  • 感谢您的链接。它至少部分解释了这个问题(“在任务上调用 value 方法不会被严格评估”)。
猜你喜欢
  • 1970-01-01
  • 2011-05-06
  • 2011-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多