代码之家  ›  专栏  ›  技术社区  ›  Rob Spieldenner

如何使用Ant生成Emma代码覆盖率报告?

  •  26
  • Rob Spieldenner  · 技术社区  · 16 年前

    如何设置要生成的Ant任务 Emma 代码覆盖率报告?

    3 回复  |  直到 14 年前
        1
  •  14
  •   Rob Spieldenner    15 年前

    要回答有关源目录和检测目录的位置的问题(可以将这些目录切换到标准目录结构的任何目录):

    <property file="build.properties" />
    <property name="source" location="src/main/java" />
    <property name="test.source" location="src/test/java" />
    <property name="target.dir" location="target" />
    <property name="target" location="${target.dir}/classes" />
    <property name="test.target" location="${target.dir}/test-classes" />
    <property name="instr.target" location="${target.dir}/instr-classes" />
    

    Classpaths:

    <path id="compile.classpath">
      <fileset dir="lib/main">
        <include name="*.jar" />
      </fileset>
    </path>
    
    <path id="test.compile.classpath">
      <path refid="compile.classpath" />
      <pathelement location="lib/test/junit-4.6.jar" />
      <pathelement location="${target}" />
    </path>
    
    <path id="junit.classpath">
      <path refid="test.compile.classpath" />
      <pathelement location="${test.target}" />
    </path>
    

    首先,您需要设置Ant可以在哪里找到Emma库:

    <path id="emma.lib" >
        <pathelement location="${emma.dir}/emma.jar" />
        <pathelement location="${emma.dir}/emma_ant.jar" />
    </path>
    

    然后导入任务:

    <taskdef resource="emma_ant.properties" classpathref="emma.lib" />
    

    然后检测代码:

    <target name="coverage.instrumentation">
        <mkdir dir="${instr.target}"/>
        <mkdir dir="${coverage}"/>
        <emma>
            <instr instrpath="${target}" destdir="${instr.target}" metadatafile="${coverage}/metadata.emma" mode="copy">
                <filter excludes="*Test*"/>
            </instr>
        </emma>
        <!-- Update the that will run the instrumented code -->
        <path id="test.classpath">
            <pathelement location="${instr.target}"/>
            <path refid="junit.classpath"/>
            <pathelement location="${emma.dir}/emma.jar"/>
        </path>
    </target>
    

    然后使用适当的vm参数运行目标,例如:

    <jvmarg value="-Demma.coverage.out.file=${coverage}/coverage.emma" />
    <jvmarg value="-Demma.coverage.out.merge=true" />
    

    最后生成报告:

    <target name="coverage.report" depends="coverage.instrumentation">
        <emma>
            <report sourcepath="${source}" depth="method">
                <fileset dir="${coverage}" >
                    <include name="*.emma" />
                </fileset>
                <html outfile="${coverage}/coverage.html" />
            </report>
        </emma>
    </target>
    
        2
  •  2
  •   matt b    16 年前

    这个 User Guide has a good example of how to set up your build script 这样不仅可以将插入指令的代码与执行分离,而且还可以将其包含在同一个代码中 <target> 这样你就不必运行一系列不同的目标,而是可以做一些类似的事情 ant emma tests (如果 ant tests 例如,您通常是如何运行单元测试的)。

    以下是他们的例子:

    <target name="emma" description="turns on EMMA instrumentation/reporting" >
        <property name="emma.enabled" value="true" />
        <!-- EMMA instr class output directory: -->
        <property name="out.instr.dir" value="${basedir}/outinstr" />
        <mkdir dir="${out.instr.dir}" />
    </target>
    
    <target name="run" depends="init, compile" description="runs the examples" >
        <emma enabled="${emma.enabled}" >
          <instr instrpathref="run.classpath"
                 destdir="${out.instr.dir}" 
                 metadatafile="${coverage.dir}/metadata.emma"
                 merge="true"
          />
        </emma>
    
        <!-- note from matt b: you could just as easily have a <junit> task here! -->
        <java classname="Main" fork="true" >
          <classpath>
           <pathelement location="${out.instr.dir}" />
            <path refid="run.classpath" />
            <path refid="emma.lib" />
          </classpath> 
          <jvmarg value="-Demma.coverage.out.file=${coverage.dir}/coverage.emma" />
          <jvmarg value="-Demma.coverage.out.merge=true" />
        </java>
    
        <emma enabled="${emma.enabled}" >
          <report sourcepath="${src.dir}" >
            <fileset dir="${coverage.dir}" >
              <include name="*.emma" />
            </fileset>
    
            <txt outfile="${coverage.dir}/coverage.txt" />
            <html outfile="${coverage.dir}/coverage.html" />
          </report>
        </emma>
    </target>
    
        3
  •  0
  •   Community T.Woody    7 年前

    EMMA 2.1介绍了另一种获取运行时覆盖率信息的方法(.ec文件)。可以从运行被检测应用程序的计算机的给定端口远程请求数据。所以没必要阻止虚拟机。

    要获取包含运行时覆盖率数据的文件,需要在运行测试和生成覆盖率报告之间在Ant脚本中插入以下代码段:

    <emma>
        <ctl connect="${emma.rt.host}:${emma.rt.port}" >
            <command name="coverage.get" args="${emma.ec.file}" />
            <command name="coverage.reset" />
        </ctl>
    </emma>
    

    其他步骤与emma 2.0类似。它们在 previous post

    有关EMMA 2.1功能的更多信息: http://sourceforge.net/project/shownotes.php?group_id=108932&release_id=336859

    推荐文章