简体   繁体   English

使用Jacoco for Scala进行单元测试范围

[英]Unit tests coverage using Jacoco for Scala

When I using the follow structure on our Maven project: 当我在Maven项目中使用以下结构时:

<plugins>
        <plugin>
            <groupId>org.scala-tools</groupId>
            <artifactId>maven-scala-plugin</artifactId>
            <version>2.15.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <args>
                    <arg>-g:line</arg>
                </args>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.5.8.201207111220</version>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

The tests are executed but the jacoco.exec file is not created. 执行了测试,但未创建jacoco.exec文件。 I tried to add this tag destFile to prepare-agent goal but have not success (the file with coverage information is not created). 我试图将此标签destFile添加到prepare-agent目标,但没有成功(未创建包含coverage信息的文件)。

Anyone have any form to calculate code coverage of unit test for Scala using maven and Jacoco? 是否有人使用maven和Jacoco计算Scala单元测试的代码覆盖率?

Work of Jacoco maven plugin depends on versions of JDK, Scala and plugin itself. Jacoco maven插件的工作取决于JDK,Scala和插件本身的版本。

Just added the same config and it works properly: both jacoco.exec and site/jacoco with reports are in the target dir: https://github.com/plokhotnyuk/calculator/commit/e3a84db02dd942b0df71d4fa337df29512e213f6 只需添加相同的配置即可正常运行:jacoco.exec和带有报告的site / jacoco都位于目标目录中: https : //github.com/plokhotnyuk/calculator/commit/e3a84db02dd942b0df71d4fa337df29512e213f6

Then got agent failure when using latest (0.6.2.201302030002) version of Jacoco maven plugin: 然后在使用最新(0.6.2.201302030002)版本的Jacoco maven插件时出现代理故障:

Caused by: java.lang.RuntimeException: Class java/util/UUID could not be instrumented.
    at org.jacoco.agent.rt.internal_5d10cad.core.runtime.ModifiedSystemClassRuntime.createFor(ModifiedSystemClassRuntime.java:138)
    at org.jacoco.agent.rt.internal_5d10cad.core.runtime.ModifiedSystemClassRuntime.createFor(ModifiedSystemClassRuntime.java:99)
    at org.jacoco.agent.rt.internal_5d10cad.PreMain.createRuntime(PreMain.java:51)
    at org.jacoco.agent.rt.internal_5d10cad.PreMain.premain(PreMain.java:43)
    ... 6 more
Caused by: java.lang.NoSuchFieldException: $jacocoAccess
    at java.lang.Class.getField(Class.java:1542)
    at org.jacoco.agent.rt.internal_5d10cad.core.runtime.ModifiedSystemClassRuntime.createFor(ModifiedSystemClassRuntime.java:136)
    ... 9 more
FATAL ERROR in native method: processing of -javaagent failed
Exception in thread "main" 

And then the failure was worked out by switching from JDK8 to JDK7. 然后通过从JDK8切换到JDK7来解决故障。 Coverage report digits didn't changed. 承保范围报告数字未更改。

Also after switching to Scala 2.10 source line highlighting started looks different: 同样,切换到Scala 2.10后,开始突出显示源代码行看起来也不同:

Scala 2.9 斯卡拉2.9 在此处输入图片说明

Scala 2.10 Scala 2.10 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM