使用Cobertura进行覆盖测试应注意的几个细节

使用Cobertura中,编写build.xml时,请注意如下两个细节:
1.
<javac debug="yes" srcdir="${source.dir}" destdir="${classes.dir}" classpathref="build.classpath" />
如上代码所示,请增加 debug="yes"
如果无此属性,则在生成的覆盖报告中,所有类的 Line Coverage是N/A。同时注意观察Ant的构建输出,提示"[cobertura-instrument] WARN   visitEnd, No line number information found for class cn.com.sungole.medapp.Web.App.  Perhaps you need to compile with debug=true?">。

2.
<classpath location="${instrumented.dir}" />
<classpath>
	<path refid="build.classpath"></path>
	<pathelement location="${test.classes.dir}" />
	<pathelement location="${classes.dir}" />
</classpath>

请在junit中注意添加“ <classpath location="${instrumented.dir}" />”,并注意与下边classpath的顺序,不能颠倒。
如果无此节点,则生成的覆盖报告中,所有类的 Line Coverage均为0%。
同时,Ant的构建正确输出中
    [junit] Running cn.com.sungole.medapp.domain.test.BillTest
    [junit] Tests run: 4, Failures: 0, Errors: 0, Time elapsed: 2.593 sec
    [junit] Cobertura: Loaded information on 10 classes.
    [junit] Cobertura: Saved information on 10 classes.
将无最后两行输出信息。

完整的配置示例:

<?xml version="1.0"?>
<project name="coberturaSample1">
	<property name="default.target.dir" value="target" />
	<property name="classes.dir" value="${default.target.dir}/classes" />
	<property name="test.classes.dir" value="${default.target.dir}/test-classes" />
	<property name="test.report.dir" value="${default.target.dir}/test-reports" />
	<property name="lib.dir" value="${basedir}/lib" />
	<property name="javadoc.dir" value="${default.target.dir}/apidocs" />

	<property name="source.dir" value="src" />
	<property name="test.source.dir" value="test" />

	<property name="test.pattern" value="**/**Test.java" />

	<!-- Coverage reports are deposited into these directories -->
	<property name="cobertura.dir" value="${default.target.dir}/cobertrua" />
	<!-- Instrumented classes are deposited into this directory -->
	<property name="instrumented.dir" value="instrumented" />

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

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

	<target name="clean">
		<delete dir="${classes.dir}" quiet="true" />
		<delete dir="${test.classes.dir}" />
		<delete dir="${default.target.dir}" />
	</target>

	<target name="init" depends="clean">
		<mkdir dir="${classes.dir}" />
		<mkdir dir="${test.classes.dir}" />
		<mkdir dir="${javadoc.dir}" />
		<mkdir dir="${default.target.dir}" />
		<mkdir dir="${instrumented.dir}" />
		<path id="build.classpath">
			<fileset dir="${lib.dir}">
				<include name="**/*.jar" />
			</fileset>
			<fileset dir="${default.target.dir}">
				<include name="**/*.jar" />
			</fileset>
		</path>
	</target>

	<target name="compile-source" depends="init" description="compiles all .java files in source directory">
		<javac debug="yes" srcdir="${source.dir}" destdir="${classes.dir}" classpathref="build.classpath" />
	</target>

	<target name="instrument" depends="compile-source">
		<delete file="cobertura.ser" />
		<delete file="${instrumented.dir}" />

		<!--Instrument the application classes, writing the instrumented classes into ${build.instrumented.dir}.-->
		<cobertura-instrument todir="${instrumented.dir}">
			<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="jar" depends="instrument" description="生成jar 文件。Comppresstion .jar file">
		<jar jarfile="${default.target.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
		</jar>
	</target>

	<target name="complie-tests" depends="jar" description="compiles all .java files in test directory ">
		<javac debug="yes" srcdir="${test.source.dir}" destdir="${test.classes.dir}" classpathref="build.classpath">
		</javac>
	</target>

	<target name="javadoc" depends="init">
		<javadoc author="true" use="true" version="true" charset="gbk" sourcepath="${source.dir}" classpathref="build.classpath" destdir="${javadoc.dir}">
		</javadoc>
	</target>

	<target name="test" depends="complie-tests" description="runs JUnit tests">
		<mkdir dir="${test.report.dir}" />

		<copy file="src/hibernate.cfg.xml" todir="target/classes" />
		<copy file="src/log4j.properties" todir="target/classes" />
		<copy todir="target/classes">
			<fileset dir="src" />
			<globmapper from="*.hbm.xml" to="*.hbm.xml" />
		</copy>

		<junit haltonfailure="no" printsummary="on" fork="yes" dir="${basedir}">
			<sysproperty key="basedir" value="${basedir}"/>
			<formatter type="xml"/>
			
			<!--
				Note the classpath order: instrumented classes are before the
				original (uninstrumented) classes.  This is important.
				如果缺少此行,则在覆盖率报告中,所有的类会出现"Line Coverage 为 0%"的现象
			-->			
			<classpath location="${instrumented.dir}" />
			
			<classpath>
				<path refid="build.classpath"></path>
				<pathelement location="${test.classes.dir}" />
				<pathelement location="${classes.dir}" />
			</classpath>
			
			<formatter type="xml" />
			<batchtest todir="${test.report.dir}">
                <fileset dir="${test.source.dir}">  
                    <include name="${test.pattern}" />  
                </fileset>
			</batchtest>
		</junit>
	</target>
	
	<target name="converage-check">
		<cobertura-check branchrate="40" totallinerate="100" />
	</target>
	
	<target name="converage-report">
		<cobertura-report srcdir="${source.dir}" destdir="${cobertura.dir}" format="html"/>
	</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="${cobertura.dir}">
			<fileset dir="${source.dir}">
				<include name="**/*.java"/>
			</fileset>
		</cobertura-report>
	</target>
	
	<target name="coverage" depends="jar,instrument,test,converage-report,alternate-coverage-report"></target>
	
	<target name="all" depends="coverage,javadoc" />
</project>

你可能感兴趣的:(html,xml,Web,ant,JUnit)