亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在maven 中進行單元測試測試代碼覆蓋率?

如何在maven 中進行單元測試測試代碼覆蓋率?

狐的傳說 2018-11-21 23:19:38
如何在maven 中進行單元測試測試代碼覆蓋率
查看完整描述

1 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

1.首先需要對統計模塊的POM.XML增加如下字段:




  1. <build>  

  2.         <plugins>  

  3.             <plugin>  

  4.                 <groupId>org.codehaus.mojo</groupId>  

  5.                 <artifactId>cobertura-maven-plugin</artifactId>  

  6.                 <version>2.6</version>

  7.                <configuration>

  8.                    <formats>

  9.                       <format>html</format>

  10.                       <format>xml</format>

  11.                    </formats>

  12.                 </configuration>  

  13.             </plugin>  

  14.         </plugins>  

  15.     </reporting>



或用<reporting>標簽,如下:


  1. <reporting>  

  2.         <plugins>  

  3.             <plugin>  

  4.                 <groupId>org.codehaus.mojo</groupId>  

  5.                 <artifactId>cobertura-maven-plugin</artifactId>  

  6.                 <version>2.6</version>  

  7.             </plugin>  

  8.         </plugins>  

  9.     </reporting>  


區別:在reporting節點中加入則在mvn site中執行,如果在build節點中加入,則在build的時候自動運行檢查。注意:如果是多模塊的maven項目,需要在每個想統計模塊的pom.xml中進行配置,這樣會將各模塊的報告進行匯集。
2.執行下面的cobertura命令



[plain] view plaincopy

  1. mvn cobertura:help          查看cobertura插件的幫助  

  2. mvn cobertura:clean         清空cobertura插件運行結果  

  3. mvn cobertura:check         運行cobertura的檢查任務  

  4. mvn cobertura:cobertura     運行cobertura的檢查任務并生成報表,報表生成在target/site/cobertura目錄下  

  5. cobertura:dump-datafile     Cobertura Datafile Dump Mojo  

  6. mvn cobertura:instrument    Instrument the compiled classes  


另,有的項目一些借口定義,常量定義和異常定義這些是不需要單元測試的,還有一些不重要的,我們可以進行過濾


按類的類別進行過濾

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>cobertura-maven-plugin</artifactId>

<version>2.5.2</version>

<configuration>

<ignores>

<!--經過修改的 cobertura, 支持方法級別的過濾 -->

<ignore>*main*</ignore>

<!--以上修改指的是過濾項目中所有類中的方法名中含有 main 的方法 -->

</ignores>

<IgnoreTrival>true</IgnoreTrival>

</configuration>

</plugin>

或對路徑過濾:

<configuration>    <instrumentation>    <excludes>    <!--此處用于指定哪些類會從單元測試的統計范圍中被剔除 -->                <exclude>exs/res/process/egencia/Mock*.class</exclude>                <exclude>exs/res/process/test/**/*Test.class</exclude> </excludes>    </instrumentation>    </configuration>    <executions>          <execution>                <goals>                     <goal>clean</goal>                </goals>           </execution>   </executions>



查看完整回答
反對 回復 2018-12-12
  • 1 回答
  • 0 關注
  • 1469 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號