使用Cobertura统计单元测试覆盖率(下)

发表于:2010-7-27 12:00

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:51Testing软件测试网采编

  现在给出完成的build.xml文件,仅供参考:

<?xml version="1.0" encoding="UTF-8"?>
<project name="study-cobertura" default="coverage" basedir=".">
    <description>The ant file for study-cobertuna</description>
       
    <property name="src.java.dir" value="${basedir}/java" />
    <property name="src.test.dir" value="${basedir}/test" />
    <property name="build.dir" value="${basedir}/build" />
    <property name="src.java.classes.dir" value="${build.dir}/src-java-classes" />
    <property name="src.test.classes.dir" value="${build.dir}/src-test-classes" />
    <property name="instrumented.classes.dir" value="${build.dir}/instrumented-classes" />
    <property name="reports.dir" value="${basedir}/reports" />
    <property name="reports.junit.xml.dir" value="${reports.dir}/junit-xml" />
    <property name="reports.junit.html.dir" value="${reports.dir}/junit-html" />
    <property name="coverage.cobertura.xml.dir" location="${reports.dir}/cobertura-xml"/>
    <property name="coverage.cobertura.summary.dir" location="${reports.dir}/cobertura-summary-xml"/>
    <property name="coverage.cobertura.html.dir" location="${reports.dir}/cobertura-html"/>
    <property name="lib.dir" location="${basedir}/lib"/>

    <path id="lib.classpath">
        <fileset dir="${lib.dir}">
            <include name="**/*.jar"/>
        </fileset>
    </path>
   
    <taskdef classpathref="lib.classpath" resource="tasks.properties" />
   
    <target name="init">
        <mkdir dir="${src.java.classes.dir}"/>
        <mkdir dir="${src.test.classes.dir}"/>
        <mkdir dir="${instrumented.classes.dir}"/>
        <mkdir dir="${reports.dir}"/>
        <mkdir dir="${reports.junit.html.dir}"/>
        <mkdir dir="${reports.junit.xml.dir}"/>
        <mkdir dir="${coverage.cobertura.xml.dir}"/>
        <mkdir dir="${coverage.cobertura.summary.dir}"/>
        <mkdir dir="${coverage.cobertura.html.dir}"/>
    </target>
   
    <target name="compile" depends="init">
        <javac srcdir="${src.java.dir}" destdir="${src.java.classes.dir}" debug="yes">
            <classpath refid="lib.classpath" />
        </javac>
        <javac srcdir="${src.test.dir}" destdir="${src.test.classes.dir}" debug="yes">
            <!-- This is very import to include the src.java.classes.dir here -->
            <classpath location="${src.java.classes.dir}" />
            <classpath refid="lib.classpath" />
        </javac>
    </target>
   
    <target name="instrument">
        <!-- Remove the coverage data file and any old instrumentation classes -->
        <delete file="cobertura.ser" />
        <delete dir="${instrumented.classes.dir}" />
       
        <!-- Instrument the application classes, writing the instrumented classes into ${instrumented.classes.dir} -->
        <cobertura-instrument todir="${instrumented.classes.dir}">
            <!-- The following line causes instrument to ignore any source line containing a reference to log4j, for the purpose of coverage reporting -->
            <ignore regex="org.apache.log4j.*"/>
            <fileset dir="${src.java.classes.dir}">
                <include name="**/*.class"/>
            </fileset>
        </cobertura-instrument>
    </target>
   
    <target name="test" depends="init,compile">
        <junit fork="yes" dir="${basedir}" failureProperty="test.failed">
            <!-- Note: the classpath order: instrumented classes are before the original (uninstrumented) classes. This is important!!! -->
            <classpath location="${instrumented.classes.dir}" />
            <classpath location="${src.java.classes.dir}" />
            <classpath location="${src.test.classes.dir}" />
            <classpath refid="lib.classpath" />
           
            <formatter type="xml"/>
            <test name="${testcase}" todir="${reports.junit.xml.dir}" if="testcase" />
            <batchtest todir="${reports.junit.xml.dir}" unless="testcase">
                <fileset dir="${src.test.dir}">
                    <include name="**/*.java"/>
                </fileset>
            </batchtest>
        </junit>
       
        <junitreport todir="${reports.junit.xml.dir}">
            <fileset dir="${reports.junit.xml.dir}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report format="frames" todir="${reports.junit.html.dir}"/>
        </junitreport>
    </target>
   
    <target name="coverage-check">
        <cobertura-check branchrate="34" totallinerate="100" />
    </target>

31/3123>
《2023软件测试行业现状调查报告》独家发布~

精彩评论

  • yshana
    2013-7-24 17:37:36

    [taskdef] Could not load definitions from resource tasks.properties. It could not be found.
    tasks.properties.是什么文件

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号