您如何专门针对集成测试施加scala代码覆盖率?

时间:2018-09-10 23:15:12

标签: scala sbt code-coverage scoverage

正在使用以下sbt命令运行集成测试

  

sbt干净覆盖它:测试coverage报告

此命令运行集成测试,对其进行检测并生成报告。

Build.sbt具有以下内容:

coverageMinimum in IntegrationTest := 21.0
coverageFailOnMinimum in IntegrationTest := true

输出如下:

[info] Statement coverage.: 20.16%
[info] Branch coverage....: 12.00%
[info] Coverage reports completed
[info] All done. Coverage was [20.16%]

输出结果的代码覆盖率为20.16%,但是build.sbt中的限制没有强制执行该限制。

如果我将build.sbt更改为遵循它,那么它将起作用:

coverageMinimum := 21.0
coverageFailOnMinimum := true

想知道我为集成测试指定限制时缺少了什么

版本信息:

sbt:0.13.17

sbt-coverage:1.5.1

1 个答案:

答案 0 :(得分:4)

以下两种变通方法似乎可以在我的机器上运行(sbt-scoverage 1.5.1,sbt 1.1.1,scala 2.12.5)

解决方法1-使用inConfig来限制配置范围:

inConfig(IntegrationTest)(ScoverageSbtPlugin.projectSettings),
inConfig(IntegrationTest)(Seq(coverageMinimum := 21, coverageFailOnMinimum := true))

现在执行sbt clean coverage it:test it:coverageReport会抛出Coverage minimum was not reached

解决方法2-在自定义command 中修改coverageMinimum设置:

def itTestWithMinCoverage = Command.command("itTestWithMinCoverage") { state =>
  val extracted = Project extract state
  val stateWithCoverage = extracted.append(Seq(coverageEnabled := true, coverageMinimum := 21.0, coverageFailOnMinimum := true), state)
  val (s1, _) = Project.extract(stateWithCoverage).runTask(test in IntegrationTest, stateWithCoverage)
  val (s2, _) = Project.extract(s1).runTask(coverageReport in IntegrationTest, s1)
  s2
}

commands ++= Seq(itTestWithMinCoverage)

现在执行sbt itTestWithMinCoverage会引发Coverage minimum was not reached。请注意,执行itTestWithMinCoverage之后,state被丢弃,因此coverageMinimum应该恢复为默认值,因此不会影响单元测试。

问题似乎是(除了我对范围是如何工作的了解不足之外),checkCoverage甚至在设置coverageMinimum in IntegrationTest之后仍选择了coverageMinimum的默认值。