利用ant 和 Junit 生成测试报告
2016-07-15 15:25
274 查看
我们除了使用java来直接运行junit之外,我们还可以使用junit提供的junit task与ant结合来运行。
涉及的几个主要的ant task如下:
运行Junit需要jakarta-ant-1.4-optional.jar和Junit.jar包,因为这两个包用于支持ant task--<junit>的,所以不能在build.xml文件中加载,需要将他们放到ANT_HOME中的lib目录中
junit.jar下载地址:http://pan.baidu.com/s/1hsbg464
jakarta-ant-1.4-optional.jar下载地址:http://pan.baidu.com/s/1hsjTXhM
完成上面的配置之后,开始编写build.xml,并将其放置在项目的根目录下。
编写junit case例子,注意需要继承TestCase
进入项目根目录,执行ant命令
进入根目录下的report目录,找到index.html文件
打开index.html查看测试结果
参考:
http://www.cnblogs.com/puresoul/p/4201565.html http://blog.csdn.net/tochal/article/details/12560151
如果在运行testcase时需要依赖第三方包,那么build.xml需要被改成下面的内容
涉及的几个主要的ant task如下:
<junit>,定义一个junit task <batchtest>,位于<junit>中,运行多个TestCase <test>,位于<junit>中,运行单个TestCase <formatter>,位于<junit>中,定义一个测试结果输出格式 <junitreport>,定义一个junitreport task <report>,位于<junitreport>中,输出一个junit report |
junit.jar下载地址:http://pan.baidu.com/s/1hsbg464
jakarta-ant-1.4-optional.jar下载地址:http://pan.baidu.com/s/1hsjTXhM
完成上面的配置之后,开始编写build.xml,并将其放置在项目的根目录下。
<?xml version="1.0" encoding="utf-8"?> <project name="project" default="junit"> <property name="run.classpath" value="bin"/> <property name="run.srcpath" value="src"/> <property name="test.xml" value="xml"/> <property name="test.report" value="report"/> <property name="lib.dir" value="lib"/> <target name="init"> <delete dir="${test.report}"/> <mkdir dir="${test.report}"/> <delete dir="${test.xml}"/> <mkdir dir="${test.xml}"/> </target> <target name="compile" depends="init"> <javac destdir="${run.classpath}" srcdir="${run.srcpath}" classpathref="compile.path" includeantruntime="on"/> </target> <!--Junit task--> <target name="junit" depends="compile"> <junit printsummary="false"> <classpath path="${run.classpath}"/> <formatter type="xml"/> <batchtest todir="${test.xml}"> <fileset dir="${run.classpath}"> <!--运行${run.classpath}下所有和"**/*.class"匹配的用例--> <include name="**/*.class"/> </fileset> </batchtest> </junit> <junitreport todir="${test.xml}"> <fileset dir="${test.xml}"> <include name="TEST-*.xml"/> </fileset> <report format="frames" todir="${test.report}"/> </junitreport> </target> </project>
编写junit case例子,注意需要继承TestCase
package com.test.report; import static org.junit.Assert.*; import org.junit.Test; import junit.framework.TestCase; public class ANTTest extends TestCase { int i = 0; Boolean b = false; @Test public void test001() { if (i == 0) { b = true; } else { assertTrue("i is 0", b); } } @Test public void test002() { if (i == 1) { b = true; } else { assertTrue("i is 0", b); } } }
进入项目根目录,执行ant命令
进入根目录下的report目录,找到index.html文件
打开index.html查看测试结果
参考:
http://www.cnblogs.com/puresoul/p/4201565.html http://blog.csdn.net/tochal/article/details/12560151
如果在运行testcase时需要依赖第三方包,那么build.xml需要被改成下面的内容
<?xml version="1.0" encoding="utf-8"?> <project name="project" default="junit"> <property name="run.classpath" value="bin"/> <property name="run.srcpath" value="src"/> <property name="test.xml" value="xml"/> <property name="test.report" value="report"/> <property name="lib.dir" value="lib"/> <path id="compile.path"> <fileset dir="${lib.dir}"> <include name="**/*.jar"/> </fileset> <pathelement path="${run.classpath}"/> </path> <target name="init"> <delete dir="${test.report}"/> <mkdir dir="${test.report}"/> <delete dir="${test.xml}"/> <mkdir dir="${test.xml}"/> </target> <target name="compile" depends="init"> <javac destdir="${run.classpath}" srcdir="${run.srcpath}" classpathref="compile.path" includeantruntime="on"/> </target> <!--Junit task--> <target name="junit" depends="compile"> <junit printsummary="true"> <classpath refid="compile.path"/> <formatter type="xml"/> <!--<test name="cn.com.vp4.hup.testcase.TestCase_AudioFocus"/> --> <batchtest todir="${test.xml}"> <fileset dir="${run.classpath}"> <include name="**/*AudioFocus.class"/> </fileset> </batchtest> </junit> <junitreport todir="${test.xml}"> <fileset dir="${test.xml}"> <include name="TEST-*.xml"/> </fileset> <report format="frames" todir="${test.report}"/> </junitreport> </target> </project>
相关文章推荐
- spring源码分析之spring-core-env
- 深入理解计算机系统chapter5
- jquery.ajaxfileupload.js
- 李洪强iOS开发之XMPP
- java基础数据类型(你还记得几个)
- Android的后台任务IntentService
- 移动客户端开发快速上手ReactNative的学习路线
- NSScanner类的使用
- 归并-插入混合排序算法
- ajax 提交数组 泛型集合(二)
- 原码, 反码, 补码 详解
- opencv—显示图像
- 常见排序算法C++总结
- 查看Mysql实时执行的Sql语句
- 哪位大虾知道斜杠,反斜械, 破折号 等的英文怎么拼写
- Android Activity泄漏问题解决方案
- Java网络编程的开始
- 如何将pdf转换成excel可编辑的格式
- Hbase架构与原理
- Windows 程序内部运行机制