Monday, 11 March 2013

TestNG vs JUnit


Here are some features of TestNG which is not available in Junit
1. Method Dependency: In Junit execution of test methods is depends on order of string literals in method name. Its hard to maintain the ordering and its not visible if there is any dependency of one method on other method. On the Other hand in TestNG you can mention dependency using dependsOnMethods.
@Test ( dependsOnMethods = "testPreSettlement" )
public void testSettlement(){ ... }

In this case if first method fails, second method doesn't go in fail status but you will see it as skipped.

2. Parametric Testing (DDT): As data driven test framework TestNG  has feature of parametric testing where you can pass parameter to a test method using configuration. Junit needs more code to use this feature.
@testng.parameters value="param1, param2"
public void testSettlement(){ ... }

You can configure this in test suite file as below:
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd">
<suite name="settlement">
 <test name="testSettlement">
  <parameter name="param1" value="Admin"/>
  <parameter name="param2" value="2"/>
  <classes> 
   <class name="co.uk.SettlementTest"/>
  </classes>
 </test> 
</suite>

3. Ordering/Prioritiesing Test cases: In TestNG you can use Priority annotation to put ordering between test cases.
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Priority { int value() default 0; }

For Example
@Priority(-1)
@Test
public void testSettlement(){ ... }

4. Grouping Tests and Repeatability:  In TestNg you can group tests, even configure number of times you want to execute particular test case. You can also set timeout for executing test method.
@Test(threadPoolSize = 4, invocationCount = 5,  timeOut = 10000, groups = { "functional"})

No comments:

Post a Comment