Cobertura和Jetty

4
我正在尝试在Jetty上运行我的Web应用程序时使用Cobertura获取覆盖率报告。 我们已经通过使用surefire插件对单元测试运行了cobertura。 我们还配置了failsafe插件来运行集成测试。
我已经(手动)为我的war进行了仪器化并部署了它。
当使用仅包含集成测试的配置文件运行“mvn verify”时,似乎Cobertura正在工作,因为我在eclipse控制台中得到了各种新的警告(我从那里运行jetty),这可能是因为字节码被Cobertura更改了。 但是,即使在Jetty服务器上调用“stop”,我也没有得到要写入的.ser文件。
当运行“mvn cobertura:cobertura”时,我会得到一个.ser文件,并且在我的Web应用程序的target / site目录下生成报告。该报告显示0%的覆盖率,因为“cobertura:cobertura”不运行任何测试。
如何使用failsafe运行我的集成测试使Cobertura正常工作? 有什么其他建议吗?
谢谢, Ben
2个回答

5

我使用cobertura-it插件解决了这个问题。它扩展了原始的cobertura插件,并允许使用仅报告目标。此外,我必须运行两个单独的命令(请参见下文)以测试和生成报告(合并成一个命令不起作用)。以下是我的插件配置。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-it-maven-plugin</artifactId>
    <version>2.5</version>
    <configuration>
        <formats>
            <format>html</format>
        </formats>
        <check>
            <haltOnFailure>false</haltOnFailure>
        </check>
    </configuration>
    <executions>
        <execution>
            <id>pre-integration-test</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>cobertura</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>            
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>7.4.2.v20110526</version>
    <configuration>
        <stopKey>aaaaaaaaaaaaa</stopKey>
        <stopPort>8085</stopPort>
    </configuration>
    <executions>
        <execution>
            <id>start-jetty</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <scanIntervalSeconds>0</scanIntervalSeconds>
                <daemon>true</daemon>
                <classesDirectory>target/generated-classes/cobertura</classesDirectory>
            </configuration>
        </execution>
        <execution>
            <id>stop-jetty</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>net.sourceforge.cobertura</groupId>
            <artifactId>cobertura</artifactId>
            <version>1.9.4.1</version>
        </dependency>
    </dependencies>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.10</version>
    <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>

所以我运行了这个命令:

mvn clean verify

请注意,在Jetty停止后,会出现一些cobertura消息:

[INFO] -------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] -------------------------------------------------------
[INFO] Total time: 1 minute 13 seconds
[INFO] Finished at: Sun Nov 13 12:58:29 ICT 2011
[INFO] Final Memory: 86M/204M
[INFO] -------------------------------------------------------
2011-11-13 12:58:29.765:WARN::4 threads could not be stopped
Flushing results...
Flushing results done
Cobertura: Loaded information on 342 classes.
Cobertura: Saved information on 342 classes.

最后,我使用mvn site生成报告。以下是我的报告配置。
<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <!-- An error on version 2.8 -->
            <version>2.7</version>
            <configuration>
                <reportsDirectories>
                    <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                    <reportsDirectory>${project.build.directory}/failsafe-reports</reportsDirectory>
                </reportsDirectories>
            </configuration>
            <reportSets>
                <reportSet>
                    <reports>
                        <report>report-only</report>
                    </reports>
                </reportSet>
            </reportSets>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-project-info-reports-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <!-- An error that takes long time to generate this report -->
                <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
            </configuration>
            <reportSets>
                <reportSet>
                    <reports>
                        <report>dependencies</report>
                    </reports>
                </reportSet>
            </reportSets>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-it-maven-plugin</artifactId>
            <configuration>
                <formats>
                    <format>html</format>
                    <format>xml</format>
                </formats>
            </configuration>
            <reportSets>
                <reportSet>
                    <reports>
                        <report>report-only</report>
                    </reports>
                </reportSet>
            </reportSets>
        </plugin>
    </plugins>
</reporting>

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接