2012-04-10 50 views
1

我是使用Emma的新手。我正在尝试为EAR项目中的模块的JUnit测试用例添加emma ant任务。我在这里没有几个问题。在build.xml中使用Emma for JUnit

  • 我应该使用仪器类来打包我的EAR projet吗?
  • 为junit添加emma ant任务的好方法是什么?我应该使用emmarun:即时模式还是离线模式?对于JUnit,我应该使用fork还是不使用fork?

我正在使用Emma离线模式和Junit分叉。这里是我的build.xml

<!--Target and task for EMMA --> 
<taskdef resource="emma_ant.properties" classpathref="Emma.libraryclasspath" /> 
<target name="emma" description="turns on EMMA's instrumentation/reporting" > 
    <property name="emma.enabled" value="true" /> 
    <mkdir dir="${out.instr.dir}" /> 
    <property name="emma.filter" value="" /> 
</target> 

<target name="test" depends="init, compile" description="Run JUnit Test cases under emma environment"> 
    <!-- Emma instrumentation --> 
    <emma enabled="${emma.enabled}" verbosity="verbose"> 
     <instr instrpath="${class.dir}" 
        destdir="${out.instr.dir}"   
        metadatafile="${coverage.dir}/metadata.em" 
        merge="true" 
        mode="copy"> 
      <filter value="${emma.filter}" /> 
     </instr> 
    </emma> 

    <!-- JUnit Start --> 
    <junit printsummary="yes" fork="yes"> 
     <test name="com.hf.platform.authorizer.WebTxnAuthorizerTest" todir="${test.report.dir}"> 
      <formatter type="xml"/> 
     </test> 
     <classpath> 
      <path refid="HFPlatformWeb.classpath"/> 
      <path refid="Emma.libraryclasspath"/> 
     </classpath> 
     <jvmarg value="-Demma.coverage.out.file=${coverage.dir}/coverage.ec" /> 
     <jvmarg value="-Demma.coverage.out.merge=false" /> 
    </junit> 
    <!-- Junit End --> 

    <emma enabled="${emma.enabled}" verbosity="verbose"> 
     <report> 
      <sourcepath> 
       <dirset dir="${basedir}"> 
        <include name="src"/> 
        <include name="test-src"/> 
       </dirset> 
      </sourcepath> 
      <fileset dir="${coverage.dir}"> 
       <include name="*.em"/> 
       <include name="*.ec"/> 
      </fileset> 
     <xml outfile="${coverage.report.dir}/report.xml" /> 
     <txt outfile="${coverage.report.dir}/report.txt" /> 
     <html outfile="${coverage.report.dir}/report.html" /> 
     </report> 
    </emma> 

</target> 

当我运行一个测试,它不会产生任何报告。但是当我用EclEmma运行相同的单元测试时,它会给出正确的输出。

+0

eclEmma使用[jacoco](http://www.eclemma.org/jacoco/index.html),这是一个有些从不接近到代码覆盖,其中也有蚂蚁的任务。 – oers 2012-04-12 12:08:58

回答

2

在上面的例子中,我们需要确保以下两点

  1. 为metadatafile和覆盖报告文件中的文件路径是.ec,.EM或.emma文件应该是绝对或相对项目。 例如
  2. 对于夹在检测和报告任务之间运行的java/junit任务,它必须使用检测到的类文件路径。 例如

    <classpath> <pathelement location="${out.instr.dir}" /> <path refid="Emma.libraryclasspath"/> <path refid="HFPlatformEJB.classpath"/> </classpath>