如何使Cobertura在M2构建中失败以降低代码覆盖率

如何使Cobertura在M2构建中失败以降低代码覆盖率,第1张

如何使Cobertura在M2构建中失败以降低代码覆盖率

据我所知,
如果将该

<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.prehaus.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
mvnarchetype: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.AppTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.09 secResults :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 fileInstrumenting 1 file to /home/pascal/Projects/cobertura-haltonfailure-testcase/target/generated-classes/coberturaCobertura: 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.AppTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.098 secResults :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 fileCobertura: 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并停止了构建。我没有发现您的插件配置有任何差异,也无法重现您描述的行为。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5616174.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存