【问题标题】:maven-surefire-plugin, jacoco-maven-plugin show no coveragemaven-surefire-plugin, jacoco-maven-plugin 显示没有覆盖
【发布时间】:2016-10-02 10:16:51
【问题描述】:

我正在使用 maven-surefire-plugin 2.17 版和 maven。

我正在使用 jacoco-maven-plugin 来分析我的 junit 测试:

我在 pom.xml 中设置的 jacoco 插件如下所示:

    <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.6.201602180812</version>
            <executions>
                <execution>
                    <id>agent-for-ut</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <destFile>${sonar.jacoco.reportPath}</destFile>
                        <append>true</append>
                    </configuration>
                </execution>
                <execution>
                    <id>post-unit-test</id>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>${sonar.jacoco.reportPath}</dataFile>
                        <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

我在 src/main/java/something/excel 下有一个类,如下所示:

package something.excel;

public class VDHTCStyle
{
    public int doSomething() {
        int i=0;
        return i+7;
    }
}

我在 src/test/java/something/excel 下的测试类是这样的:

package something.excel;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class VDHTCStyleStyleTest
{
    private VDHTCStyleStyle vDHTCStyleStyle;

    @Before
    public void setUp()
    {
        vDHTCStyleStyle = new VDHTCStyleStyle();
    }

    @Test
    public void shouldDoSomething() {
        int something = vDHTCStyleStyle.doSomething();
        assertEquals(something, 7);
    }

}

当我用

运行它时
mvn clean install

我在日志中看到了这一点:

[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ xxxxx ---
[INFO] Surefire report directory: /var/lib/jenkins/workspace/xxxx/modules/xx/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running something.excel.VDHTCStyleTest
|classnames | 2.2.3 | A simple utility for conditionally joining classNames together|
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.062 sec - in something.excel.VDHTCStyleTest 

现在,如果我查看生成的 jacoco-ut 报告文件,我会看到如下内容:

GROUP,PACKAGE,CLASS,INSTRUCTION_MISSED,INSTRUCTION_COVERED,BRANCH_MISSED,BRANCH_COVERED,LINE_MISSED,LINE_COVERED,COMPLEXITY_MISSED,COMPLEXITY_COVERED,METHOD_MISSED,METHOD_COVERED
Module: xxx,something.excel,VDHTCStyleStyle,9,0,0,0,3,0,2,0,2,0

据我了解,它认为没有覆盖任何行、指令、分支或方法。

为什么?帮助:)

【问题讨论】:

    标签: maven-surefire-plugin jacoco-maven-plugin


    【解决方案1】:

    问题似乎是 maven-surefire-plugin 在多模块项目中指定两次时行为不可预测

    我的父 pom 在构建部分有它,子模块 pom 在构建部分也有它。因此,它似乎正在运行测试,但子模块中的覆盖率始终为 0。

    修复方法是从子模块 pom 中删除 maven-surefire-plugin,并且只将它放在父 pom 的构建部分中。

    问题已解决。

    【讨论】:

    • 这是针对特定版本的surefire吗?它被报告为错误吗?
    • 不,我没有将其报告为错误,因为我觉得这是我的错误配置(我已经包含了两次)
    猜你喜欢
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 2016-03-05
    • 2021-03-31
    • 2012-02-11
    • 1970-01-01
    • 2014-06-28
    • 2020-11-09
    相关资源
    最近更新 更多