【学习笔记】系列十五:Ant单元测试JUnit

来源:互联网 发布:用ps做淘宝店长图 编辑:程序博客网 时间:2024/06/05 07:34

好了,接下来继续Ant,有关单元测试,涉及到JUnit和TestNG

首先来说JUnit,执行用例很简单,但是大部分人应该不仅仅需要执行,更多的是想要得到测试报告

JUnit自生成.xml格式的测试报告,不过可视性不是很好,需要整理

而且,IDE下的JUnit执行后,测试报告是不自动储存的


Demo如下:

这次的代码是自己的,不会出现xxxx了

<?xml version="1.0" encoding="UTF-8"?><project name="TestJekinsJunit" default="junit" basedir=".">    <!-- ========== -->    <!-- 变量设置 -->    <!-- ========== -->    <!-- 源代码路径 -->    <property name="src.path" location="src/com/doulei/jenkins/junit/test/calculator"/>    <!-- 编译文件class路径 -->    <property name="build.path" location="build/"/>    <!-- 单元测试代码路径 -->    <property name="test.path" location="src/com/doulei/jenkins/junit/test/cases"/>    <!-- lib包路径 -->    <property name="lib.path" value="lib/"/>    <!-- 生成报告xml路径 -->    <property name="report.path" value="report/"/>    <!-- ========== -->    <!-- 设置classpath -->    <!-- ========== -->    <path id="compile.path">        <fileset dir="${lib.path}">            <include name="**/*.jar"/>        </fileset>        <pathelement path="${build.path}"/>    </path>    <!-- ========== -->    <!-- 清除历史文件 -->    <!-- ========== -->    <target name="clean">        <delete dir="${build.path}"/>        <delete dir="${report.path}"/>    </target>    <target name="init" depends="clean">        <mkdir dir="${build.path}"/>        <mkdir dir="${report.path}"/>    </target>    <!-- ========== -->    <!-- 编译文件 -->    <!-- ========== -->    <target name="compile" depends="init" description="compile">        <javac srcdir="${src.path}" destdir="${build.path}" includeAntRuntime="false" debug="on" encoding="utf-8"               classpathref="compile.path"/>        <javac srcdir="${test.path}" destdir="${build.path}" includeAntRuntime="false" debug="on" encoding="utf-8"               classpathref="compile.path"/>    </target>    <!-- ========== -->    <!-- 执行测试案例 -->    <!-- ========== -->    <target name="junit" depends="compile">        <junit printsummary="true" fork="true">            <formatter type="xml" usefile="true"/>            <classpath refid="compile.path"/>            <batchtest fork="on" todir="${report.path}" haltonfailure="no">                <fileset dir="${build.path}">                    <include name="**/*Test.class"/>                </fileset>            </batchtest>        </junit>        <!-- 产生测试报告文档 -->        <junitreport todir="${report.path}">            <fileset dir="${report.path}">                <include name="TEST-*.xml"/>            </fileset>            <report format="frames" todir="${report.path}"/>        </junitreport>    </target></project>


Ant执行后,打开report路径下的index.html即可看到测试报告详情

加一张报告页面截图


0 0