51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 3331|回复: 1
打印 上一主题 下一主题

myeclipse+ant+cobertura自动生成报告问题

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2011-12-20 17:10:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
环境配置:
myeclipse版本8.6
ant1.7.1
junit4.9
cobertura1.9.4.1
log4j1.2.16

练习写了一个单元测试。ant运行成功。自动生成的报告中junit报告显示运行失败(而通过junit运行,测试是成功的)
cobertura报告显示覆盖率为0,实际已经达到100%

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

<project name="cobertura.examples.basic" default="coverage" basedir=".">

    <description>
    Cobertura - http://cobertura.sourceforge.net/
    Copyright (C) 2003 jcoverage ltd.
    Copyright (C) 2005 Mark Doliner &lt;thekingant@users.sourceforge.net&gt;
    Copyright (C) 2006 Dan Godfrey
    Cobertura is licensed under the GNU General Public License
    Cobertura comes with ABSOLUTELY NO WARRANTY
    </description>

    <property file="build.properties" />

    <path id="cobertura.classpath">
        <fileset dir="${cobertura.dir}">
            <include name="cobertura.jar" />
            <include name="lib/**/*.jar" />
        </fileset>
    </path>

    <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>

    <target name="init">
        <mkdir dir="${classes.dir}" />
        <mkdir dir="${instrumented.dir}" />
        <mkdir dir="${reports.xml.dir}" />
        <mkdir dir="${reports.html.dir}" />
        <mkdir dir="${coverage.xml.dir}" />
        <mkdir dir="${coverage.summaryxml.dir}" />
        <mkdir dir="${coverage.html.dir}" />
    </target>

    <target name="compile" depends="init">
        <javac srcdir="${src.dir}{test.dir}" destdir="${classes.dir}"  fork="true" debug="yes">
            <classpath refid="cobertura.classpath" />
        </javac>
    </target>

    <target name="instrument" depends="init,compile">
        <!--
            Remove the coverage data file and any old instrumentation.
        -->
        <delete file="cobertura.ser"/>
        <delete dir="${instrumented.dir}" />

        <!--
            Instrument the application classes, writing the
            instrumented classes into ${build.instrumented.dir}.
        -->
        <cobertura-instrument todir="${instrumented.dir}">
            <!--
                The following line causes instrument to ignore any
                source line containing a reference to log4j, for the
                purposes of coverage reporting.
            -->
            <ignore regex="org.apache.log4j.*" />

            <fileset dir="${classes.dir}">
                <!--
                    Instrument all the application classes, but
                    don't instrument the test classes.
                -->
                <include name="**/*.class" />
                <exclude name="**/*Test.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.dir}" />
            <classpath location="${classes.dir}" />

            <!--
                The instrumented classes reference classes used by the
                Cobertura runtime, so Cobertura and its dependencies
                must be on your classpath.
            -->
            <classpath refid="cobertura.classpath" />

            <formatter type="xml" />
            <test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
            <batchtest todir="${reports.xml.dir}" unless="testcase">
                <fileset dir="${src.dir}">
                    <include name="**/*Test.java" />
                </fileset>
            </batchtest>
        </junit>

        <junitreport todir="${reports.xml.dir}">
            <fileset dir="${reports.xml.dir}">
                <include name="TEST-*.xml" />
            </fileset>
            <report format="frames" todir="${reports.html.dir}" />
        </junitreport>
    </target>

    <target name="coverage-check">
        <cobertura-check branchrate="34" totallinerate="100" />
    </target>

    <target name="coverage-report">
        <!--
            Generate an XML file containing the coverage data using
            the "srcdir" attribute.
        -->
        <cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />
    </target>

    <target name="summary-coverage-report">
        <!--
            Generate an summary XML file containing the coverage data using
            the "srcdir" attribute.
        -->
        <cobertura-report srcdir="${src.dir}" destdir="${coverage.summaryxml.dir}" format="summaryXml" />
    </target>

    <target name="alternate-coverage-report">
        <!--
            Generate a series of HTML files containing the coverage
            data in a user-readable form using nested source filesets.
        -->
        <cobertura-report destdir="${coverage.html.dir}">
            <fileset dir="${src.dir}">
                <include name="**/*.java"/>
            </fileset>
        </cobertura-report>
    </target>

    <target name="clean" description="Remove all files created by the build/test process.">
        <delete dir="${classes.dir}" />
        <delete dir="${instrumented.dir}" />
        <delete dir="${reports.dir}" />
        <delete file="cobertura.log" />
        <delete file="cobertura.ser" />
    </target>

    <target name="coverage" depends="compile,instrument,test,coverage-report,summary-coverage-report,alternate-coverage-report" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports."/>

</project>


build.properties
# The source code for the examples can be found in this directory
src.dir=src
test.dir=test
# The path to cobertura.jar
cobertura.dir=../..

# Classes generated by the javac compiler are deposited in this directory
classes.dir=classes

# Instrumented classes are deposited into this directory
instrumented.dir=instrumented

# All reports go into this directory
reports.dir=reports

# Unit test reports from JUnit are deposited into this directory
reports.xml.dir=${reports.dir}/junit-xml
reports.html.dir=${reports.dir}/junit-html

# Coverage reports are deposited into these directories
coverage.xml.dir=${reports.dir}/cobertura-xml
coverage.summaryxml.dir=${reports.dir}/cobertura-summary-xml
coverage.html.dir=${reports.dir}/cobertura-html


被测试类
package com.ooobgy.cober;

public class Count {
    /**
     * return x/y
     * @param x被除数
     * @param y除数
     * @return 商
     */
    public double div(double x,double y){
        if(y==0){
            throw new IllegalArgumentException("除0错误");
        }
        return x/y;
    }

}


测试类
package com.ooobgy.cober;

import static org.junit.Assert.*;

import org.junit.Test;

public class CountTest {

    @Test
    public void testDiv() {
//      assertTrue(true);//1.do nothing  
        
        Count counter = new Count();         
         
        //3.all  
        assertEquals(3.0/1.2, counter.div(3.0, 1.2),0.01);  
        try {  
              
            counter.div(3.0, 0.0);  
            fail();  
        } catch (Exception e) {  
            assertEquals(e.getClass(), IllegalArgumentException.class);  
            assertEquals(e.getMessage(), "除0错误");  
        }  
    }
}

那位高手帮着看下问题,谢谢
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
 楼主| 发表于 2011-12-22 10:29:04 | 只看该作者
有没有大侠帮助的
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-11-7 05:25 , Processed in 0.092409 second(s), 28 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表