ant脚本执行junit测试用例

除了使用java来直接运行junit之外,我们还可以使用ant脚本,结合脚本执行junit用例,并生成测试报告,在进行每日构建等动作时非常有用。

一个完整的例子:

  1. <?xml version="1.0"?>  
  2. <project name="project" default="test">  
  3.     <property name="build" value="bin"></property>  
  4.     <property name="src" value="src"></property>  
  5.     <property name="src-test" value="src-test"></property>  
  6.     <property name="lib" value="../lib"></property>  
  7.     <property name="testReport" value="testReport"></property>  
  8.     <path id="javac-lib">  
  9.     <fileset dir="${lib}">  
  10.         <include name="*.jar" />  
  11.     </fileset>  
  12.       
  13.     <!--缺少以下的设置可能出现ClassNotFoundException错误-->  
  14.     <pathelement location="${build}"/>  
  15.     </path>   
  16.   
  17.     <target name="compile">  
  18.        <javac destdir="${build}" debug="true" encoding="UTF-8">  
  19.         <src path="src" />  
  20.         <classpath refid="javac-lib" />  
  21.     </javac>  
  22.     </target>      
  23.   
  24.     <target name="test" depends="compile">   
  25.     <delete dir="${testReport}" />  
  26.     <mkdir dir="${testReport}" />  
  27.       
  28.     <!--执行JUnit测试用例-->   
  29.     <junit printsummary="yes">   
  30.         <classpath refid="javac-lib"/>   
  31.         <formatter type="xml"/>   
  32.         <batchtest todir="${testReport}">   
  33.             <fileset dir="${build}">   
  34.                 <include name="**/*Test.class"/>   
  35.             </fileset>   
  36.         </batchtest>   
  37.     </junit>   
  38.       
  39.     <!--生成html的测试报告-->   
  40.     <junitreport todir="${testReport}">   
  41.         <fileset dir="${testReport}">  
  42.             <include name="TEST-*.xml"/>   
  43.         </fileset>   
  44.         <report format="frames" todir="${testReport}"/>  
  45.     </junitreport>   
  46.   
  47.     <!--删除xml的测试结果-->  
  48.     <delete dir="${testReport}">  
  49.         <include name="*.xml" />  
  50.     </delete>   
  51.     </target>  
  52. </project>  
<?xml version="1.0"?>
<project name="project" default="test">
    <property name="build" value="bin"></property>
    <property name="src" value="src"></property>
    <property name="src-test" value="src-test"></property>
    <property name="lib" value="../lib"></property>
    <property name="testReport" value="testReport"></property>
    <path id="javac-lib">
	<fileset dir="${lib}">
		<include name="*.jar" />
	</fileset>
	
	<!--缺少以下的设置可能出现ClassNotFoundException错误-->
	<pathelement location="${build}"/>
    </path> 

    <target name="compile">
       <javac destdir="${build}" debug="true" encoding="UTF-8">
		<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>


以上实例按几个步骤执行:

1、设置了代码路径、编译环境等
2、执行javac编译target
3、执行junit测试target

其中junit target又按几个步骤执行:
1、初始化报告输出目录
2、执行junit用例,'<junit printsummary="yes">'
3、<formatter type="xml"/>生成xml的测试结果
4、batchtest批量执行用例
5、junitreport根据每个xml用例执行结果生成html格式报告
6、删除每个xml用例结果,如果想看生成xml格式结果,可以把这一步屏蔽

PS:
执行过程中发生ClassNotFoundException错误的解决办法,注意在环境中是否加入了你的编译路径,<pathelement location="${build}"/>参考:http://yusudong.iteye.com/blog/1134915

JUnit Task详细的参数以及各参数的描述可以参考官网的说明:http://ant.apache.org/manual/Tasks/junit.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值