ant整合junit自己主动化測试
一. 使用Junit进行測试
1. Java业务代码:
public class HelloWorld {
// 測试返回"world"
public String hello() {
return "world";
}
// 測试返回"hello"
public String world() {
return "hello";
}
// 測试为空
public String nil() {
return null;
}
// 測试不为空
public String notNil() {
return "abc";
}
// 測试抛出异常
public String ext() {
return null;
}
}
2. 使用junit3測试, 继承TestCase
import junit.framework.TestCase;
public class HelloWorldTest extends TestCase {
private HelloWorld helloWorld; @Override
protected void setUp() throws Exception {
helloWorld = new HelloWorld();
System.out.println("helloWorld init");
} public void testHello() {
String str = helloWorld.hello();
assertEquals("測试world失败", str, "world");
} public void testWorld() {
String str = helloWorld.world();
assertEquals("測试world失败", str, "hello");
} public void testNotNil() {
assertNotNull("对象为空", helloWorld.notNil());
} public void testNil() {
assertNull("对象不为空", helloWorld.nil());
} public void testExt() {
try {
helloWorld.ext();
fail("没有抛出异常");
} catch (NumberFormatException e) {
}
} @Override
protected void tearDown() throws Exception {
System.out.println("hello world destory");
helloWorld = null;
}
}
3. 使用junit4測试, 使用注解
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*; // 静态引入, assertEquals兼容junit3
public class HelloWorldTest {
private HelloWorld helloWorld; @Before
public void setUp() {
helloWorld = new HelloWorld();
} @Test
public void testHello() {
String str = helloWorld.hello();
assertEquals("hello測试失败",str,"world");
} @Test
public void testWorld() {
String str = helloWorld.world();
assertEquals("world測试失败",str, "hello");
} @Test
public void testNil() {
assertNull("对象不为空",helloWorld.nil());
} @Test
public void testNotNil() {
assertNotNull("对象为空", helloWorld.notNil());
} @Test(expected=NumberFormatException.class)
public void testExt() {
helloWorld.ext();
} @After
public void tearDown() {
helloWorld = null;
}
}
二. ant整合junit測试
1. 生成測试报告:
<?xml version="1.0" encoding="UTF-8"? >
<project name="junit-test">
<property name="src" location="src" />
<property name="test" location="test" />
<property name="lib" location="lib" />
<property name="build" location="build" />
<property name="build.classes" location="${build}/classes" />
<property name="build.test" location="${build}/test" />
<property name="build.test.classes" location="${build.test}/classes" />
<property name="build.test.report" location="${build.test}/report" />
<!--
<property name="runTest" value="com.zdp.test.HelloWorldTest" />
-->
<property name="runTest" value="**/*Test.class"></property> <!-- 手动指明classpath, 一个path中能够加多个classpath,使用链式编写 -->
<path id="lib_path">
<fileset dir="${lib}" includes="*.jar" />
</path>
<path id="compile_path">
<path refid="lib_path" />
<pathelement location="${build.classes}"/>
</path>
<path id="compile_test_path">
<path refid="compile_path" />
<pathelement location="${build.test.classes}"/>
</path> <target name="clean">
<echo>1.開始清理文件</echo>
<delete dir="${build}"></delete>
</target> <target name="init">
<echo>2.项目的初始化</echo>
<mkdir dir="${build}"/>
<mkdir dir="${build.classes}"/>
<mkdir dir="${build.test}"/>
<mkdir dir="${build.test.classes}"/>
<mkdir dir="${build.test.report}"/>
</target> <target name="compile_src" depends="init">
<echo>3.编译源文件</echo>
<javac srcdir="${src}" destdir="${build.classes}"
classpathref="lib_path" includeantruntime="true"/>
</target> <target name="compile_test" depends="compile_src">
<echo>4.编译測试文件</echo>
<javac srcdir="${test}" destdir="${build.test.classes}"
classpathref="compile_path" includeantruntime="true"/>
</target> <target name="run_test" depends="clean, compile_test">
<echo>5.执行单元測试</echo>
<junit printsummary="false" haltonfailure="false">
<classpath refid="compile_test_path"></classpath>
<formatter type="xml"/>
<batchtest todir="${build.test.report}">
<fileset dir="${build.test.classes}" includes="${runTest}"/>
</batchtest>
</junit>
<junitreport todir="${build.test.report}">
<fileset dir="${build.test.report}" includes="TEST-*.xml"/>
<report format="frames" todir="${build.test.report}/html"/>
</junitreport>
</target>
<target name="end" depends="run_test">
<echo>6.測试结束</echo>
</target>
</project>
2. 生成doc文档并打包
<? xml version="1.0" encoding="UTF-8"? >
<project default="zip">
<property name="src" location="src"></property>
<property name="build" location="build"></property>
<property name="build.classes" location="${build}/classes"></property>
<property name="build.doc" location="${build}/doc/api"></property>
<property name="zip" location="${build}/zip"></property>
<property name="version" value="SNAPSHOT_0.1"></property>
<property name="projectName" value="helloworld_${version}"></property>
<property name="zipFileName" value="helloworld_${version}.zip"></property> <target name="clean">
<echo>1.開始清理文件</echo>
<delete dir="${build}"></delete>
</target> <target name="init">
<echo>2.项目的初始化</echo>
<mkdir dir="${build}"/>
<mkdir dir="${build.classes}"/>
<mkdir dir="${build.doc}"/>
</target> <target name="doc" depends="init">
<echo>3.生成doc文档</echo>
<javadoc sourcepath="${src}" private="true" windowtitle="HelloWorld JAVA DOC"
use="true" packagenames="com.zdp.*" destdir="${build.doc}"
charset="UTF-8" docencoding="UTF-8" encoding="UTF-8">
<classpath path="${build.classes}"></classpath>
</javadoc>
</target> <target name="zip" depends="doc">
<echo>4.生成zip包</echo>
<zip destfile="${zip}/${zipFileName}" duplicate="preserve">
<zipfileset dir="${build.doc}" includes="**/*.*" prefix="${projectName}/doc/api"></zipfileset>
<zipfileset dir="${src}" includes="**/*.*" prefix="${projectName}/src"></zipfileset>
</zip>
</target>
</project>
ant整合junit自己主动化測试的更多相关文章
- Android自己主动化測试解决方式
如今,已经有大量的Android自己主动化測试架构或工具可供我们使用,当中包含:Activity Instrumentation, MonkeyRunner, Robotium, 以及Robolect ...
- 带有机器人框架的.NET自己主动化測试
Clayton Neal在软件測试和质量保证方面有超过13年的经验,当中有八年的Windows, web,和移动应用程序的測试自己主动化经验.他在測试领域的全部等级都工作过.近期他在Bloomberg ...
- 【金阳光測试】大话Android自己主动化測试--Android自己主动化系列(1)--金阳光于2013年4月份
Android自己主动化測试框架和工具在四年多的发展日趋成熟. 从五年前的第一代自己主动化架构演进到眼下第四代(本系列讲座第7篇后将具体剖析第三代和第四代自己主动化框架)从曾经最早谷歌推崇的monke ...
- Android自己主动化測试之Monkeyrunner用法及实例
眼下android SDK里自带的现成的測试工具有monkey 和 monkeyrunner两个.大家别看这俩兄弟名字相像,但事实上是完全然全不同的两个工具,应用在不同的測试领域.总的来说,monke ...
- MAC中在eclipse luna上搭建移动平台自己主动化測试框架(UIAutomator/Appium/Robotium/MonkeyRunner)关键点记录
这几天由于原来在用的hp laptop的电池坏掉了,机器一不小心就断电.所以仅仅能花时间在自己的mackbook pro上又一次搭建整套环境.大家都知道搭建好开发环境是个非常琐碎须要耐心的事情,特别是 ...
- Android Monkey自己主动化測试
前言 假设你做Android开发,还没有使用过Monkey进行測试,那么今天看到这篇文章,希望能解决你Android測试中的一些问题.起码能帮你省点測试的时间而且发现很多其它的问题. Monkey简单 ...
- iOS自己主动化測试的那些干货
前言 假设有測试大佬发现内容不正确.欢迎指正,我会及时改动. 大多数的iOS App(没有持续集成)迭代流程是这种 也就是说.測试是公布之前的最后一道关卡.假设bug不能在測试中发现,那么bug 就会 ...
- Android 自己主动化測试(3)<monkeyrunner> 依据ID查找对象&touch&type (python)
我在之前的两篇文章中用java来实现过 Android 自己主动化測试(1)怎样安装和卸载一个应用(java).Android 自己主动化測试(2)依据ID查找对象(java). 可是本质上都是用mo ...
- 基于QTP的自己主动化測试框架介绍
继前面用了七章介绍了基于QTP的自己主动化測试框架,以下再用几个视频再补充一下. 视频一:基本框架特点介绍说明 .框架的特点从正反两面进行了分析以及主要思想 http://v. ...
随机推荐
- Too Many open files 问题排查
问题描述:使用netty做性能测试时,并发过大造成Too Many open files问题 该类错误是因为linux系统对socket连接时需要打开的文件句柄数有限制可以通过ulimit -a 查看 ...
- 【贪心】小Y的炮[cannon]题解
模拟赛的题目,做的时候由于第二题表打太久了,只剩下40分钟,想都没想就写了一个爆搜20分... 这道题单调性很关键,下面会解释 P.S.解释在代码里 #include<cstdio> #i ...
- POJ 2976 裸的01分数规划
题意:给你n个数对(认为是a数组和b数组吧),从中取n-m个数对,如果选第i个数对,定义x[i]=1,求R=∑(a[i]*x[i])/∑(b[i]*x[i])取得最大值时R的值.输出R*100(保留到 ...
- C#三种创建对象方法所需时间比较。。。。。
C#创建对象的三种方法 new().Activator.Assembly,接下来通过代码直接来看看运行的速度.... 首先,先看看三种创建对象实例的方法: //new(); public stati ...
- OpenVX
OpenVX openvx 1. 编译 尝试编译openvx_sample,下载相关代码. 下载的sample code直接使用make可以生成libopenvx.so. 使用python Buil ...
- C语言笔记(一)
笑话一枚:程序员 A:“哥们儿,最近手头紧,借点钱?”程序员 B:“成啊,要多少?”程序员 A:“一千行不?”程序员 B:“咱俩谁跟谁!给你凑个整,1024,拿去吧.” =============== ...
- JavaOO小结二,及MySQL小结
流按照传输内容分有几种?各自的父类是什么? 流按照传输内容有 字节流.字符流.对象流.但其本质都是字节流.字符流和对象流是在字节流基础上作了一层封装,以便更好对字符和对象进行操作. 字节流的父类:In ...
- OpenCv:椭圆上点的计算方程
椭圆 椭圆(Ellipse)是平面内到定点F1.F2的距离之和等于常数(大于|F1F2|)的动点P的轨迹,F1.F2称为椭圆的两个焦点.其数学表达式为: ...
- Type inference
Type inference refers to the automatic detection of the data type of an expression in a programming ...
- JavaScript中原生事件
DOM0事件模型: 所有浏览器都支持,只能注册一种事件 1.绑定: document.getElementById("id").onclick = function(e){}; 解 ...