2013-04-18 50 views
1

我有以下项目结构:没有运行时使用的覆盖艾玛,JUnit和蚂蚁

  • 的src/COM /虚拟/ abc.java
  • src_tests /来/虚拟/ abcTest.java
  • 构建.xml

我需要检查使用emma进行的测试所做的代码覆盖率。 从阅读艾玛+ junit的例子我得出一个结论,要获得一个报告,我需要做到以下几点:

  1. 编译“SRC”
  2. 编译“src_tests”
  3. 仪编译“src_tests” >“instrumented_src_tests”上“instrumented_src_tests”
  4. 运行JUnit用额外jvmarg

的问题是,在步骤4中应返回某种然后与“代表所使用的文件的ort'命令应该创建一个报告。我越来越

emma-report: 
    [report] processing input files ... 
    [report] 1 file(s) read and merged in 67 ms 
    [report] nothing to do: no runtime coverage data found in any of the data files 

〜编辑 我附加了我的build.xml

<?xml version="1.0" encoding="UTF-8"?> 

<project name="HELL scream" default="all" basedir="."> 
    <property name="build.sources.dir" location="${basedir}/src"/> 
    <property name="build.sources.des" location="${basedir}/bin/classes"/> 
    <property name="test.sources.dir" location="${basedir}/src_test"/> 
    <property name="test.sources.des" location="${basedir}/bin/classes_test"/> 
    <property name="test.reports.des" location="${basedir}/reports-junit"/> 
    <property name="emma.sources.des" location="${basedir}/bin/classes_emma"/> 
    <property name="emma.reports.des" location="${basedir}/reports-emma"/> 
    <property name="emma.final.reports.des" location="${basedir}/reports-emma/final"/> 
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> 
    <path id="emma.lib" > 
     <fileset dir="/home/user1/Desktop/emma-2.0.5312/lib"> 
      <include name="*.jar"/> 
     </fileset> 
    </path> 

    <taskdef resource="emma_ant.properties" classpathref="emma.lib" /> 
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> 
    <target name="clean-all"> 
     <delete failonerror="false"> 
      <fileset dir="${emma.final.reports.des}"/> 
      <fileset dir="${emma.reports.des}"/> 
      <fileset dir="${emma.sources.des}"/> 
      <fileset dir="${test.reports.des}"/> 
      <fileset dir="${test.sources.des}"/> 
      <fileset dir="${build.sources.des}"/> 
     </delete> 
    </target> 
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> 
    <target name="compile-sources"> 
     <mkdir dir="${build.sources.des}"/> 
     <javac srcdir="${build.sources.dir}" includes="" excludes="" destdir="${build.sources.des}" listfiles="true" debug="true" includeantruntime="false"/> 
    </target> 
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> 
    <target name="compile-tests"> 
     <mkdir dir="${test.sources.des}"/> 
     <javac srcdir="${test.sources.dir}" includes="" excludes="" destdir="${test.sources.des}" listfiles="true" debug="true" includeantruntime="false"> 
      <classpath> 
       <pathelement location="/home/user1/Desktop/junit-4.10.jar"/> 
       <pathelement location="${build.sources.des}"/> 
      </classpath> 
     </javac> 
    </target> 
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> 
    <target name="compile-emma-tests"> 
     <emma enabled="true" > 
      <instr instrpath="${test.sources.des}" destdir="${emma.sources.des}" metadatafile ="${emma.reports.des}/instrumentation.emma" merge ="true"/> 
     </emma> 
    </target> 
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> 
    <target name="run-tests"> 
     <mkdir dir="${test.reports.des}"/> 
     <junit haltonfailure="no" showoutput="yes" printsummary="true">    
      <formatter type="plain" usefile="false" /> 
      <formatter type="xml"/> 
      <classpath> 
       <pathelement location="/home/user1/Desktop/junit-4.10.jar"/> 
       <pathelement location="${build.sources.des}"/> 
       <pathelement location="${emma.sources.des}"/>     
       <path refid="emma.lib" /> 
      </classpath> 

      <batchtest todir="${test.reports.des}" fork="true"> 
       <fileset dir="${emma.sources.des}"/> 
      </batchtest>      

      <jvmarg value="-Demma.coverage.out.file=${emma.reports.des}/coverage.emma" /> 
      <jvmarg value="-Demma.coverage.out.merge=false" /> 
     </junit>    
    </target> 
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> 
    <target name="junit-tests-report"> 
     <junitreport todir="${test.reports.des}"> 
      <fileset dir="${test.reports.des}"> 
       <include name="TEST-*.xml"/> 
      </fileset> 

      <report format="frames" todir="${test.reports.des}/junit_reports"/> 
     </junitreport> 
    </target> 
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> 
    <target name="emma-tests-report"> 
     <emma enabled="true" > 
      <report sourcepath="${build.sources.dir}">  
       <fileset dir="${emma.reports.des}" > 
        <include name="*.emma" /> 
       </fileset> 

       <txt outfile="${emma.final.reports.des}/coverage.txt" depth="package" columns="class,method,block,line,name" /> 
       <xml outfile="${emma.final.reports.des}/coverage.xml" depth="package" /> 
       <html outfile="${emma.final.reports.des}/coverage.html" depth="method" columns="name,class,method,block,line" /> 
      </report> 
     </emma> 
    </target> 
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> 
    <target name="all" depends="clean-all, compile-sources, compile-tests, compile-emma-tests, run-tests, junit-tests-report, emma-tests-report"/> 
