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

本文介绍如何使用Ant构建工具与JUnit测试框架集成,实现自动化测试并生成可视化的测试报告。通过示例代码详细展示了配置步骤及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

好了,接下来继续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即可看到测试报告详情

加一张报告页面截图


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值