- 浏览: 325563 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
大菜头:
yelifei94 写道按你说发 依旧乱码中。。。如果依然无法 ...
JForum 2.1.8 中文乱码问题 -
shenxielangzi:
折腾半天呀,也是这个问题。疯了
java.sql.SQLException: ORA-00911: 无效字符 -
v4S9vdHc:
这应该是说 return 要在函数体内
SyntaxError: 'return' outside function -
chjh_721:
Supported Values for @SuppressWarnings -
zhanyingle_1981:
哈哈,同道中人
java.sql.SQLException: ORA-00911: 无效字符
http://www.mkyong.com/unittest/junit-4-vs-testng-comparison/
JUnit 4 Vs TestNG
JUnit 4 and TestNG are both very popular unit test framework in Java. Both frameworks look very similar in functionality. Which one is better? Which unit test framework should i use in Java project?
Here i did a feature comparison between JUnit 4 and TestNG.
1) Annotation Support
The annotation supports are implemented in both JUnit 4 and TestNG look similar.
Feature | JUnit 4 | TestNG |
test annotation | @Test | @Test |
run before all tests in this suite have run | – | @BeforeSuite |
run after all tests in this suite have run | – | @AfterSuite |
run before the test | – | @BeforeTest |
run after the test | – | @AfterTest |
run before the first test method that belongs to any of these groups is invoked | – | @BeforeGroups |
run after the last test method that belongs to any of these groups is invoked | – | @AfterGroups |
run before the first test method in the current class is invoked | @BeforeClass | @BeforeClass |
run after all the test methods in the current class have been run | @AfterClass | @AfterClass |
run before each test method | @Before | @BeforeMethod |
run after each test method | @After | @AfterMethod |
ignore test | @ignore | @Test(enbale=false) |
expected exception | @Test(expected = ArithmeticException.class) | @Test(expectedExceptions = ArithmeticException.class) |
timeout | @Test(timeout = 1000) | @Test(timeout = 1000) |
The main annotation differences between JUnit4 and TestNG are
1) In JUnit 4, we have to declare “@BeforeClass” and “@AfterClass” method as static method. TestNG is more flexible in method declaration, it does not have this constraints.
2) 3 additional setUp/tearDown level: suite and group (@Before/AfterSuite, @Before/AfterTest, @Before/AfterGroup). See more detail here.
JUnit 4
@BeforeClass public static void oneTimeSetUp() { // one-time initialization code System.out.println("@BeforeClass - oneTimeSetUp"); }
TestNG
@BeforeClass public void oneTimeSetUp() { // one-time initialization code System.out.println("@BeforeClass - oneTimeSetUp"); }
2) In JUnit 4, the annotation naming convention is a bit confusing, e.g “Before”, “After” and “Expected”, we do not really understand what is “Before” and “After” do, and what we “Expected” from test method? TestNG is easier to understand, it using “BeforeMethod”, “AfterMethod” and “ExpectedException” instead.
2) Exception Test
The “exception testing” means what exception throw from the unit test, this feature are implemented in both JUnit 4 and TestNG.
JUnit 4
@Test(expected = ArithmeticException.class) public void divisionWithException() { int i = 1/0; }
TestNG
@Test(expectedExceptions = ArithmeticException.class) public void divisionWithException() { int i = 1/0; }
3) Ignore Test
The “Ignored” means whether it should ignore the unit test, this feature are implemented in both JUnit 4 and TestNG .
JUnit 4
@Ignore("Not Ready to Run") @Test public void divisionWithException() { System.out.println("Method is not ready yet"); }
TestNG
@Test(enabled=false) public void divisionWithException() { System.out.println("Method is not ready yet"); }
4) Time Test
The “Time Test” means if an unit test takes longer than the specified number of milliseconds to run, the test will terminated and mark as fails, this feature is implemented in both JUnit 4 and TestNG .
JUnit 4
@Test(timeout = 1000) public void infinity() { while (true); }
TestNG
@Test(timeOut = 1000) public void infinity() { while (true); }
5) Suite Test
The “Suite Test” means bundle a few unit test and run it together. This feature is implemented in both JUnit 4 and TestNG. However both are using very different method to implement it.
JUnit 4
The “@RunWith” and “@Suite” are use to run the suite test. The below class means both unit test “JunitTest1” and “JunitTest2” run together after JunitTest5 executed. All the declaration is define inside the class.
@RunWith(Suite.class) @Suite.SuiteClasses({ JunitTest1.class, JunitTest2.class }) public class JunitTest5 { }
TestNG
XML file is use to run the suite test. The below XML file means both unit test “TestNGTest1” and “TestNGTest2” will run it together.
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" > <suite name="My test suite"> <test name="testing"> <classes> <class name="com.fsecure.demo.testng.TestNGTest1" /> <class name="com.fsecure.demo.testng.TestNGTest2" /> </classes> </test> </suite>
TestNG can do more than bundle class testing, it can bundle method testing as well. With TestNG unique “Grouping” concept, every method is tie to a group, it can categorize tests according to features. For example,
Here is a class with four methods, three groups (method1, method2 and method3)
@Test(groups="method1") public void testingMethod1() { System.out.println("Method - testingMethod1()"); } @Test(groups="method2") public void testingMethod2() { System.out.println("Method - testingMethod2()"); } @Test(groups="method1") public void testingMethod1_1() { System.out.println("Method - testingMethod1_1()"); } @Test(groups="method4") public void testingMethod4() { System.out.println("Method - testingMethod4()"); }
With the following XML file, we can execute the unit test with group “method1” only.
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" > <suite name="My test suite"> <test name="testing"> <groups> <run> <include name="method1"/> </run> </groups> <classes> <class name="com.fsecure.demo.testng.TestNGTest5_2_0" /> </classes> </test> </suite>
With “Grouping” test concept, the integration test possibility is unlimited. For example, we can only test the “DatabaseFuntion” group from all of the unit test classes.
6) Parameterized Test
The “Parameterized Test” means vary parameter value for unit test. This feature is implemented in both JUnit 4 and TestNG. However both are using very different method to implement it.
JUnit 4
The “@RunWith” and “@Parameter” is use to provide parameter value for unit test, @Parameters have to return List[], and the parameter will pass into class constructor as argument.
@RunWith(value = Parameterized.class) public class JunitTest6 { private int number; public JunitTest6(int number) { this.number = number; } @Parameters public static Collection<Object[]> data() { Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } }; return Arrays.asList(data); } @Test public void pushTest() { System.out.println("Parameterized Number is : " + number); } }
It has many limitations here; we have to follow the “JUnit” way to declare the parameter, and the parameter has to pass into constructor in order to initialize the class member as parameter value for testing. The return type of parameter class is “List []”, data has been limited to String or a primitive value for testing.
TestNG
XML file or “@DataProvider” is use to provide vary parameter for testing.
XML file for parameterized test.
Only “@Parameters” declares in method which needs parameter for testing, the parametric data will provide in TestNG’s XML configuration files. By doing this, we can reuse a single test case with different data sets and even get different results. In addition, even end user, QA or QE can provide their own data in XML file for testing.
Unit Test
public class TestNGTest6_1_0 { @Test @Parameters(value="number") public void parameterIntTest(int number) { System.out.println("Parameterized Number is : " + number); } }
XML File
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" > <suite name="My test suite"> <test name="testing"> <parameter name="number" value="2"/> <classes> <class name="com.fsecure.demo.testng.TestNGTest6_0" /> </classes> </test> </suite>
@DataProvider for parameterized test.
While pulling data values into an XML file can be quite handy, tests occasionally require complex types, which can’t be represented as a String or a primitive value. TestNG handles this scenario with its @DataProvider annotation, which facilitates the mapping of complex parameter types to a test method.
@DataProvider for Vector, String or Integer as parameter
@Test(dataProvider = "Data-Provider-Function") public void parameterIntTest(Class clzz, String[] number) { System.out.println("Parameterized Number is : " + number[0]); System.out.println("Parameterized Number is : " + number[1]); } //This function will provide the patameter data @DataProvider(name = "Data-Provider-Function") public Object[][] parameterIntTestProvider() { return new Object[][]{ {Vector.class, new String[] {"java.util.AbstractList", "java.util.AbstractCollection"}}, {String.class, new String[] {"1", "2"}}, {Integer.class, new String[] {"1", "2"}} }; }
@DataProvider for object as parameter
P.S “TestNGTest6_3_0” is an simple object with just get set method for demo.
@Test(dataProvider = "Data-Provider-Function") public void parameterIntTest(TestNGTest6_3_0 clzz) { System.out.println("Parameterized Number is : " + clzz.getMsg()); System.out.println("Parameterized Number is : " + clzz.getNumber()); } //This function will provide the patameter data @DataProvider(name = "Data-Provider-Function") public Object[][] parameterIntTestProvider() { TestNGTest6_3_0 obj = new TestNGTest6_3_0(); obj.setMsg("Hello"); obj.setNumber(123); return new Object[][]{ {obj} }; }
TestNG’s parameterized test is very user friendly and flexible (either in XML file or inside the class). It can support many complex data type as parameter value and the possibility is unlimited. As example above, we even can pass in our own object (TestNGTest6_3_0) for parameterized test
7) Dependency Test
The “Parameterized Test” means methods are test base on dependency, which will execute before a desired method. If the dependent method fails, then all subsequent tests will be skipped, not marked as failed.
JUnit 4
JUnit framework is focus on test isolation; it did not support this feature at the moment.
TestNG
It use “dependOnMethods “ to implement the dependency testing as following
@Test public void method1() { System.out.println("This is method 1"); } @Test(dependsOnMethods={"method1"}) public void method2() { System.out.println("This is method 2"); }
The “method2()” will execute only if “method1()” is run successfully, else “method2()” will skip the test.
TestNG’s trick of skipping, rather than failing, can really take the pressure off in large test suites. Rather than trying to figure out why 50 percent of the test suite failed, we can concentrate on why 50 percent of it was skipped! Better yet, TestNG complements its dependency testing setup with a mechanism for rerunning only failed tests.
Conclusion
After go thought all the features comparison, i suggest to use TestNG as core unit test framework for Java project, because TestNG is more advance in parameterize testing, dependency testing and suite testing (Grouping concept). TestNG is meant for high-level testing and complex integration test. Its flexibility is especially useful with large test suites. In addition, TestNG also cover the entire core JUnit4 functionality. It’s just no reason for me to use JUnit anymore.
Reference
TestNG
————
http://en.wikipedia.org/wiki/TestNG
http://www.ibm.com/developerworks/java/library/j-testng/
http://testng.org/doc/index.html
http://beust.com/weblog/
JUnit
———–
http://en.wikipedia.org/wiki/JUnit
http://www.ibm.com/developerworks/java/library/j-junit4.html
http://junit.sourceforge.net/doc/faq/faq.htm
http://www.devx.com/Java/Article/31983/0/page/3
http://ourcraft.wordpress.com/2008/08/27/writing-a-parameterized-junit-test/
TestNG VS JUnit
——————
http://docs.codehaus.org/display/XPR/Migration+to+JUnit4+or+TestNG
http://www.ibm.com/developerworks/java/library/j-cq08296/index.html
http://www.cavdar.net/2008/07/21/junit-4-in-60-seconds/
发表评论
-
org.hibernate.QueryException: in expected错误
2011-10-20 16:18 1586错误信息:org.hibernate.QueryExcepti ... -
Oracle 是不支持主键自增
2011-04-23 08:59 1311今天遇到一个问题,以为oracle 是支持主键自增的,结果程序 ... -
hibernate 插入Date字段 时分秒丢失
2011-04-23 08:51 2256Oracle 中如果字段类型是Date,在使用JBoss JP ... -
JBoss启动异常: Port already in use: 1098
2010-05-11 12:44 5824JBoss启动异常: Port already in use: ... -
caused by: java.lang.NoSuchMethodError: javassist.scopedpool.ScopedClassPool.
2010-03-31 14:27 2576今天用testng 跑case时发现 caused b ... -
java.sql.SQLException: ORA-00911: 无效字符
2010-02-01 16:25 5840今天在用原生sql, jdbc操作数据库时老有问题: java ... -
用Artifactory管理内部Maven仓库
2009-12-31 23:27 9101来源:http://forearrow.iteye.com/b ... -
Hudson : pending-waiting for next available executor问题
2009-12-24 10:58 4218今天上午发现测试用的job没有起来, 通过hudson 的we ... -
这几天跑持续集成的Function Test遇到的问题s
2009-12-17 17:37 2771这几天在弄持续集成功能测试的东西,遇到了两三个奇怪的问题,真的 ... -
Maven Profiles
2009-12-09 17:32 1649From: http://www.sonatype.co ... -
maven依赖关系中Scope的作用
2009-12-09 16:24 1427转载:http://peak.iteye.com/blog/2 ... -
mvn install:install文件名中需要不出现空格
2009-12-07 10:01 1969mvn install:install-file -Dgrou ... -
mvn 长命令
2009-12-06 10:08 1052mvn archetype:create -DgroupId ... -
JPA Query 注意事项一
2009-11-11 18:33 36511. 错误: org.hibernate.QueryPara ... -
java.lang.IllegalArgumentException: id to load is required for loading
2009-11-11 16:08 2100java.lang.IllegalArgumentExcept ... -
'Starting JBoss v5.0 at localhost' has encountered a problem
2009-11-05 22:04 4846eclipse中启动jboss5时提示:Server JBos ... -
Supported Values for @SuppressWarnings
2009-11-03 16:22 2428http://www.breakitdownblog.com/ ...
相关推荐
【追求代码质量:Junit4与TestNG的对比】 在软件开发中,代码质量的保证是至关重要的,而单元测试是确保代码质量的一种有效手段。本文主要探讨了两个流行的Java测试框架——JUnit 4和TestNG,它们都是用于编写和...
在搭建 Selenium 终极自动化测试环境时,需要安装 JDK、Eclipse、Junit、TestNG 和 Python 等软件。其中,JDK 是 Java 开发工具包,Eclipse 是一个集成开发环境,Junit 和 TestNG 是测试框架,Python 是一种流行的...
TestNG和JUnit是两种广泛使用的Java测试框架,它们在软件测试领域扮演着至关重要的角色。本文将对两者进行深入的比较和概要介绍,旨在帮助读者理解它们的相似之处以及如何选择适合自己的测试工具。 首先,TestNG和...
标题与描述均提到了"Testing-JUnit-TestNG",这明确指向了软件测试领域中的两个重要的测试框架:JUnit和TestNG。这两个框架在Java开发社区中被广泛使用,用于编写自动化测试用例,以确保代码质量和功能正确性。 ###...
除了基本的测试功能,JUnit4还包含了一些扩展框架,如TestNG和Mockito,它们能帮助我们进行更复杂的测试场景。TestNG提供了更丰富的测试注解和并行测试支持,而Mockito则是一个强大的模拟框架,可以帮助我们创建和...
4. TestNG:TestNG 是一个更全面的测试框架,比 JUnit 更加强大且灵活,它支持单元测试、集成测试、功能测试和端到端测试。TestNG 引入了诸如并行测试、测试分组、依赖注入、配置方法等特性,可以满足大型项目的复杂...
TestNG和JUnit是两种广泛使用的Java测试框架,它们在软件质量保证和持续集成流程中扮演着重要角色。本文将深入探讨这两个测试框架的特性和差异,以及为什么TestNG被誉为下一代测试框架。 TestNG是由Cedric Beust...
JUnit4的设计使其易于与其他库和框架集成,例如Mockito用于模拟对象,PowerMock用于测试静态方法和构造函数,或是TestNG提供更高级的测试功能。 ## 五、持续集成与持续测试 在持续集成环境中,JUnit4可以与Jenkins...
创建一个新的 Java 项目,在 Eclipse 中新建一个 Java 项目,添加 Junit 4 和 Selenium 相关的 Jar 包,然后创建一个新的包和类,以便编写自动化测试脚本。 相关知识点 * Selenium 自动化测试框架的搭建 * JDK 的...
JUnit4测试框架是Java开发中广泛使用的单元测试工具,它为开发者提供了编写和运行可重复、可靠的测试用例的能力。这个“junit4测试jar包”包含了一切你需要在项目中集成JUnit4进行测试的库文件。只需将其复制到你的...
JUnit4是Java编程语言中最广泛使用的单元测试框架之一,它为开发者提供了编写可重复执行、易于维护的测试代码的能力。这个“junit4 jar包”包含了运行JUnit4测试所必需的库文件,主要包括两个核心组件:`junit-4.11....
### JUnit4 学习知识点详解 #### 一、JUnit4 概述 JUnit4 是 JUnit 测试框架的一个重大更新版本,它充分利用了 Java 5 的注解(Annotation)特性来简化测试用例的编写过程。注解是一种元数据,用于描述程序中的...
- **TestNG**: TestNG是一个基于JUnit和NUnit思想设计的新一代测试框架,提供了更丰富的特性和灵活性,包括但不限于并行测试执行、灵活的依赖管理、更强大的参数化支持等。TestNG相比JUnit在测试控制、重复执行、...
终极自动化测试环境搭建:Selenium+Eclipse+Junit+TestNG+Python 本文旨在指导读者搭建一个终极自动化测试环境,利用 Selenium+Eclipse+Junit+TestNG+Python 实现自动化测试。下面是详细的搭建过程: 一、安装 JDK...
JUnit4是Java编程语言中最广泛使用的单元测试框架之一,它为开发者提供了强大的工具来编写、组织和执行单元测试。JUnit4引入了许多改进和新特性,极大地提升了测试的灵活性和效率。下面将详细介绍JUnit4的关键概念、...
### Selenium+Eclipse+JUnit+TestNG自动化测试学习笔记 #### 一、环境搭建与配置 **1. 安装 JDK** - **版本**: JDK 1.7 - **下载地址**: ...
赠送jar包:powermock-module-junit4-2.0.9.jar; 赠送原API文档:powermock-module-junit4-2.0.9-javadoc.jar; 赠送源代码:powermock-module-junit4-2.0.9-sources.jar; 赠送Maven依赖信息文件:powermock-...