覆盖率:合并测试范围和测试范围: [英] scoverage: Combine Coverage from test and it:test

查看:153
本文介绍了覆盖率:合并测试范围和测试范围:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用过滤器拆分了单元测试和集成测试:

I splitted my Unit- and Integration-Tests with a Filter:

  lazy val FunTest = config("it") extend Test

  def funTestFilter(name: String): Boolean = name endsWith "Spec"

  def unitTestFilter(name: String): Boolean = name endsWith "Test"

  ...
  testOptions in Test := Seq(Tests.Filter(unitTestFilter)),
  testOptions in FunTest := Seq(Tests.Filter(funTestFilter)),
  ...

所以我可以做这样的事情:

So I can do something like that:

sbt干净覆盖率测试dockerComposeUp:test dockerComposeStop coverageReport

不幸的是,这杀死了我所有的覆盖率,只有生成的 BuildInfo 具有覆盖率。

Sadly that kills all my Coverage, only the generated BuildInfo has a Coverage.

仅使用 sbt干净覆盖率测试coverageReport sbt干净覆盖它:测试coverageReport 可以按预期工作。

Using only sbt clean coverage test coverageReport or sbt clean coverage it:test coverageReport work as expected.

整个项目可以在这里找到: https://github.com/pme123/play-binding-form

The whole project can be found here: https://github.com/pme123/play-binding-form

覆盖范围版本: 1.5.1

推荐答案

SBT支持增量编译,但是Scoverage不支持增量编译。 Scoverage会在编译开始之前清除检测信息,并每次都从头开始检测过程。编译所有启用了覆盖的类的子集都会导致错误的覆盖率报告。

SBT supports incremental compilation, but Scoverage does not support it. Scoverage clears instrumentation information before compilation starts and starts instrumentation process from scratch every time. Compilation of a subset of all classes with Scoverage enabled will result in wrong coverage reports.

在这种情况下, sbt-buldinfo 插件已在服务器模块中启用。它注册源生成器,该源生成器在每次编译之前执行,并生成 server / target / scala_2.12 / src_managed / main / sbt-buildinfo / BuildInfo.scala 文件。

In this case sbt-buldinfo plugin is enabled in server module. It registers source generator, which is executed before every compilation and generates server/target/scala_2.12/src_managed/main/sbt-buildinfo/BuildInfo.scala file.

SBT BuildInfo插件足够聪明,仅在内容更改时才可以重新生成此文件,但由于 BuildInfoOption.BuildTime 已包含在 buildInfoOptions 设置,在每次编译之前都会重新生成
这个文件。

SBT BuildInfo plugin is smart enough to regenerate this file only when its content changes, but since BuildInfoOption.BuildTime is included in buildInfoOptions setting, this file is regeneraged before every compilation.

在编译过程中,编译器每次找到一个修改后的文件( BuildInfo.scala ),并开始对此文件进行增量编译。 Scoverage清除其先前的检测信息,并仅保存有关 BuildInfo.scala 文件的信息。

When it comes to compilation process, compiler finds one modified file (BuildInfo.scala) every time and starts incremental compilation of this one file. Scoverage clears its previous instrumentation information and saves only information about BuildInfo.scala file.

如果执行 sbt干净覆盖率测试dockerComposeUp it:test dockerComposeStop coverageReport 第一个编译过程是 test 任务的一部分,第二个编译过程是 it:test 任务。这就是为什么单独使用它们时没有问题。

In case of execution like sbt clean coverage test dockerComposeUp it:test dockerComposeStop coverageReport the first compilation process is part of test task, and the second one it:test task. That's why there is no problem, when they are used separately.

Docker与我们的问题无关。

Docker has nothing to do with our problem.

要解决该问题,必须至少在启用覆盖率的情况下,防止每次编译时重新生成 BuildInfo.scala 文件。
我通过以下方式修改 project / Settings.scala 文件来做到这一点:

To fix the problem you have to prevent from BuildInfo.scala file regeneration on every compilation, at least when coverage is enabled. I did it by modifying project/Settings.scala file in this way:

  private lazy val buildInfoSettings = Seq(

    buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion),

    buildInfoOptions ++= { if (coverageEnabled.value) Seq() else Seq(BuildInfoOption.BuildTime) }, // <-- this line was changed
    buildInfoOptions += BuildInfoOption.ToJson,

    buildInfoPackage := "pme123.adapters.version"
  )

<$ c $打开覆盖范围时,c> buildInfoOptions 不包含 BuildTime 选项。

它看起来不大,但可以。您可能会找到更好的方法。

It doesn't look elegeant, but it works. You can probably find better way.

这篇关于覆盖率:合并测试范围和测试范围:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