sbt - 为顺序测试定义自定义配置

时间:2014-04-09 19:28:19

标签: scala sbt

我的一些测试要求它们按顺序执行。我在http://www.scala-sbt.org/0.13.0/docs/Detailed-Topics/Testing.html上阅读了有关自定义配置的信息,但我遗漏了一些内容,因为我的配置无法正常运行。

这是:

import sbt._
import Keys._

object SchedulingBackendBuild extends Build {
  lazy val SequentialTest = config("sequentialTest") extend(Test)
  def sequentialTestFilter(name: String): Boolean = {
    println("===seq test filter")
    name endsWith "SeqSpec"
  }
  def unitTestFilter(name: String): Boolean = {
    println("===unit test filter")
    !sequentialTestFilter(name)
  }

  lazy val root = Project(id = "scheduling-backend",
    base = file("."),
    settings = Project.defaultSettings
  ).configs(SequentialTest)
  .settings(inConfig(SequentialTest)(Defaults.testTasks): _*)
  .settings(
      testOptions in Test ++= Seq(Tests.Filter(unitTestFilter)),
      testOptions in SequentialTest ++= Seq(Tests.Filter(sequentialTestFilter))
  )
}

我希望test只执行不以SeqSpec结尾的测试,这是有效的,但是当我尝试执行sequentialTest:test时,不会执行任何测试。我在我的过滤器中添加了println,即使执行sequentialTest:test,我也能看到

===unit test filter
===seq test filter
===seq test filter

所以两个过滤器都被执行了。

当我输入inspect sequentialTest:testOptions时,我正在

[info] Task: scala.collection.Seq[sbt.TestOption]
[info] Description:
[info]  Options for running tests.
[info] Provided by:
[info]  {file:/path/to/project/scheduling-backend/}scheduling-backend/sequentialTest:testOptions
[info] Defined at:
[info]  /path/to/project/scheduling-backend/project/Build.scala:22
[info] Reverse dependencies:
[info]  sequentialTest:testOnly::testOptions
[info]  sequentialTest:testQuick::testOptions
[info]  sequentialTest:test::testOptions
[info] Delegates:
[info]  sequentialTest:testOptions
[info]  test:testOptions
[info]  runtime:testOptions
[info]  compile:testOptions
[info]  *:testOptions
[info]  {.}/sequentialTest:testOptions
[info]  {.}/test:testOptions
[info]  {.}/runtime:testOptions
[info]  {.}/compile:testOptions
[info]  {.}/*:testOptions
[info]  */sequentialTest:testOptions
[info]  */test:testOptions
[info]  */runtime:testOptions
[info]  */compile:testOptions
[info]  */*:testOptions
[info] Related:
[info]  sequentialTest:testOnly::testOptions
[info]  sequentialTest:testQuick::testOptions
[info]  test:testOptions
[info]  test:testQuick::testOptions
[info]  */*:testOptions
[info]  test:testOnly::testOptions
[info]  test:test::testOptions
[info]  sequentialTest:test::testOptions

所以对我来说看起来没问题,第22行是testOptions in SequentialTest ++= Seq(Tests.Filter(sequentialTestFilter))

1 个答案:

答案 0 :(得分:0)

部分答案:如果我将++=运算符(附加到序列)更改为:=(替换序列),一切正常(文档中使用:=运算符)。即使这样,我也想知道为什么++=在这种情况下是不好的选择。