jUnit+ant

<?xml version="1.0" encoding="UTF-8"?>
<!--
	1、build.xml放在程序根目录下,跟src并行
	2、新建sorce folder命名为test
	3、test目录下与src目录下对应相同目录,放置测试文件
		比如:src/com/LoginDao
			test/com/LoginDaoTest(测试文件名必须为这种格式)
	4、运行cmd,进入程序根目录,输入ant运行就能生成了
	5、参考资料:
	http://miaoxianjie.iteye.com/blog/1585506
	http://ant.apache.org/manual/index.html
	default:定义初始运行的target
-->
<project basedir="." default="JUnitAllTests" name="TestJUnit4">
	<property environment="env"/>
	<property name="debuglevel" value="source,lines,vars"/>
	<!-- 配置测试报告生成目录 -->
	<property name="junit.output.dir" value="JunitTestResult"/>
    <path id="TestJUnit4.classpath">
        <pathelement location="bin"/>
    </path>  
    <target name="JUnitAllTests">
    		<!-- 删除原路径-->
	 	    <delete dir="${junit.output.dir}"/>
    		<!-- 新建路径-->
	        <mkdir dir="${junit.output.dir}"/>
	        <junit fork="yes" printsummary="withOutAndErr">
	            <formatter type="xml"/>
	        	<!--
	        		(batchtest)
	        		fork:Run the tests in a separate VM. Overrides value set in <junit>(覆盖原值,非必须).
	        		todir:Directory to write the reports to.(写入目录)
	        	-->
	        	<batchtest fork="yes" todir="${junit.output.dir}">
	        			<!-- 测试文件所在路径 -->
	        		    <fileset dir="test">
	        		    	<!-- 匹配测试文件名 -->
	        		      <include name="**/*Test*.java"/>
	        		      <exclude name="**/AllTests.java"/>
	        		    </fileset>
	        		  </batchtest>
	            <classpath refid="TestJUnit4.classpath"/>
	        </junit>
    	<!-- 定位下一个执行target -->
	 	 <antcall target="junitreport"/>
	    </target>
	<!-- 配置jUnit报告属性 -->
    <target name="junitreport">
    	<!-- 目标路径 -->
        <junitreport todir="${junit.output.dir}">
        	<!--
        		fileset:junitreport collects individual xml files generated by the JUnit task using the nested <FileSet> element.
        		(合并.xml生成报告)
        		report:would generate a TESTS-TestSuites.xml file in the directory reports and generate the default framed report in the directory
        	-->
            <fileset dir="${junit.output.dir}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report format="frames" todir="${junit.output.dir}"/>
        </junitreport>
    </target>
</project>


1、This task has been tested with JUnit 3.0 up to JUnit 3.8.2; it won't work with versions prior to JUnit 3.0. It also works with JUnit 4.0, including "pure" JUnit 4 tests using only annotations and noJUnit4TestAdapter.

2、

Note: You must have junit.jar available. You can do one of:

  1. Put both junit.jar and ant-junit.jar in ANT_HOME/lib.
  2. Do not put either in ANT_HOME/lib, and instead include their locations in your CLASSPATH environment variable.
  3. Add both JARs to your classpath using -lib.
  4. Specify the locations of both JARs using a <classpath> element in a <taskdef> in the build file.
  5. Leave ant-junit.jar in its default location in ANT_HOME/lib but include junit.jar in the <classpath> passed to<junit>(since Ant 1.7)
<?xml version="1.0" encoding="utf-8"?>
<project name="test" default="test" basedir=".">
	<!--配置基本属性-->
	<property name="src" value="src"/>
	<property name="lib" value="lib" />
	<property name="base.dir" value="junit"/>
	<!-- .class文件存放路径 -->
	<property name="build" value="${base.dir}/build"/>
	<!-- jar文件存放路径 -->
	<property name="dist" value="${base.dir}/dist"/>
	<!-- 测试报告存放路径 -->
	<property name="classpath" location="${build}"/>
	<!--配置测试报告的属性(路径)-->
	<property name="report"   value="${base.dir}/report"/>
	<property name="report.xml"  value="${report}/junit/xml"/>
	<property name="report.html" value="${report}/junit/html"/>
	<!--配置运行时classpath-->
	<path id="classpath.run">
		<pathelement path="${classpath}"/>
		<fileset dir="${lib}">
			<include name="*.jar"/>
	  	</fileset>
	</path>
	<!--配置测试时classpath-->
	<path id="classpath.test">
		<path refid="classpath.run"/>
		<path location="${dist}/lib/test-${DSTAMP}.jar"/>
	</path>
	<!--任务初始化-->
	<target name="init" >
		<tstamp/>
		<!-- 删除之前生成的文件 -->
		<delete dir="${build}"/>
		<delete dir="${report}"/>
		<delete dir="${dist}"/>
		<!-- 新建路径 -->
		<mkdir dir="${build}"/>
	</target>
	<!--配置编译任务-->
	<target name="compile" depends="init">
		<javac srcdir="${src}" destdir="${build}">
			<classpath refid="classpath.run" />
		</javac>
	</target>
	<!--配置打包任务-->
	<target name="dist" depends="compile">
		<mkdir dir="${dist}/lib"/>
		<jar jarfile="${dist}/lib/test-${DSTAMP}.jar" basedir="${build}"/>
	</target>
	<!--配置运行任务-->
	<target name="run" depends="compile, dist">
		
		<java classname="com.*.*Test">
			<classpath>
				<path refid="classpath.run"/>
			</classpath>
		</java>
		
		
		
	</target>
	
	<!--配置JUnit测试,打印测试结果-->
	<target name="test" depends="compile, dist">
		<mkdir dir="${report.xml}"/>
		<mkdir dir="${report.html}"/>
		<junit printsummary="yes" haltonfailure="no">
			<classpath refid="classpath.run"/>
			<formatter type="xml"/>
			<batchtest fork="yes" todir="${report.xml}">
				<fileset dir="${src}" includes="**/*Test*.java"/>
			</batchtest>
		</junit>
		<junitreport todir="${report.html}">
			<fileset dir="${report.xml}">
				<include name="*.xml"/>
			</fileset>
			<report format="frames" todir="${report.html}"/>
		</junitreport>
	</target>
</project>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值