您的位置:首页 > 其它

利用ant 和 Junit 生成测试报告

2016-07-15 15:25 274 查看
我们除了使用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,并将其放置在项目的根目录下。

<?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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: