testNg vs junit 4.X @Test
http://www.ibm.com/developerworks/cn/java/j-cq08296/
一个简单的测试用例
初看起来,JUnit 4 和 TestNG 中实现的测试非常相似。为了更好地理解我的意思,请看一下清单 1 中的代码。这是一个 JUnit 4 测试,它有一个 macro-fixture(即仅在所有测试运行前调用一次的 fixture),这个 macro-fixture 由 @BeforeClass 属性表示:
清单 1. 一个简单的 JUnit 4 测试用例
package test.com.acme.dona.dep; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.BeforeClass;
import org.junit.Test; public class DependencyFinderTest {
private static DependencyFinder finder; @BeforeClass
public static void init() throws Exception {
finder = new DependencyFinder();
} @Test
public void verifyDependencies()
throws Exception {
String targetClss =
"test.com.acme.dona.dep.DependencyFind"; Filter[] filtr = new Filter[] {
new RegexPackageFilter("java|junit|org")}; Dependency[] deps =
finder.findDependencies(targetClss, filtr); assertNotNull("deps was null", deps);
assertEquals("should be 5 large", 5, deps.length);
}
}
JUnit 用户会立即注意到:这个类中没有了以前版本的 JUnit 中所要求的一些语法成分。这个类没有 setUp() 方法,也不对 TestCase 类进行扩展,甚至也没有哪个方法的名称以 test 开始。这个类还利用了 Java 5 的一些特性,例如静态导入,很明显地,它还使用了注释。
更多的灵活性
在清单 2 中,您可以看到同一个 测试项目。不过这次是用 TestNG 实现的。这里的代码跟清单 1 中的测试代码有个微妙的差别。发现了吗?
清单 2. 一个 TestNG 测试用例
package test.com.acme.dona.dep; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Configuration;
import org.testng.annotations.Test; public class DependencyFinderTest {
private DependencyFinder finder; @BeforeClass
private void init(){
this.finder = new DependencyFinder();
} @Test
public void verifyDependencies()
throws Exception {
String targetClss =
"test.com.acme.dona.dep.DependencyFind"; Filter[] filtr = new Filter[] {
new RegexPackageFilter("java|junit|org")}; Dependency[] deps =
finder.findDependencies(targetClss, filtr); assertNotNull(deps, "deps was null" );
assertEquals(5, deps.length, "should be 5 large");
}
}
显然,这两个清单很相似。不过,如果仔细看,您会发现 TestNG 的编码规则比 JUnit 4 更灵活。清单 1 里,在 JUnit 中我必须把 @BeforeClass 修饰的方法声明为 static,这又要求我把 fixture,即 finder 声明为 static。我还必须把 init() 声明为 public。看看清单 2,您就会发现不同。这里不再需要那些规则了。我的 init() 方法既不是 static,也不是 public。
从最初起,TestNG 的灵活性就是其主要优势之一,但这并非它惟一的卖点。TestNG 还提供了 JUnit 4 所不具备的其他一些特性。
junit4.x
testNg vs junit 4.X @Test的更多相关文章
- 关于testNG和JUnit的对比
关于选择JUnit还是选testNG,这几篇文章,建议读一读: API参考文档: Junit API文档:http://junit.org/junit4/javadoc/latest/index.ht ...
- testng入门教程11 TestNG运行JUnit测试
现在,您已经了解了TestNG和它的各种测试,如果现在担心如何重构现有的JUnit代码,那就没有必要,使用TestNG提供了一种方法,从JUnit和TestNG按照自己的节奏.也可以使用TestNG执 ...
- 总结TestNg与JUnit的异同
工作中一直用的是junit,近期稍微学习了一下TestNg,发现TestNg比java强大太多. TestNg简介 TestNg也是一套测试框架,它的灵感来源于Junit(java的单元测试框架)和N ...
- TestNG 与 Junit的比较
转自 http://www.blogjava.net/fanscial/archive/2005/12/14/23780.html 1. JDK 5 Annotations (JDK ...
- TestNG 与 Junit的比较(转)
转自 http://www.blogjava.net/fanscial/archive/2005/12/14/23780.html 1. JDK 5 Annotations (JDK ...
- 【转】TestNG 与 Junit的比较
转自 http://www.blogjava.net/fanscial/archive/2005/12/14/23780.html 1. JDK 5 Annotations (JDK ...
- JUnit 4 与 TestNG 对比
原文出处: 付学良的网志 原文出处2: http://www.importnew.com/16270.html -------------------------------------------- ...
- Java Unit Testing - JUnit & TestNG
转自https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaUnitTesting.html yet another insignifican ...
- TestNG 入门教程
原文出处:http://www.cnblogs.com/TankXiao/p/3888070.html 阅读目录 TestNG介绍 在Eclipse中在线安装TestNG 在Eclipse中离线安装T ...
随机推荐
- 概率DP light oj 1038
t个数据 然后一个n 输出变成1的期望 看个数据 dp[n]代表n变成1的期望 cnt代表因子个数 pi代表因子 那么dp[n]=1/cnt*(dp[n/p1]+1)+1/cnt*(dp[n/p2]+ ...
- git初体验(三)git分支
分支的理念就是分身,就像孙悟空拔出猴毛变出很多跟自己一模一样的猴子,然后每个猴子做自己的事情互不干涉,等到所有猴子做完之后,猴子集合来合并劳动成果,然后悟空就把那些猴子猴孙门统统收回了. 你创建了一个 ...
- 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件
CustomResource ResourceDictionary 加载外部的 ResourceDictionary 文件 示例1.演示“CustomResource”相关知识点Resource/Cu ...
- 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧
背水一战 Windows 10 之 绑定 x:Bind 绑定 x:Bind 绑定之 x:Phase 使用绑定过程中的一些技巧 示例1.演示 x:Bind 绑定的相关知识点Bind/BindDemo.x ...
- 【USACO 1.3】Barn Repair
贪心,去掉最大的min(m,c)-1个间隔 /******************************************* TASK: barn1 LANG: C++ Created Tim ...
- 【BZOJ-1952】城市规划 [坑题] 仙人掌DP + 最大点权独立集(改)
1952: [Sdoi2010]城市规划 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 73 Solved: 23[Submit][Status][ ...
- jquery插件-表单验证插件-validator对象
三 Validator对象 1.介绍:Validate方法返回的对象称作Validator对象 2.使用 Validator对象常用方法 Validator.form() 返回:Boolean 验证: ...
- 【poj3264】 Balanced Lineup
http://poj.org/problem?id=3264 (题目链接) 题意 给出序列,求区间最大值-最小值 Solution 无修改,询问较多,ST表水一发. ST算法(Sparse Table ...
- jQuery 仿百度输入标签插件
之前在做cms系统的时候,由于缺少tag的设计,准备自己搞一个,百般搜索,居然给我发现了一个无论在视觉和操作上都是非常爽的 原作者的地址:http://www.soso.io/article/1710 ...
- android备忘录
1.跑马灯-滚动字幕 多用于广告,在屏幕上方滚动显示,可以是文字,图片等 #滚动字幕 TextView 中加(singleLine="true",ellipsize="m ...