如果行或分支覆盖范围低于给定的阈值,我试图将我的WAR项目构建配置为失败。我一直在使用Java Power
Tools
一书中第455页提供的配置,但是没有成功。这是我的项目的Maven
2 POM的相关代码段:
<build>
...
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<check>
<!-- Per-class thresholds -->
<lineRate>80</lineRate>
<branchRate>80</branchRate>
<!-- Project-wide thresholds -->
<totalLineRate>90</totalLineRate>
<totalBranchRate>90</totalBranchRate>
</check>
<executions>
<execution>
<goals>
<goal>clean</goal>
<goal>check</goal>
</goals>
</execution>
<execution>
<id>coverage-tests</id>
<!-- The "verify" phase occurs just before "install" -->
<phase>verify</phase>
<goals>
<goal>clean</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
<instrumentation>
<excludes>
<exclude>au/**/*Constants.*</exclude>
</excludes>
<ignores>
<ignore>au/**/*Constants.*</ignore>
</ignores>
</instrumentation>
</configuration>
</plugin>
...
</plugins>
...
</build>
就像我说的那样,覆盖率报告工作正常,问题在于,如果行或分支覆盖率低于我指定的阈值,则“安装”目标不会失败。有人能正常工作吗?如果是这样,那么您的POM是什么样的?您使用的是哪个版本的Cobertura和Maven?我正在使用Maven
2.0.9和Cobertura 2.2。
我尝试了Googling并阅读了Cobertura文档,但没有运气(至少可以说后者很少)。
据我所知,
如果将该<haltOnFailure>
元素设置为true,并且任何指定的检查均失败,那么Cobertura将导致构建失败,这正是您所要求的。但是实际上,true
如果您未指定此元素,则默认为,因此
不必 将其添加到配置检查中。默认版本是(或至少应该是)使构建低于任何覆盖率阈值。
编辑: 我做了一些进一步的测试,并且haltOnFailure
似乎在我的环境(Maven
2.2.1。以及插件的2.3、2.2、2.1版,即Linux上的cobertura的1.9.2、1.9、1.8版)上按预期工作。我正在用以下结果更新此答案。
实际上,我已经<execution>
在pom中添加了一个元素。我可能会误解了cobertura:check的文档中说“ 默认绑定到生命周期阶段:verify
”的<execution>
部分,但是,没有该元素,在我的构建的 验证
阶段未触发cobertura:check。在我用于cobertura-maven-plugin的设置下面: __
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.3</version>
<configuration>
<check>
<!--<haltOnFailure>true</haltOnFailure>--><!-- optional -->
<!-- Per-class thresholds -->
<lineRate>80</lineRate>
<branchRate>80</branchRate>
<!-- Project-wide thresholds -->
<totalLineRate>90</totalLineRate>
<totalBranchRate>90</totalBranchRate>
</check>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<!--<goal>clean</goal>--><!-- works if uncommented -->
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
mvn clean install
在mvn archetype:create
使用上述插件配置修补的新生成的Maven项目(带有)上运行时,我得到以下结果:
$ mvn archetype:create -DgroupId=com.mycompany.samples -DartifactId=cobertura-haltonfailure-testcase
...
$ mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building cobertura-haltonfailure-testcase
[INFO] task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory /home/pascal/Projects/cobertura-haltonfailure-testcase/target
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to /home/pascal/Projects/cobertura-haltonfailure-testcase/target/classes
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 1 source file to /home/pascal/Projects/cobertura-haltonfailure-testcase/target/test-classes
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: /home/pascal/Projects/cobertura-haltonfailure-testcase/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mycompany.samples.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.09 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: /home/pascal/Projects/cobertura-haltonfailure-testcase/target/cobertura-haltonfailure-testcase-1.0-SNAPSHOT.jar
[INFO] Preparing cobertura:check
[WARNING] Removing: check from forked lifecycle, to prevent recursive invocation.
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [cobertura:instrument {execution: default}]
[INFO] Cobertura 1.9.2 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Instrumenting 1 file to /home/pascal/Projects/cobertura-haltonfailure-testcase/target/generated-classes/cobertura
Cobertura: Saved information on 1 classes.
Instrument time: 337ms
[INFO] Instrumentation was successful.
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/pascal/Projects/cobertura-haltonfailure-testcase/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: /home/pascal/Projects/cobertura-haltonfailure-testcase/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mycompany.samples.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.098 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] [cobertura:check {execution: default}]
[INFO] Cobertura 1.9.2 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Cobertura: Loaded information on 1 classes.
[ERROR] com.mycompany.samples.App failed check. Line coverage rate of 0.0% is below 80.0%
Project failed check. Total line coverage rate of 0.0% is below 90.0%
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Coverage check failed. See messages above.
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 18 seconds
[INFO] Finished at: Sat Oct 24 21:00:39 CEST 2009
[INFO] Final Memory: 17M/70M
[INFO] ------------------------------------------------------------------------
$
我没有使用maven 2.0.9进行测试,但是在我的机器上,haltOnFailure
生成了一个BUILD
ERROR并停止了构建。我没有发现您的插件配置有任何差异,也无法重现您描述的行为。
我正在为我的grails 3.1.6项目添加cobertura代码覆盖率,覆盖率报告似乎有点少,即我有单元测试用例(pos和neg两种情况)来覆盖if块,但报告说没有覆盖if块。gradle:2.13 Groovy:2.4.5 Grails:3.1.6 cobertura gradle Plugin:net.saliman:gradle-cobertura-plugin:2.3.2 这是我的代码
问题内容: 如果不满足覆盖率阈值(即覆盖率必须至少为80%或构建失败),那么对于给定的项目,我曾见过许多使Maven支持的Jenkins构建失败的文章。 我想知道是否有一种方法可以配置Jenkins使构建失败,如果覆盖率低于上一个构建,即如果构建N的覆盖率是20%,而N + 1的覆盖率是19%,那么构建将失败。我不想设定明确的门槛,但我希望覆盖范围保持稳定或随着时间的推移而提高。 问题答案: 将最
问题内容: 我从声纳休息API获取单元测试覆盖率百分比指标。 如果构建低于定义值,如何使构建失败? 问题答案: 提供该功能。 定义JaCoCo插件使用配置规则的和: 多种选择 支持的选项有: 线 科 指令 复杂 方法 类 我相信您可以进行一般检查(例如,验证整个项目的覆盖率至少为0.80)。 指令示例-整体指令覆盖率80% 本示例要求整个指令的覆盖率为80%,并且不得错过任何课程: 失败消息 如果
问题内容: 我正在使用声纳来衡量代码质量。我不知道的一件事是使用Cobertura测量代码覆盖率的步骤。 我按照http://cobertura.sourceforge.net/anttaskreference.html中的步骤进行操作,并且能够生成xml文件。如何将这些xml文件放入SONAR? 有没有在SONAR中使用Cobertura的简便方法? 我在与SONAR服务器不同的服务器上运行代码
我有以下代码,我需要重构它,以降低复杂性,增加模块化和封装性。我还需要减少ck度量值。 你如何重构这个代码?切换案例是否降低了复杂性?
问题内容: 有没有一种方法可以将代码排除在Cobertura覆盖率报告之外?我们提供了一些不应包含在覆盖率报告中的方法,因此不会降低覆盖率。 我知道四叶草具有这样的功能,但是我还没有为Cobertura找到任何类似的东西。 问题答案: 您可以从检测中排除类。然后它们不应出现在报告中。请参阅下面的 排除 语句。 您也可以忽略对某些方法的调用。请参阅下面的 ignore 语句。 如果您使用的是mave