Cobertura代码覆盖是错误的

时间:2013-06-01 15:17:03

标签: unit-testing maven sonarqube cobertura

我一直在尝试使用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服务器上,我有以下内容: 詹金斯配置:     声纳     声纳名称:声纳本地3.5.1     服务器网址:http://local host:9000

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

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

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

1 个答案:

答案 0 :(得分:2)

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

看看code coverage by integration tests with sonar 并在separating integration and unit tests

简而言之:您必须启动jacoco maven插件,该插件将设置两个属性,一个用于单元测试,另一个用于测试,并使用这些属性启动surefire和failsafe。

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

在我自己的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中,你必须设置“postbuild”JaCoCo插件 执行文件路径的“** / **。target”

希望这会对你有所帮助。