#contents
 ----
 -Jenkins + Maven で Coverage Report を出す
 -手元の IDE (Eclipse/Netbeans) でもテストカバレッジとテストで通っていない行を知りたい
 *Eclipse [#z59cb00c]
 -djUnit
 --http://works.dgic.co.jp/djwiki/
 --定番だったけど @RunWith(Enclosed.class) との相性がうまくないみたい (2013-01-06)
 -EclMMA
 --http://www.eclemma.org/
 --Eclipseマーケットプレイスからインストール可能
 *Netbeans [#lf6717e9]
 -Netbeans 7.2 で Maven Project の場合、標準で対応している。今日日 Maven を使わないなんてあり得ないので、標準で対応していると言っていいだろう
 --カバレッジを表示するためには pom.xml に、JaCoCo plugin を登録する必要がある
 #code(xml){{
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
 
     <groupId>com.mycompany</groupId>
     <artifactId>Sandbox</artifactId>
     <version>1.0-SNAPSHOT</version>
     <packaging>jar</packaging>
 
     <name>Sandbox</name>
     <url>http://maven.apache.org</url>
     
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>
     
     <build>
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
                 <version>3.0</version>
                 <configuration>
                     <source>1.7</source>
                     <target>1.7</target>
                 </configuration>
             </plugin>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-site-plugin</artifactId>
                 <version>3.2</version>
                 <configuration>
                     <locales>ja</locales>
                     <inputEncoding>UTF-8</inputEncoding>
                     <outputEncoding>UTF-8</outputEncoding>
                     <reportPlugins>
                         <plugin>
                             <groupId>org.jacoco</groupId>
                             <artifactId>jacoco-maven-plugin</artifactId>
                             <version>0.6.0.201210061924</version>
                         </plugin>
                     </reportPlugins>
                 </configuration>
             </plugin>            
         </plugins>
     </build>
 
     <dependencies>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>4.11</version>
             <scope>test</scope>
         </dependency>
     </dependencies>
 </project>
 }}
 --そのうえで、テストを実行すると、[プロジェクト右クリック] - [コードカバレッジ] で、コードカバレッジと通過箇所を見ることができる
 #ref(coverage1.png)
 #br
 #ref(coverage2.png)
 #br
 #ref(coverage3.png)
 #br
 -Ant Project の場合には、Cobertura Module Test Coverage
 --http://platform.netbeans.org/tutorials/nbm-test.html
 *Jacoco よりも Covertura のほうが良さげ (2014-01-09 追記) [#u8fdd1de]
 -Jacoco は設定が難しい
 --[Jenkins][Jacoco]JenkinsとJacocoでカバレッジを取る, http://d.hatena.ne.jp/gloryof/20130713/1373679979
 --jacoco + jmockit = pom.xmlに工夫が必要, http://s-parts.info/post/67833216230/jacoco-jmockit-pom-xml
 -Jacoco のダメなところ
 --Jacoco は、(カバレッジ計測用に) バイトコードを汚すので意図せず build が止まると、不正なバイトコードが残る
 --複数回 compile や test が走ると、"Build errors: Class already instrumented" というエラーが出て build が止まる。エラーを無視するオプションはない
 -利用例 (この設定で Netbeans 7.3.1 でカバレッジ表示ができる)
 #code(xml){{{
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
 
     <groupId>com.example</groupId>
     <artifactId>Quiz7</artifactId>
     <version>1.0-SNAPSHOT</version>
     <packaging>jar</packaging>
 
     <name>Quiz7</name>
     <url>http://maven.apache.org</url>
 
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <jmockit.version>1.4</jmockit.version>
         <jmockit.coverage.version>0.999.20</jmockit.coverage.version>
     </properties>
 
     <dependencies>
         <dependency>
             <groupId>commons-lang</groupId>
             <artifactId>commons-lang</artifactId>
             <version>2.6</version>
             <type>jar</type>
         </dependency>
         <!-- jMockit (Must define before JUnit!) -->
         <dependency>
             <groupId>com.googlecode.jmockit</groupId>
             <artifactId>jmockit</artifactId>
             <version>${jmockit.version}</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>com.googlecode.jmockit</groupId>
             <artifactId>jmockit-coverage</artifactId>
             <version>${jmockit.coverage.version}</version>
             <scope>runtime</scope>
         </dependency>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>4.10</version>
             <scope>test</scope>
             <exclusions>
                 <exclusion>
                     <groupId>org.hamcrest</groupId>
                     <artifactId>hamcrest-core</artifactId>
                 </exclusion>
             </exclusions>
         </dependency>
         <dependency>
             <groupId>org.hamcrest</groupId>
             <artifactId>hamcrest-all</artifactId>
             <version>1.3</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>commons-beanutils</groupId>
             <artifactId>commons-beanutils</artifactId>
             <version>1.8.3</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>org.apache.bcel</groupId>
             <artifactId>bcel</artifactId>
             <version>5.2</version>
         </dependency>
     </dependencies>
     <build>
         <plugins>
             <!-- OSGi Bundle Ready -->
             <plugin>
                 <groupId>org.apache.felix</groupId>
                 <artifactId>maven-bundle-plugin</artifactId>
                 <version>2.3.7</version>
                 <extensions>true</extensions>
             </plugin>
             <!-- Compile -->
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
                 <version>2.3.2</version>
                 <configuration>
                     <source>1.7</source>
                     <target>1.7</target>
                 </configuration>
             </plugin>
             <!-- Copy jMockit coverage to site. phase は、post-site になるはずだけど、それではうまく動かない -->
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-antrun-plugin</artifactId>
                 <version>1.7</version>
                 <executions>
                     <execution>
                         <id>site</id>
                         <phase>site</phase>
                         <goals>
                             <goal>run</goal>
                         </goals>
                         <configuration>
                             <tasks>
                                 <echo>copy coverage report to site</echo>
                                 <copy todir="target/site/coverage-report">
                                     <fileset dir="coverage-report" includes="**/*"/>
                                 </copy>
                             </tasks>
                         </configuration>
                     </execution>
                 </executions>
             </plugin>            
             <!-- Coverage -->
             <plugin>
                 <groupId>org.codehaus.mojo</groupId>
                 <artifactId>cobertura-maven-plugin</artifactId>
                 <version>2.6</version>
             </plugin>
             <!-- Site -->
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-site-plugin</artifactId>
                 <version>3.3</version>
                 <configuration>
                     <locales>ja</locales>
                     <inputEncoding>UTF-8</inputEncoding>
                     <outputEncoding>UTF-8</outputEncoding>
                     <reportPlugins>
                         <!-- Project Info -->
                         <plugin>
                             <groupId>org.apache.maven.plugins</groupId>
                             <artifactId>maven-project-info-reports-plugin</artifactId>
                             <version>2.7</version>
                             <reportSets>
                                 <reportSet>
                                     <reports>
                                         <report>index</report>
                                         <report>dependencies</report>
                                         <!--
                                         <report>dependency-convergence</report>
                                         <report>dependency-management</report>
                                         <report>plugins</report>
                                         <report>plugin-management</report>
                                         <report>cim</report>
                                         <report>issue-tracking</report>
                                         <report>license</report>
                                         <report>mailing-list</report>
                                         <report>project-team</report>
                                         <report>scm</report>
                                         <report>summary</report>
                                         -->
                                     </reports>
                                 </reportSet>
                             </reportSets>
                         </plugin>
                         <!-- Javadoc -->
                         <plugin>
                             <groupId>org.apache.maven.plugins</groupId>
                             <artifactId>maven-javadoc-plugin</artifactId>
                             <version>2.9.1</version>
                             <configuration>
                                 <docencoding>UTF-8</docencoding>
                                 <charset>UTF-8</charset>
                                 <encoding>UTF-8</encoding>
                                 <bottom>Copyright &amp;copy; 2014 Snail Co. LTD.All Rights Reserved.</bottom>
                             </configuration>
                         </plugin>
                         <!-- Source code -->
                         <plugin>
                             <groupId>org.apache.maven.plugins</groupId>
                             <artifactId>maven-jxr-plugin</artifactId>
                             <version>2.3</version>
                         </plugin>
                         <!-- Checkstyle -->
                         <plugin>
                             <groupId>org.apache.maven.plugins</groupId>
                             <artifactId>maven-checkstyle-plugin</artifactId>
                             <version>2.10</version>
                             <configuration>
                                 <configLocation>src/conf/Checkstyle_MyRule.xml</configLocation>
                                 <excludes>**/test/*.java</excludes>
                             </configuration>
                         </plugin>
                         <!-- Findbugs -->
                         <plugin>
                             <groupId>org.codehaus.mojo</groupId>
                             <artifactId>findbugs-maven-plugin</artifactId>
                             <version>2.5.2</version>
                         </plugin>
                         <!-- PMD -->
                         <plugin>
                             <groupId>org.apache.maven.plugins</groupId>
                             <artifactId>maven-pmd-plugin</artifactId>
                             <version>3.0.1</version>
                         </plugin>
                         <!-- Test -->
                         <plugin>
                             <groupId>org.apache.maven.plugins</groupId>
                             <artifactId>maven-surefire-report-plugin</artifactId>
                             <version>2.16</version>
                             <!-- PENDING: I want to generate the coverage report under {prj}/target/site/coverage-report,
                             but I can't overwrite default setting, {prj}/coverage-report.
                             cf http://jmockit.googlecode.com/svn/trunk/www/tutorial/CodeCoverage.html -->
                         </plugin>
                         <!-- TODO -->
                         <plugin>
                             <groupId>org.codehaus.mojo</groupId>
                             <artifactId>taglist-maven-plugin</artifactId>
                             <version>2.4</version>
                         </plugin>
                     </reportPlugins>
                 </configuration>
             </plugin>
         </plugins>
     </build>
 </project>
 }}}
 -site には、カバレッジが出ないので jmockit-coverage report を記録として使う。まぁ、Jenkins が cobertura の実行結果を拾ってくれるんで、それで良いっちゃあ良いんだが
 ----
 [[Java#xUNIT]]

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Short-URL: http://at-sushi.com/pukiwiki/index.php?cmd=s&k=70f2c17cd5
ISBN10
ISBN13
9784061426061