Powermock and sonar jacoco的覆盖率不兼容问题解决 1

来源:互联网 发布:查成绩的软件 编辑:程序博客网 时间:2024/06/02 05:05

我们的系统一直使用Powermock做unit test的mock实现,之前就发现了Powermock在sonar的jacoco的测试覆盖率非常的低,所以我们一直使用cobertura作为我们的sonar测试覆盖率工具,在maven pom文件中使用cobertura的配置如下:

<properties>....<!-- define the plugin in sonar, not require after sonar 4.5 --><sonar.java.coveragePlugin>cobertura</sonar.java.coveragePlugin><!-- report path --><sonar.cobertura.reportPath>target/site/cobertura/cobertura.xml</sonar.cobertura.reportPath></properties><!-- plugin -->  <plugin>                 <groupId>org.codehaus.mojo</groupId>                 <artifactId>cobertura-maven-plugin</artifactId>                 <version>2.6</version>                 <configuration>                     <formats>                     <!-- generate xml report -->                        <format>xml</format>                     </formats>                     <instrumentation>                       <!-- ignore generated code -->                        <ignore>com.cloud.generated.*</ignore>                    </instrumentation>                </configuration>                 <executions>                     <execution>                         <id>generate-report</id>                        <phase>prepare-package</phase>                        <goals>                             <goal>cobertura</goal>                          </goals>                     </execution>                 </executions>             </plugin> 

这样在package phase就可以在sit/cobertura下看到cobertura的report,执行mvn sonar:sonar就可以扫描cobertura的结果做为test覆盖率。
Note1: sonar server必须安装了支持cobertura结果分析的plugin。
Note2:cobertura会额外多run一次test,在测试用例耗时不长的时候是件好事情,在测试失败的时候可以方便排查是否是cobertura instrument对测试产生了影响。

这一次公司系统环境迁移到新的sonar环境,就不再支持cobertura的配置,所以我们被迫需要转到jacoco测试覆盖率,可是Powermock和jacoco动态测试覆盖率实在不堪入目。我们在网上调查,发现有2种解决方案:
1. 静态Jacoco, offline instrument
2. Powermock Agent

这里先说第一种offline instrument,完整例子可以参考:http://www.eclemma.org/jacoco/trunk/doc/examples/build/pom-offline.xml
这里说说我用到的实现:

<properties>....<jacoco.version>0.7.8-SNAPSHOT</jacoco.version><!-- define ut/it report path, the same in our system due to sonar version is too old and doesn't support seperated it report--><sonar.jacoco.reportPath>${project.build.directory}/jacoco.exec</sonar.jacoco.reportPath><sonar.jacoco.itReportPath>${project.build.directory}/jacoco.exec</sonar.jacoco.itReportPath><!-- jacoco agent path for dymanic/static instrument--><sonar.jacoco.jar>${settings.localRepository}/org/jacoco/org.jacoco.agent/${jacoco.version}/org.jacoco.agent-${jacoco.version}-runtime.jar</sonar.jacoco.jar><!-- parameter used to control the instrument process, instrument for offline should only be used for ci phase not for loadbuild and package release --><jacoco.skip.instrument>true</jacoco.skip.instrument></properties><!-- include jacoco agent for instrument --><dependency><!--  must be on the classpath  --><groupId>org.jacoco</groupId><artifactId>org.jacoco.agent</artifactId><classifier>runtime</classifier><version>${jacoco.version}</version><scope>test</scope></dependency><plugins><plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>${jacoco.version}</version><configuration>  <excludes>      <exclude>com.cloud.generated.*</exclude>  </excludes>  <skip>${jacoco.skip.instrument}</skip></configuration><executions><execution><id>default-instrument</id><goals><goal>instrument</goal></goals></execution><execution><id>default-restore-instrumented-classes</id><goals><goal>restore-instrumented-classes</goal></goals></execution><execution><id>default-report</id><phase>prepare-package</phase><goals><goal>report</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.13</version><configuration><argLine>-XX:-UseSplitVerifier -javaagent:${sonar.jacoco.jar}=destfile=${sonar.jacoco.reportPath,includes=com.cloud.codes}</argLine><excludes>  <exclude>**/*IntegerationTest.java</exclude></configuration></plugin></plugins>

这种实现的问题主要在于支持不够,offline instrument在我们的系统测试过程中对powermockito和openjpa的支持很差,只好放弃选择方案2。方案二的实现在下一次的blog中细说。

1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 狗在屋里有味道怎么办 养狗家里有异味怎么办 养狗房子有味道怎么办 狗身上有腥臭味怎么办 狗狗不想养了怎么办 药流2天后没出血怎么办 药流当天血多怎么办 怀孕四十天不想要怎么办 人流后出血量大怎么办 药流一直不出血怎么办 药流一直不见红怎么办 药流12天还流血怎么办? 把别人车刮了怎么办 车划掉漆了怎么办啊 倒车时碰了车怎么办 车头掉了一点漆怎么办 新买的车被刮了怎么办 白色车车头漆掉了一小块怎么办 临时牌照丢了1张怎么办 临牌遗失了一张怎么办 临牌只有一张了怎么办 临时牌丢了一个怎么办 药流出血特别多怎么办 新车刮擦了一点怎么办 自己的车撞墙了怎么办 新车被撞了个坑怎么办 车门被刮变形了怎么办 撞到别人的新车怎么办 新车掉了一点漆怎么办 新车擦了点漆怎么办 车挂了一点漆怎么办 第一天上班中途想走了怎么办 车子前脸裂开了怎么办 不想干了想辞职怎么办 药流期间老是吐怎么办 在工厂上班辞工后没发工资怎么办 培训期三天想走怎么办 药流吃了药吐了怎么办 工作3天不下去怎么办 在新公司融不进去怎么办 药流吃药吐了怎么办