Scala 代码覆盖工具

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/953998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 01:26:28  来源:igfitidea点击:

Code Coverage Tools for Scala

unit-testingscalacontinuous-integrationhudsoncode-coverage

提问by arnab

What are the available code coverage tools for Scala?

Scala 有哪些可用的代码覆盖率工具?

I have Scala spec tests and a Hudson continuous integration set-up. Is there something I can hook-in to this setup to measure and track code coverage?

我有 Scala 规范测试和 Hudson 持续集成设置。有什么我可以挂钩到这个设置来衡量和跟踪代码覆盖率的吗?

回答by Joni

SCCT is a compiler plugin which instruments the classes to gather coverage data:

SCCT 是一个编译器插件,它检测类以收集覆盖数据:

http://mtkopone.github.com/scct/

http://mtkopone.github.com/scct/

回答by Daniel Spiewak

I use Cobertura. However, any Java coverage tool should work just fine. The only catch is that you will end up with a large number of auto-generated classes in your coverage list. This is because while Scala compiles down into very natural JVM bytecode, it is forced to produce an unnaturally large number of classes to accommodate common functional features like lazy evaluation.

我使用Cobertura。但是,任何 Java 覆盖工具都应该可以正常工作。唯一的问题是,您最终会在覆盖列表中看到大量自动生成的类。这是因为当 Scala 编译成非常自然的 JVM 字节码时,它被迫生成不自然的大量类来适应常见的功能特性,如惰性求值。

回答by user152392

Undercoveris little better.

卧底好一点。

回答by Ira Baxter

One problem with non-mainstream languages (such as Scala) is that tools are hard to find, because they are hard to build.

非主流语言(例如 Scala)的一个问题是工具很难找到,因为它们很难构建。

This technical paper Branch Coverage for Arbitrary Languages Made Easy(I'm the author) describes how to build test coverage tools for langauges in systematic way to help get around this problem, using a generic tool-building infrastructure.

这篇技术论文Branch Coverage for Arbitrary Languages Made Easy(我是作者)描述了如何使用通用的工具构建基础结构以系统的方式构建语言的测试覆盖率工具,以帮助解决这个问题。

We've implemented test coverage tools for Java, C#, COBOL, C, C++, PL/SQL, ...this way, including instrumenters, data collection and test coverage display and reporting. It would be straightforward to implement Scala like this.

我们已经为 Java、C#、COBOL、C、C++、PL/SQL 等实现了测试覆盖工具,...这样,包括检测器、数据收集和测试覆盖显示和报告。像这样实现 Scala 会很简单。

The solutions posed by other answers produces confusing information from the implementation of Scala ("auto genreated classes"). What developers want to see is coverage data in terms of their code. The approach we use instruments the source code, so the results are stated entirely and only in terms of the source code; even the test coverage viewer shows the source code covered with coverage information.

其他答案提出的解决方案从 Scala 的实现(“自动类型化类”)中产生了令人困惑的信息。开发人员希望看到的是在他们的代码方面的覆盖率数据。我们使用的方法是对源代码进行检测,因此结果完全且仅根据源代码进行说明;甚至测试覆盖率查看器也会显示覆盖率信息所覆盖的源代码。

回答by theon

I've put together a SBT plugin called xsbt-coveralls-pluginthat uses scct under the hood, but publishes the results to http://coveralls.io.

我已经组合了一个名为xsbt-coveralls-plugin 的 SBT 插件,它在底层使用 scct,但将结果发布到http://coveralls.io

Disclaimer:I've only just built this plugin yesterday (10th March 2013) so don't expect it to be perfect yet, but do send bugs and feature requests to the github page

免责声明:我昨天(2013 年 3 月 10 日)才刚刚构建了这个插件,所以不要指望它是完美的,但请向 github 页面发送错误和功能请求

Still, it's good if you want to code coverage reports to be publicly visible. Check out an example of the results here

尽管如此,如果您希望将覆盖率报告编码为公开可见,这很好。在此处查看结果示例

回答by Andriy Plokhotnyuk

I use jacoco. It does not require compile- or runtime- dependencies, instruments classes on the fly w/o special instrumentation phase.

我用 jacoco。它不需要编译或运行时依赖性,无需特殊检测阶段即可动态检测类。

Also it integrated with Sonar and published on Maven Central.

它还与 Sonar 集成并发布在 Maven Central 上。

Here is example: https://github.com/Godin/sonar-experiments/tree/master/jacoco-examples/scala-example

这是示例:https: //github.com/Godin/sonar-experiments/tree/master/jacoco-examples/scala-example

I would like to add better reporting: more detailed branch coverage makrup, excluding of generated classes/methods, and to be handy like ScalaDoc (see SCCT reports for example)

我想添加更好的报告:更详细的分支覆盖 makrup,不包括生成的类/方法,并且像 ScalaDoc 一样方便(例如参见 SCCT 报告)