Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences


Referenced by
Articles
Articles_de
Articles_pt




JSPWiki v2.2.33

[RSS]


Hide Menu

CodeCoverage


This is version 5. It is not the current version, and thus it cannot be edited.
[Back to current version]   [Restore this version]


Code Coverage - How To for integrating Emma with AppFuse 1.8.1 ant build. If you'd prefer to use Cobertura, please see the AppFuse + Cobertura HowTo. If you're trying to decide if EMMA or Cobertura is a better solution, you may want to check out opinion's expressed on Matt's Cobertura vs. EMMA blog entry.
  • Add emma library to Appfuse :
    • create directory lib/emma-2.0.5312 and put emma.jar and emma_ant.jar in it;
    • add the following in lib.properties
  #
  # Emma http://emma.sourceforge.net
  #
  emma.version=2.0.5312
  emma.dir=${lib.dir}/emma-${emma.version}
  emma.jar=${lib.dir}/emma-${emma.version}/emma.jar
  • Add emma taskdef into the properties.xml
  <path id="emma.libs" >
       <pathelement location="${emma.dir}/emma.jar" />
       <pathelement location="${emma.dir}/emma_ant.jar" />
  </path>

  <path id="all.sources" >
       <pathelement path="src/dao" />
       <pathelement path="src/service" />
       <pathelement path="src/web" />
   </path>

  <taskdef resource="emma_ant.properties" classpathref="emma.libs" />
  • Add the following task in build.xml :
  <target name="emma" description="turns on EMMA instrumentation/reporting" >
       <property name="emma.enabled" value="true" />
       <!-- EMMA coverage data output directory: -->
       <property name="coverage.dir" value="${basedir}/coverage" />
       <!-- EMMA instrumented classes output directory: -->
       <property name="out.instr.dir" value="${basedir}/outinstr" />
       <mkdir dir="${out.instr.dir}" />
       <!-- Filter for classes to instrument -->
       <property name="emma.filter" value="org.appfuse.*" />
  </target>
  • Now we'll modify existing ant tasks :
  • clean : add somewhere in task
    <delete dir="${out.instr.dir}"/>
    <delete dir="${coverage.dir}"/>
  • prepare : add to remove instrumented classes
       <delete dir="${out.instr.dir}"/>
  • compile macrodef : add the following after all the javac tasks;
          <emma enabled="${emma.enabled}" >
               <instr instrpath="${build.dir}/@{module}/classes"
                   destdir="${out.instr.dir}/@{module}/classes"
               metadatafile="${coverage.dir}/metadata-@{module}.emma"
               merge="true"
               />
           </emma>
  • test-module macrodef : below is modified test-module macrodef :
    <macrodef name="test-module">
       <attribute name="module"/>
       <attribute name="additional.src.dirs" default=""/>
       <element name="options" optional="yes"/>

       <sequential>
           <echo level="info">Testing @{module}...</echo>
           <mkdir dir="${test.dir}/data"/>
           <propertycopy name="testcase" from="@{module}-testcase" silent="true"/>
           <!-- Replace tokens in test properties files -->
           <copy todir="${test.dir}/@{module}/classes">
               <fileset dir="test/@{module}" excludes="**/*.java"/>
               <filterset refid="variables.to.replace"/>
           </copy>

         <junit printsummary="no" forkmode="once" errorProperty="test.failed"
               failureProperty="test.failed" fork="${junit.fork}">
               <classpath>
                   <!-- Instrumented classes - must be BEFORE the normal ones -->
                   <!-- ============================= -->
                   <pathelement location="${out.instr.dir}/@{module}/classes" />

                   <pathelement path="@{additional.src.dirs}"/>
                   <path refid="@{module}.test.classpath"/>
                   <pathelement location="${build.dir}/@{module}/classes"/>
                   <pathelement location="${test.dir}/@{module}/classes"/>
                   <pathelement path="${java.class.path}"/>
                   <pathelement path="${webapp.target}"/>
                   <pathelement path="${build.dir}/dao/gen"/>
                   <!-- For .properties and .xml files -->
                   <pathelement path="${build.dir}/web/classes"/>
                   <pathelement path="src/@{module}"/>

                   <!-- Emma library -->
                   <!-- ============================= -->
                   <pathelement location="${emma.jar}" />

               </classpath>

               <!-- emma-specific jvm parameter -->
               <!-- =========================== -->
               <jvmarg value="-Demma.coverage.out.file=${coverage.dir}/coverage-@{module}.emma" />

               <formatter type="xml"/>
               <formatter type="brief" usefile="false"/>
               <batchtest todir="${test.dir}/data" if="testcase">
                   <fileset dir="${test.dir}/@{module}/classes">
                       <include name="**/*${testcase}*"/>
                       <exclude name="**/*TestCase.class"/>
                   </fileset>
               </batchtest>
               <batchtest todir="${test.dir}/data" unless="testcase">
                   <fileset dir="${test.dir}/@{module}/classes">
                       <include name="**/*Test.class*"/>
                   </fileset>
               </batchtest>
           </junit>

           <!-- emma report task -->
           <!-- ================================== -->
           <emma enabled="${emma.enabled}" >
               <report sourcepathref="all.sources" depth="method" >
                   <fileset dir="${coverage.dir}" >
                       <include name="*.emma" />
                   </fileset>
                   <html outfile="${coverage.dir}/coverage.html" />
               </report>
           </emma>

       <fail if="test.failed">
             Unit tests failed. For error messages, check the log files in
             ${test.dir}/data or run "ant test-reports"
             to generate reports at ${test.dir}/reports.</fail>
       </sequential>
   </macrodef>
  • That's all! Run ant emma test-all and after the tests run you should get code coverage report for all your classes in coverage/coverage.html.


Go to top   More info...   Attach file...
This particular version was published on 06-Nov-2006 13:52:49 MST by MattRaible.