SBT 退出失败的业力测试



我有一个在Play框架上运行的Angular应用程序。我已经添加了我的 Karma/Jasmine 测试套件,并使用以下 build.sbt 配置将其作为"sbt 测试"的一部分运行......

// run the angular JS unit tests (karma & jasmine)
lazy val jsTest = taskKey[Int]("jsTest")
jsTest in Test := {
    "test/js/node_modules/karma/bin/karma start karma.conf.js" !
}
test := Def.taskDyn {
    val exitCode = (jsTest in Test).value
    if (exitCode == 0)
    Def.task {
        (test in Test).value
    }
    else Def.task()
}.value

但是,如果其中一个测试失败,sbt 似乎不会退出......

Chrome 50.0.2661 (Mac OS X 10.10.5): Executed 90 of 90 (1 FAILED) (0.512 secs / 0.453 secs)
[success] Total time: 3 s, completed 02-Jun-2016 12:11:13

运行 sbt 测试后,我也运行 sbt dist,如果任何测试失败,我不希望发生这种情况。我希望 sbt 在 JS 或 scala 测试失败时退出。

谢谢!

看起来你让 SBT test任务成功,即使 Karma 的退出代码不是0。 最简单的解决方法是在这种情况下抛出异常,SBT 会将其检测为任务失败:

  lazy val jsTest = taskKey[Int]("jsTest")
  jsTest in Test := {
    "test/js/node_modules/karma/bin/karma start karma.conf.js" !
  }
  test := Def.taskDyn {
    val exitCode = (jsTest in Test).value
    if (exitCode == 0)
      Def.task {
        (test in Test).value
      }
    else sys.error("Karma tests failed with exit code " + exitCode)
  }.value

但是现在你处于一个奇怪的情况,即使测试失败,jsTest任务在技术上仍然成功。 让jsTest任务检查错误代码会更合适,test任务依赖于它:

  lazy val jsTest = taskKey[Unit]("jsTest")
  jsTest in Test := {
    val exitCode = "test/js/node_modules/karma/bin/karma start karma.conf.js" !
    if (exitCode != 0) {
      sys.error("Karma tests failed with exit code " + exitCode)
    }
  }
  test := Def.taskDyn {
    (jsTest in Test).value
    Def.task((test in Test).value)
  }.value

如果你同意让JS测试和Scala测试并行运行,你可以进一步简化它:

  lazy val jsTest = taskKey[Unit]("jsTest")
  jsTest in Test := {
    val exitCode = "test/js/node_modules/karma/bin/karma start karma.conf.js" !
    if (exitCode != 0) {
      sys.error("Karma tests failed with exit code " + exitCode)
    }
  }
  test := {
    (jsTest in Test).value
    (test in Test).value
  }

最新更新