利用ant 和 Junit 生成测试报告

我们除了使用java来直接运行junit之外,我们还可以使用junit提供的junit task与ant结合来运行。

涉及的几个主要的ant task如下:

<junit>,定义一个junit task
<batchtest>,位于<junit>中,运行多个TestCase
<test>,位于<junit>中,运行单个TestCase
<formatter>,位于<junit>中,定义一个测试结果输出格式
<junitreport>,定义一个junitreport task
<report>,位于<junitreport>中,输出一个junit report

 

 

 

 

 

 

运行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,并将其放置在项目的根目录下。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3 <project name="project" default="junit"> 
 4   <property name="run.classpath" value="bin"/>  
 5   <property name="run.srcpath" value="src"/>  
 6   <property name="test.xml" value="xml"/>  
 7   <property name="test.report" value="report"/>  
 8   <property name="lib.dir" value="lib"/>
 9     
10   <target name="init"> 
11     <delete dir="${test.report}"/>  
12     <mkdir dir="${test.report}"/>  
13     <delete dir="${test.xml}"/>  
14     <mkdir dir="${test.xml}"/> 
15   </target> 
16    
17   <target name="compile" depends="init"> 
18     <javac destdir="${run.classpath}" srcdir="${run.srcpath}" classpathref="compile.path" includeantruntime="on"/> 
19   </target> 
20   
21   <!--Junit task--> 
22   <target name="junit" depends="compile"> 
23     <junit printsummary="false"> 
24       <classpath path="${run.classpath}"/>  
25       <formatter type="xml"/>  
26       <batchtest todir="${test.xml}"> 
27         <fileset dir="${run.classpath}"> 
28 <!--运行${run.classpath}下所有和"**/*.class"匹配的用例-->
29           <include name="**/*.class"/> 
30         </fileset> 
31       </batchtest> 
32     </junit>  
33     <junitreport todir="${test.xml}"> 
34       <fileset dir="${test.xml}"> 
35         <include name="TEST-*.xml"/> 
36       </fileset>  
37       <report format="frames" todir="${test.report}"/>
38     </junitreport> 
39   </target> 
40 </project>

编写junit case例子,注意需要继承TestCase

 1 package com.test.report;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import org.junit.Test;
 6 
 7 import junit.framework.TestCase;
 8 
 9 public class ANTTest extends TestCase {
10 
11     int i = 0;
12     Boolean b = false;
13 
14     @Test
15     public void test001() {
16         if (i == 0) {
17             b = true;
18         } else {
19             assertTrue("i is 0", b);
20         }
21     }
22 
23     @Test
24     public void test002() {
25         if (i == 1) {
26             b = true;
27         } else {
28             assertTrue("i is 0", b);
29         }
30     }
31 
32 }

进入项目根目录,执行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需要被改成下面的内容

 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3 <project name="project" default="junit"> 
 4   <property name="run.classpath" value="bin"/>  
 5   <property name="run.srcpath" value="src"/>  
 6   <property name="test.xml" value="xml"/>  
 7   <property name="test.report" value="report"/>  
 8   <property name="lib.dir" value="lib"/>
 9     
10   <path id="compile.path"> 
11     <fileset dir="${lib.dir}"> 
12       <include name="**/*.jar"/> 
13     </fileset>  
14     <pathelement path="${run.classpath}"/> 
15   </path>
16     
17   <target name="init"> 
18     <delete dir="${test.report}"/>  
19     <mkdir dir="${test.report}"/>  
20     <delete dir="${test.xml}"/>  
21     <mkdir dir="${test.xml}"/> 
22   </target> 
23    
24   <target name="compile" depends="init"> 
25     <javac destdir="${run.classpath}" srcdir="${run.srcpath}" classpathref="compile.path" includeantruntime="on"/>
26   </target>
27     
28   <!--Junit task-->  
29   <target name="junit" depends="compile"> 
30     <junit printsummary="true"> 
31       <classpath refid="compile.path"/>  
32       <formatter type="xml"/>  
33       <!--<test name="cn.com.vp4.hup.testcase.TestCase_AudioFocus"/> --> 
34       <batchtest todir="${test.xml}"> 
35         <fileset dir="${run.classpath}"> 
36           <include name="**/*AudioFocus.class"/> 
37         </fileset> 
38       </batchtest>
39     </junit>  
40     <junitreport todir="${test.xml}"> 
41       <fileset dir="${test.xml}"> 
42         <include name="TEST-*.xml"/> 
43       </fileset>  
44       <report format="frames" todir="${test.report}"/> 
45     </junitreport> 
46   </target> 
47 </project>

 

posted @ 2016-07-15 15:25  月色深潭  阅读(6056)  评论(0编辑  收藏  举报