簡體   English   中英

使用Sonar和maven進行Jacoco代碼覆蓋,以便在單獨的模塊中進行集成測試

[英]Jacoco code coverage with Sonar and maven for integration tests in separate module

我已經使用SonarQube和Jacoco配置我的項目以進行代碼覆蓋。 除了一件事,一切都很好。 我在許多maven子模塊中划分了項目:

project (pom.xml) - 
|-moduleA (no pom here)
|   |- it (pom.xml) - integration tests for "impl" module
|   |- impl (pom.xml) - implementaion + Unit tests
|-moduelB
|   |- it (pom.xml) - integration tests for "impl" module
|   |- impl (pom.xml) - implementaion + Unit tests
|-moduleC ...

我使用以下命令開始構建:

mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent -Dmaven.compiler.debug=true install sonar:sonar

問題是,Jacoco可輕松支持在同一個模塊內的單元測試覆蓋率(所以在我的情況下,單元測試impl模塊),但不能從分析覆蓋率it模塊,本身不包含任何實現類。 它只包含impl模塊的集成測試。 我很清楚為什么它不起作用。 我在Jacoco日志中得到這樣的信息:

No JaCoCo analysis of project coverage can be done since there is no class files.

我在moduleA / it / pom.xml中定義了sonar.java.binaries屬性,如下所示:

    <properties>
         <sonar.java.binaries>../impl/target/classes</sonar.java.binaries>
    </properties>

it覆蓋率分析仍然失敗,但Jacoco日志中的消息變為:

[INFO] Analysing D:\project\moduleA\it\target\jacoco.exec
[WARNING] Coverage information was not collected. Perhaps you forget to include debug information into compiled classes?

我必須提到D:\\project\\moduleA\\it\\target\\jacoco.exec文件存在。 我還檢查了D:\\project\\moduleA\\impl\\target\\classes中的編譯類包含調試數據(所有行,變量和源)。

我在sonar.java.binaries中嘗試不同的路徑,但結果總是一樣的。 我試過了:

../impl/target/classes
../../impl/target/classes
../../../impl/target/classes
../impl/target
...

如何配置Jacoco(聲納)以允許它在與jacoco.exec文件相關的不同maven子模塊中查找類二進制文件?

基於這個問題 ,我使用了JaCoCo的ant任務庫的Maven Ant插件 ,它允許更豐富的包含樹:

<project ...>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <dependencies>
                    <dependency>
                        <groupId>org.jacoco</groupId>
                        <artifactId>org.jacoco.ant</artifactId>
                        <version>${jacoco.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>ant-contrib</groupId>
                        <artifactId>ant-contrib</artifactId>
                        <version>20020829</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>report-it-coverage</id>
                        <phase>post-integration-test</phase>
                        <goals><goal>run</goal></goals>
                        <configuration>
                            <skip>${skipITs}</skip>
                            <target name="report-it-coverage">
                                <taskdef name="jacoco-report" classname="org.jacoco.ant.ReportTask" classpathref="maven.plugin.classpath" />
                                <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="maven.runtime.classpath" />
                                <available file="${jacoco.it-coverage.data}" property="jacoco.exec.file.exists" />
                                <if>
                                    <equals arg1="${jacoco.exec.file.exists}" arg2="true" />
                                    <then>
                                        <echo>Analyzing ITs coverage data from '${jacoco.it-coverage.data}'</echo>
                                        <jacoco-report>
                                            <executiondata>
                                                <file file="${jacoco.it-coverage.data}" />
                                            </executiondata>
                                            <structure name="ROOT">
                                                <classfiles>
                                                    <fileset dir="${project.basedir}/${project.parent.relativePath}">
                                                        <include name="**/target/classes/my/company/package/**/*.class" />
                                                        <exclude name="**/target/classes/my/test/glue/**/*" />
                                                    </fileset>
                                                </classfiles>
                                            </structure>
                                            <html destdir="${project.reporting.outputDirectory}/jacoco-it" />
                                            <xml destfile="${project.build.directory}/jacoco.xml" />
                                        </jacoco-report>
                                    </then>
                                    <else>
                                        <echo>Missing ITs coverage data file '${jacoco.it-coverage.data}'</echo>
                                    </else>
                                </if>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM