ant构建Junit测试并输出测试报告

1.build.xml

<?xml version="1.0"?>
<project name="project" default="test">
    <property name="basedir" value="."></property>
    <property name="build" value="dist"></property>
    <property name="src" value="src"></property>
    <property name="src-test" value="src-test"></property>
    <property name="lib" value="${basedir}/libs"></property>
    <property name="testReport" value="${basedir}/${build}/testReport"></property>
    <path id="javac-lib">
	<fileset dir="${lib}">
		<include name="*.jar" />
	</fileset>
	
	<!--缺少以下的设置可能出现ClassNotFoundException错误-->
	<pathelement location="${build}"/>
    </path> 
	<target name="init">
	    <mkdir dir="${build}"/>
	</target>
    <target name="compile" depends="init">
       <javac destdir="${build}" debug="true" encoding="UTF-8" includeantruntime="true">
		<src path="src" />
		<classpath refid="javac-lib" />
	</javac>
    </target>    

    <target name="test" depends="compile"> 
	<delete dir="${testReport}" />
	<mkdir dir="${testReport}" />
 	
	<!--执行JUnit测试用例--> 
	<junit printsummary="yes"> 
		<classpath refid="javac-lib"/> 
		<formatter type="xml"/> 
		<batchtest todir="${testReport}"> 
			<fileset dir="${build}"> 
				<include name="**/*Test.class"/> 
			</fileset> 
		</batchtest> 
	</junit> 
	
	<!--生成html的测试报告--> 
	<junitreport todir="${testReport}"> 
		<fileset dir="${testReport}">
			<include name="TEST-*.xml"/> 
		</fileset> 
		<report format="frames" todir="${testReport}"/>
	</junitreport> 

	<!--删除xml的测试结果-->
	<delete dir="${testReport}">
		<include name="*.xml" />
	</delete> 
    </target>
</project>

2.在Eclipse的Junit工程中运行build.xml

工程中的Build的目录如下

3.注意事项

(1)libs目录里面放入junit.jar

(2)如果在Eclipse中运行报错,可以尝试下在cmd命令行的状态下运行ant命令

(3)build文件中的delete这个target是要删除运行后生成的xml格式的测试报告的,如果想要报告可以将该任务删除掉

猜你喜欢

转载自blog.csdn.net/tianxuexuankui/article/details/16811611