1

我一直在尝试使用 Maven 在本地 Jenkins 服务器上设置代码覆盖率。我的测试似乎没有包含在覆盖率报告中。

我的 pom.xml 中有以下内容:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
        <artifactId>sonar-maven-plugin</artifactId>
        <version>2.0</version>
</plugin>
<plugin>
    <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.5.2</version>
        <configuration>
        <check>
        <haltOnFailure>false</haltOnFailure>
            <totalLineRate>85</totalLineRate>
        </check>
        <formats>
            <format>xml</format>
            <format>html</format>
        </formats>
        </configuration>
        <executions>
            <execution>
                <id>clean</id>
                <phase>pre-site</phase>
                <goals>
                   <goal>clean</goal>
                </goals>
            </execution>
            <execution>
                <id>instrument</id>
                <phase>site</phase>
                <goals>
                    <goal>instrument</goal>
                    <goal>cobertura</goal>
                </goals>
            </execution>
        </executions>
</plugin>

这是我定义要运行的测试的地方

<plugin>
    <!-- Separates the unit tests from the integration tests. -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <skip>true</skip>
        <trimStackTrace>false</trimStackTrace>
        </configuration>
    <executions>
        <execution>
            <id>unit-tests</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <skip>false</skip>
                <includes>
                    <include>**/*Test*.java</include>
                </includes>
                <excludes>
                    <exclude>**/*IT.java</exclude>
                </excludes>
            </configuration>
        </execution>

        <execution>
            <id>integration-tests</id>
            <phase>integration-test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <skip>false</skip>
                <includes>
                    <include>**/*IT.java</include>
                </includes>
            </configuration>
        </execution>
    </executions>
</plugin>

在我的 Jenkins 服务器上,我有以下内容: Jenkins 配置:声纳声纳名称:声纳本地 3.5.1 服务器网址:http://local host:9000

项目级别:发布 Cobertura Coverate 报告:xml 报告模式:**/target/site.covertura/coverage.xml

但是当我运行 maven 任务“mvn cobertura:cobertura”或在 Jenkins 中查看声纳报告时,所有的覆盖率都是 0%,这让我觉得我的单元和集成测试没有被看到。

这是我的 pom 分离单元测试和集成测试的方式的问题吗?

4

1 回答 1

2

cobertura 无法查看集成测试来查看代码覆盖率。

尝试通过使用声纳的集成测试以及分离集成和单元测试来查看代码覆盖率

做一个简短的故事:您必须启动 jacoco maven 插件,该插件将设置两个属性,一个用于单元测试,另一个用于它测试,并使用这些属性启动surefire和故障安全。

之后,您可以在分离的小程序中显示声纳结果。

在我自己的 pom 文件中,您将看到启用的 jacoco 的一部分:

            <properties>
               <!-- Coverage and report -->
               <jacoco.destFile.unit>${project.build.directory}/jacoco-unit.target</jacoco.destFile.unit>
               <jacoco.destFile.it>${project.build.directory}/jacoco-it.target</jacoco.destFile.it>
               <coverage.reports.dir>${project.build.directory}/coverage-reports</coverage.reports.dir>
               <version.surefire>2.12.4</version.surefire>

               <!-- Sonar -->
               <sonar.jacoco.itReportPath>${jacoco.destFile.it}</sonar.jacoco.itReportPath>
               <sonar.jacoco.reportPath>${jacoco.destFile.unit}</sonar.jacoco.reportPath>
               <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
               <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
            </properties>


            <build>
            ....
            <plugins>
                <!-- Jacoco configuration -->
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.6.0.201210061924</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                            <configuration>
                                <propertyName>jacoco.argLine.unit</propertyName>
                                <destFile>${jacoco.destFile.unit}</destFile>
                            </configuration>
                        </execution>
                        <execution>
                            <id>pre-integration-test</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                            <configuration>
                                <propertyName>jacoco.argLine.it</propertyName>
                                <destFile>${jacoco.destFile.it}</destFile>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <!-- And in the surefire and failsafe plugins you need to enable jacoco like this ->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${version.surefire}</version>
                    <dependencies>
                        <dependency>
                            <groupId>org.apache.maven.surefire</groupId>
                            <artifactId>surefire-junit47</artifactId>
                            <version>${version.surefire}</version>
                        </dependency>
                    </dependencies>
                    <configuration>
                        <argLine>${jacoco.argLine.unit}
                            -Dfile.encoding=${project.build.sourceEncoding} -Xmx512m</argLine>
                        <forkMode>always</forkMode>
                        <parallel>classes</parallel>
                    </configuration>
                </plugin-->

                <!-- Play the /src/test/java/**IT.java with goal verify -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>${version.surefire}</version>
                    <dependencies>
                        <dependency>
                            <groupId>org.apache.maven.surefire</groupId>
                            <artifactId>surefire-junit47</artifactId>
                            <version>${version.surefire}</version>
                        </dependency>
                    </dependencies>
                    <configuration>
                        <forkMode>pertest</forkMode>
                        <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                        <argLine>${jacoco.argLine.it}
                            -Dfile.encoding=${project.build.sourceEncoding} -Xmx512m</argLine>
                    </configuration>
                    <executions>
                        <execution>
                            <id>integration-test</id>
                            <phase>integration-test</phase>
                            <goals>
                                <goal>integration-test</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>verify</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

在 Jenkins 中,您必须使用“**/**.target”设置“postbuild”JaCoCo 插件作为 exec 文件的路径

希望这会帮助你。

于 2013-06-02T06:06:32.650 回答