</project> 

它可能是一些小事...

此外,当使用emma.sources.dest 我得到这个在我的(唯一)测试

run-tests: 
    [junit] Running com.emma.test.MathTest 
    [junit] Testsuite: com.emma.test.MathTest 
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec 
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec 
    [junit] 
    [junit]  Caused an ERROR 
    [junit] Illegal local variable table length 5 in method com.emma.test.MathTest.<init>()V 
    [junit] java.lang.ClassFormatError: Illegal local variable table length 5 in method com.emma.test.MathTest.<init>()V 
    [junit]  at java.lang.Class.forName0(Native Method) 
    [junit]  at java.lang.Class.forName(Class.java:188) 
    [junit] 
    [junit] Test com.emma.test.MathTest FAILED 

〜已解决 补充一点:

<jvmarg value="-XX:-UseSplitVerifier"/> 
<jvmarg value="-Demma.coverage.out.file=${emma.reports.des}/coverage.emma" /> 
<jvmarg value="-Demma.coverage.out.merge=false" /> 
+0

您是否按照以下步骤设置了emma与ant http:/ /emma.sourceforge.net/userguide/ar01s03.html –

+0

是的。 (还有很多)。此外,位于该链接中的信息并不能说明如何将它与junit一起使用,而是纯Java。 – Twisted

回答

3

艾玛一直没有自2005年以来的稳定版本,并且不与的JDK的新版本打好。自从2011以来,Cobertura没有做任何新的开发。因此,我不再建议开发人员使用Emma或Cobertura。

我已经切换到JaCoCo代码覆盖。它由艾玛Eclipse团队启动,正在积极开发中。

正如JaCoCo的mission statement所述。

[...]两种最好的和广泛使用的开源工具是EMMA和Cobertura。原始作者不再主动维护这两种工具,也不支持当前的Java版本。由于缺乏回归测试,维护和功能添加很困难。

因此,我们启动了JaCoCo项目,以便在基于Java VM的环境中为代码覆盖率分析提供新的标准技术。 [...]

JaCoCo的好处是它可以在各种各样的环境中工作,并且编译后不需要对代码进行检测。仪器随着测试的执行而发生。它看起来是这样的:

<jacoco:coverage destfile="${target.dir}/jacoco.exec"> 
    <junit> 
     [...] 
    </junit> 
</jacoco> 

您再使用<jacoco:coverage>打印覆盖率报告。

你必须使用艾玛吗?如果不是的话,那么使用JaCoCo可能会让所有工作都变得更好。

1

艾玛依靠字节码注入(如果内存很好地服务于我);但是,对于Java 7,由于JVM现在具有调试接口,因此代码尚未更新。

JaCoCo通过附加到调试接口来测试代码覆盖范围,并在您的代码输入方法并侦听字节代码时通过侦听来进行代码覆盖,而无需注入检查点。这显然是做事情的最好方式(现在它存在),我怀疑Emma会完成支持Java 7的工作。

我建议您“升级”JaCoCo。

0

当您使用错误的命令生成报告时,会出现此错误。 大部分在线教程都提倡错误的(可能是老的)命令,即使我在使用下面的命令生成报告时遇到了此错误:

{JAVA_HOME} \ jre \ lib \ ext> java -cp emma .jar emma报告-r html -in coverage.em,{ALFRESCO_HOME} \ coverage.ec EMMA:处理输入文件... EMMA:1个文件在60 ms内读取和合并 EMMA:无所事事:没有任何数据文件

发现运行覆盖数据

命令的正确用法是: {JAVA_HOME} \ JRE \ lib中\分机> java命令emma.jar艾玛报告-r TXT,HTML -in { JAVA_HOME} \ jre \ lib \ ext \ coverage.em -in C:\ t1_tempSetup \ A lfresco \ coverage.ec EMMA:处理输入文件... EMMA:2个文件在70 ms内读取和合并 EMMA:将[txt]报告写入[{JAVA_HOME} \ jre \ lib \ ext \ coverage .txt] ... EMMA:将[html]报告写入[{JAVA_HOME} \ jre \ lib \ ext \ coverage \ index.html] ...