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 ...
随机推荐
- ES6新特性:利用解构赋值 (destructuring assignment), 简化代码
本文的Demo的运行环境为nodeJS, 参考:让nodeJS支持ES6的词法----babel的安装和使用 : 解构赋值是一种表达式, 利用这种新语法, 可以直接从数组或者对象中快速提取值 赋值给不 ...
- Cygwin使用方法
对于 UNIX 本身,也有各种称呼.IBM® 大型机用户说各种带字母 “z” 的行话,比如 IBM z/OS® 和 System z9 Virtual Machine (z/VM):嵌套系统开发人员使 ...
- bzoj4400: tjoi2012 桥
先传代码再填坑 #include <iostream> #include <cstdio> #include <cmath> #include <cstrin ...
- SPOJ ORDERSET - Order statistic set
ORDERSET - Order statistic set In this problem, you have to maintain a dynamic set of numbers whic ...
- Java数组操作利器:Arrays工具类
java.util.Arrays提供大量的工具方法来操作数组,这些方法全是静态方法. 1 便捷创建List public static <T> List<T> asList(T ...
- 【BZOJ-4455】小星星 容斥 + 树形DP
4455: [Zjoi2016]小星星 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 204 Solved: 137[Submit][Status] ...
- libusb-win32简介~
libusb-win32简介 libusb-win32 is a port of the USB library libusb (http://sf.net/projects/libusb/) to ...
- php Unable to find the wrapper "https"
php -m | grep -i --color openssl php 没有openssl模块 cd /data/source/php-5.3.29/ext/openssl #php的解压包下面 y ...
- CF 370B Berland Bingo
题目链接: 传送门 Berland Bingo time limit per test:1 second memory limit per test:256 megabytes Descrip ...
- JQuery 学习笔记(01)
JQuery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari 2.0+, Oper ...