为什么JaCoCo Jenkins报告中仅显示指令范围?

问题描述

我有一个Java项目,希望在Jenkins构建中看到自动化的测试报道。

为此,我如下所示修改文件

Jenkinsfile

pipeline {
    agent any
    stages {
        stage("Test") {
            steps {
                sh 'cat settings_default.xml'
                sh 'mvn -gs settings_default.xml test'
            }
        }
        stage("Test Reports") {
            steps {
                jacoco(
                        execPattern: 'target/*.exec',classpattern: 'target/classes',sourcePattern: 'src/main/java',exclusionPattern: 'src/test*',changeBuildStatus: true,runAlways: true,minimumBranchCoverage: '60'
                )
            }
        }
        stage("Build") {
            steps {
                sh 'mvn -gs settings_default.xml package'
            }
         }
    }
}

pom.xml:向build/plugins添加了以下片段:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.6</version>
    <configuration>
        <excludes>
            <exclude>**/configuration/*.*</exclude>
            <exclude>**/model/*</exclude>
            <exclude>**/MyApplication.*</exclude>
        </excludes>
    </configuration>
    <executions>
        <execution>
            <id>prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>check-coverage</id>
            <goals>
                <goal>check</goal>
            </goals>
            <configuration>
                <rules>
                    <rule>
                        <element>CLASS</element>
                        <limits>
                            <limit>
                                <counter>LINE</counter>
                                <value>COVEREdratIO</value>
                                <minimum>0.80</minimum>
                            </limit>
                            <limit>
                                <counter>BRANCH</counter>
                                <value>COVEREdratIO</value>
                                <minimum>0.80</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
        <execution>
            <id>report</id>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

当我运行詹金斯(Jenkins)作业时,我只会在报告中看到说明范围:

Screenshot of a Jenkins build with only instruction coverage being displayed

我需要更改什么?在哪里(pom.xmlJenkinsfile,服务器上的Jenkins配置等等)才能查看分支和类的覆盖范围?

更新1:正在添加

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.6</version>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>report</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>

        </plugins>
    </reporting>

pom.xml不能解决问题。

解决方法

要查看完整的jacoco报告,我将使用HTML Publisher插件。

首先,您需要配置pom.xml文件以生成jacoco覆盖率报告,并添加以下插件。

           <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.6</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <!-- attached to Maven test phase -->
                    <execution>
                        <id>report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

然后,在运行mvn install命令之后,jacoco报告将在目录target / site / jacoco / index.html中生成。

现在,您必须配置Jenkins才能显示此报告。因此,下一步是在Jenkins中安装HTML Publisher plugin。您可以使用插件管理器执行此任务。

最后,配置您的Jenkins作业以运行mvn install命令,并将HTML Publisher设置为构建后任务。基本上,您告诉Jenkins阅读并显示了jacoco报告。

HTML Publisher configuration

另一个细节是,您需要使用以下命令运行Jenkins,以便在可视化报告时避免使用problems

java -Dhudson.model.DirectoryBrowserSupport.CSP="sandbox allow-same-origin allow-scripts; default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self'; style-src 'self' 'unsafe-inline'; font-src *;"  -jar jenkins.war 

最终报告将如下所示:

jacoco report

,

发生错误的原因是,这是一个具有两个不同的pom.xml的多模块Maven项目。正在为模块(子目录)生成Jenkins报告,并且其中一个步骤配置错误(配置指向错误的目录)。

        stage("Test Reports") {
            steps {
                dir('my-module') {
                    jacoco( 
[...]

jacoco包围dir('my-module') { [...] }语句解决了这个问题。