java - 没有使用 emma、junit 和 ant 的运行时覆盖

标签 java ant junit emma

我有以下项目结构:

  • src/com/dummy/abc.java
  • src_tests/come/dummy/abcTest.java
  • build.xml

我需要通过使用 emma 的测试来检查我的代码的覆盖率。 通过阅读 emma + junit 示例,我得出一个结论,即要获得一份报告,我需要执行以下操作:

  1. 编译'src'
  2. 编译'src_tests'
  3. 仪器编译 'src_tests' => 'instrumented_src_tests'
  4. 使用额外的 jvmarg 在“instrumented_src_tests”上运行 junit

问题是第 4 步应该返回某种文件,然后与“报告”命令一起使用应该创建一个报告。我得到了

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" />

最佳答案

Emma 自 2005 年以来就没有稳定的版本,并且不能很好地与新版本的 JDK 配合使用。自 2011 以来,Cobertura 没有进行任何新开发。 . 因此,我不再建议开发人员使用 Emma 或 Cobertura。

我已经切换到 JaCoCo用于代码覆盖。它由 Emma Eclipse 团队发起,目前正在积极开发中。

如 JaCoCo 的 mission statement 所述.

[...]Two of the best and widely used available open source tools are EMMA and Cobertura. Both tools are not actively maintained by the original authors any more and do not support the current Java versions. Due to the lack of regression tests maintenance and feature additions is difficult.

Therefore we started the JaCoCo project to provide a new standard technology for code coverage analysis in Java VM based environments. [...]

JaCoCo 的优点在于它可以在各种环境中工作,而且您不必在编译后检测您的代码。在执行测试时进行检测。它看起来像这样:

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

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

你一定要用艾玛吗?如果没有,您可能会更幸运地使用 JaCoCo 使一切正常工作。

关于java - 没有使用 emma、junit 和 ant 的运行时覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16078776/

相关文章:

java - 是否在列表上运行 remove(int index) 方法以删除对象实例调用垃圾收集?

JAVA PHP加密解密

java - Tomcat 中的 Web 应用程序 : reading from a DB works but nothing gets persisted?

java - 匹配节点的图形算法

linux - ant sshexec 任务在执行命令返回时或之前返回吗?

java - 从 yml 加载属性文件作为 JUNIT 中的 Factory Bean

android - 在单个 Android 项目中同时进行 JUnit 和 Cucumber 测试

java - jar 文件的 ANT 问题

java - 自定义 JUnit 报告?

junit - 将 JMeter 报告转换为 JUnit 报告