ant集成junit自动测试的build.xml标准模板

利用 Ant 集成 Junit 自动测试并生成测试报告,可以极大的提高测试的工作效率,其优势只要是测试用例的批量处理功能。前不久在网上看到很多关于 Junit 测试的文章,欣闻 Ant 支持 Junit 的集成,迫不及待的尝试了一番。

1.          ant 的配置:

本案例采用 apache-ant-<!-- --><st1:chsdate isrocdate="False" month="12" day="30" islunardate="False" w:st="on" year="1899">1.6.5</st1:chsdate> 版本,下载 ant 后需要将 Junit3.8.1 拷贝到 ANT_HOME lib 目录下。 Ant 在执行过程 TestCase 过程中除了需要在 classpath 中可见,还需要在 ant 中可见,否则会报错。

2.          目录结构:

工作目录

| src | com | wallace | Calculator.java

                            | test | TestCalculator.java

| lib | junit.jar

| build

| report

3.          build.xml 模板:

<?xml version="1.0" encoding="utf-8"?>

<project name="test" default="test" basedir=".">

<!-- --><o:p>  </o:p>

       <!-- 配置基本属性 -->

       <property name="src" value="src"/>

       <property name="build" value="build"/>

       <property name="lib" value="lib" />

       <property name="dist" value="dist"/>

       <property name="classpath" location="${build}"/>

      

       <!-- 配置测试报告的属性 -->

       <property name="report"   value="report"/>

       <property name="report.xml"  value="${report}/junit/xml"/>

       <property name="report.html" value="${report}/junit/html"/>

<o:p>  </o:p>

       <!-- 配置运行时 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.TestCalculator">

             <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>

<o:p>  </o:p>

</project>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值