【问题标题】:SBT exit on Failed Karma Tests因业力测试失败而退出 SBT
【发布时间】:2016-10-02 02:29:26
【问题描述】:

我有一个在 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 test 后,我​​还运行 sbt dist,如果任何测试失败,我不希望发生这种情况。如果 JS 或 scala 测试失败,我希望 sbt 退出。

谢谢!

【问题讨论】:

    标签: unit-testing sbt karma-jasmine


    【解决方案1】:

    看起来您正在让 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
      }
    

    【讨论】:

    • "test/js/node_modules/karma/bin/karma start karma.conf.js" !必须在括号中
    猜你喜欢
    • 1970-01-01
    • 2012-06-10
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 2014-07-29
    相关资源
    最近更新 更多