Saturday 13 December 2014

How to upload a file using selenium webdriver

Scenario:

Using selenium webdriver, one can simulate fileupload easily using sendKeys.

Using firebug, I could see, the button which pops up operating system specific dialog box where file loaction is typed-in has got type=file.

The below is snippet from naukri site, which has got button to upload file.

<input type="file" name="attachCV" id="attachCV" valtype="attachCV" class="">

In case, input type="file", we can pass absolute location of file to be uploaded using sendKeys method exposed by WebElement class.

Ex:
WebDriver wd = new FirefoxDriver();
WebElement we = wd.findElement(By.id = "attachCV");
we.sendKeys(<Absolute FileLocation>);

Internationalization Testing Using WebDriver

Internationalization Testing Using WebDriver
One need to open browser in localized mode, in specific language.

there could be 2 approaches:
i. create profile and load it during launching browser.
    --ProfileIni pf = new ProfileIni();
    --FirefoxProfile fireFoxProfile = pf.getProfile(<profileName>);//<profileName is created using profile manager, "type firefox.exe -p on Windows+R "
 
    --WebDriver wd = new FireFoxDriver(fireFoxProfile);
ii. Create object of FirefoxProfile. Set the preference of FirefoxProfile to open in particular langauage.
--FireFoxProfile fireFoxProfile = new FireFoxProfile();
--fireFoxProfile.setPreference("intl.accept_languages","sl");
--WebDriver wd = new FireFoxDriver(fireFoxProfile);

package learn.I18n;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
/*
Internationalization Testing Using WebDriver
One need to open browser in localized mode, in specific language.

there could be 2 approaches:
i. create profile and load it during launching browser.
    --ProfileIni pf = new ProfileIni();
    --FirefoxProfile fireFoxProfile = pf.getProfile(<profileName>);//<profileName is created using profile manager, "type firefox.exe -p on Windows+R "
 
    --WebDriver wd = new FireFoxDriver(fireFoxProfile);
ii. Create object of FirefoxProfile. Set the preference of FirefoxProfile to open in particular langauage.
--FireFoxProfile fireFoxProfile = new FireFoxProfile();
--fireFoxProfile.setPreference("intl.accept_languages","sl");
--WebDriver wd = new FireFoxDriver(fireFoxProfile);



Profile setting can be done using ProfileIni Class in WebDriver
*/
public class Localization implements Constants
{

WebDriver wd = null;

@BeforeTest(enabled = true)
public void setup()
{
/*
ProfilesIni pf = new ProfilesIni();
FirefoxProfile fireFoxProfile = pf.getProfile(profileName);
wd = new FirefoxDriver(fireFoxProfile);
*/
FirefoxProfile fireFoxProfile = new FirefoxProfile();
fireFoxProfile.setPreference("intl.accept_languages","sl");
wd = new FirefoxDriver(fireFoxProfile);
}

@Test
public void testLoadApplication()
{
wd.get("http://www.gmail.com");

}


}

Sunday 7 December 2014

Good site For Agile Software Development



http://scrummethodology.com/
http://www.agilebuddha.com/agile/how-to-do-effective-capacity-planning-on-the-scrum-team/

Saturday 6 December 2014

Sprint Cycle


Traversing Linked List In Reverse Order

Traversing Linked List In Reverse Order:

package linkedList;

import java.util.List;

public interface LinkedListInterface<T>
{
void add(T t);
List<T> readNodeValues();
List<T> readNodeValuesInReverseOrder();
}


/********************************************/

package linkedList;

import java.util.ArrayList;
import java.util.List;

public abstract class AbstractLinkedListImpl<T> implements LinkedListInterface<T>
{
protected Node start = null;
static class Node
{
private Node next = null;
private Object value;

}

@Override
public void add(T t) {
Node n = new Node();
n.value = t;
if(this.start == null)
{
this.start = n;
}
else
{
//create temp node to point to start
Node temp = this.start;
//traverse to end of linked list whre next = null
while(temp.next !=  null)
{
temp = temp.next;
}
temp.next = n;
}
}

@Override
public List<T> readNodeValues() {
List<T> list = null;
while(this.start!=null)
{
list = new ArrayList<T>();
list.add((T)start.value);
this.start=this.start.next;
}

return list;
}

@Override
public List<T> readNodeValuesInReverseOrder() {

List<T> list = null;
traverse(this.start);
return list;
}

private void traverse(Node start)
{

if(start == null)
{
return;
}
traverse(start.next);
System.out.println(start.value);


}

}

/********************************/
package linkedList;

public class MyLinkedList<T> extends AbstractLinkedListImpl<T>
{

public MyLinkedList()
{
this.start = null;
}

public static void main(String [] str)
{
MyLinkedList<String> linkedList = new MyLinkedList<String>();
linkedList.add(new String("Debasish"));
linkedList.add(new String ("Subhasish"));
linkedList.add(new String ("Krishna"));
linkedList.add(new String ("Kanha"));
linkedList.readNodeValuesInReverseOrder();
}
}

Project Development Vs Car Driving

Nice article i came across @: https://www.scrumalliance.org/community/articles/2014/february/velocity


What is velocity?

The truth is: Velocity is velocity. And velocity is measured . . . as velocity is.

How do you measure your velocity while driving? (Imagine the speedometer is broken.) You've been driving for the last two hours, you've gone 160 kilometers, so you know your average velocity is 80 km per hour.

If your final destiny is about 200 km away from your starting point:
  • You know you will likely get there in 30 minutes, or
  • If you stop now you have already gone 160 km, or
  • If you need to stop and fuel up in 15 minutes, you can say you've done 180 km.
The same goes for projects: Team velocity is the rate at which a team delivers stories from the product backlog. If you know your velocity, you'll have an idea about:
  • How much value you've delivered until now (in story points and done user stories), and
  • When you'll be able to deliver all user stories in the product backlog, and
  • How many story points will you be able to deliver by a certain date.

How do we calculate velocity?

Simple! We do some basic math.

Scenario: Our team delivers 3 user stories. The sum of the story points equals 20. Our velocity is then 20.

If, in the next iteration, our team delivers 30 story points, then our average velocity is 25, or (20 SP + 30 SP) divided by 2 iterations = 25 SP.


Figure 1: Awesome handmade velocity chart

Velocity is the number of story points completed by a team in an iteration.

Why do we need it?

We need velocity to:
  • Predict how much scope can be delivered by a specific date
  • Predict a date for a fixed amount of scope to be delivered
  • Understand our limits while defining the amount of scope we will commit for a sprint

What influences velocity?

As in a car trip, there are factors that may influence our velocity:
  • Roadblocks -- aka impediments
  • Fuel -- motivation, what drives us
  • Driver experience -- knowledge/expertise/competence developer
  • Car conditions -- dev environment
  • Visibility -- project transparency
  • Directions -- project objectives
  • Traffic/driving rules -- processes
  • Destination -- product
So think: If all those things are in bad shape, or without proper definition, then your velocity decreases. The same applies to projects.

Can I count in an incomplete user story?

No, you shouldn't. Incomplete is undone. Velocity is about finished, delivered user stories. Here are some reasons why you shouldn't count incomplete user stories:
  • We can't really figure out how much is ready and how much is not; it will be a wild guess.
  • We may be led by a false sense of accuracy, due to the use of fractional numbers.
  • Incomplete means the user story still has no value for the customer.

So what should I do with those user stories?

Break the user stories into smaller ones. That way it will be easier for you to add them to a sprint and manage to deliver them.

Teams and their velocity

Should bugs and maintenance be considered in velocity estimation?

Yes, if you estimate their size in story points. It's work delivered, right? Although with no direct value to the customer, this work is indirectly associated with value delivered. The proper way to handle this is: Don't create bugs(!). Use zero-bugs & prio1-fix-bugs policies. Whenever a bug pops up, finish it off!

Should I compare the velocity of different teams?

No.

Why not? Well, because we work with story points. Therefore we work with relative estimation; we compare user stories with each other, having as our basis a reference user story that the team picked up. The story points of that reference user story could be anything. A team could say that the reference user story is equal to 400 SP or equal to 2 SP. It actually doesn't matter, because we work with relative estimates.

The goal is not the number itself -- the goal is to use this user story as a reference so we can estimate other user stories by comparison.

So, how would we compare velocity in this case?

Team A is delivering 800 SP and Team B is delivering 30 SP. Does this mean A is better than B?

I don't know! And I don't care. Each team is comparable only to itself. Velocity is not used as a measure as if we were racing against each other. Velocity is used to help us improve our own timing -- to help us get better and better and speed up, compared only to ourselves.

What about teams working with the same product backlog?

This is a different case. It is nonsense to have teams using different units of comparison for the user stories in the same product backlog. We use story points to predict how much scope we'll be able to deliver by a certain date, or to predict when we may be able to deliver the scope. So if we have more than one team working on a backlog item and they use different sizes for the user story, we won't be able to make this prediction.

In this case, teams must get together and define a reference user story. Afterward they should decide on the size of that user story in story points. Then, in every refinement, the teams (or their representatives) will estimate, using the same user story and the same size for comparison.

Then, in this case, can I compare teams?

Nope. Why would you? Maybe some are delivering fewer story points because those user stories were not well estimated compared to others. Or maybe that team is helping other teams or paying more attention to testing, refactoring, coding standards. . . . We don't know, do we? You should compare a team with itself, and dig out why the team is slowing down (if it is). Probably you'll see there are several impediments you need to take care of.

Each car has its own characteristics. We need to get the best from each, knowing that if we push too hard, we will probably break the engine.

Help teams excel, don't punish them.

Humans are humans, and we are (gladly) different from each other. You'll always have teams performing differently from each other -- and that's not necessarily bad. We just need to find out each team's own sustainable pace and keep them moving, driving our company further.
- See more at: https://www.scrumalliance.org/community/articles/2014/february/velocity#sthash.05PVtrcO.dpuf

Thursday 29 May 2014

How to extract text from PDF Using Apache Tika Library In Java

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.tika.parser.pdf.PDFParser;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.ContentHandler;

public class ParsePDFWithTika {
  public static void main(String args[]) throws Exception {

    InputStream is = null;
    try {
      is = new FileInputStream("C:/Temp/realhowto-vbs-20121221.pdf");
      ContentHandler contenthandler = new BodyContentHandler();
      Metadata metadata = new Metadata();
      PDFParser pdfparser = new PDFParser();
      pdfparser.parse(is, contenthandler, metadata, new ParseContext());
      System.out.println(contenthandler.toString());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
        if (is != null) is.close();
    }
  }
}

Tuesday 27 May 2014

Integration Of ReportNG With TestNG

ReportNG is third party reporting tool which can be plugged in with TestNG as listener.

ReportNG can be downloaded from http://reportng.uncommons.org/.

From this site, 2 jars are to be downloaded for use:
1. reportng-1.1.4.jar
2. velocity-dep-1.4

But, to run with testng-6.8.jar, those jar needs class "com.google.inject", which can be found in guice.jar.
This can be downloaded from https://code.google.com/p/google-guice/wiki/Guice40.

All 3 jars need to be in classpath. In below target "runTestNGTestCase, classpathref points to classpath which includes all classfiles and jars".

listeners attribute should be added with classnames separated by comma.
listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter">
So, target for running testNG with reportNG should look like as below:

<target name="runTestNGTestCase">
<testng classpathref="classes.test.location" outputDir="test-output" useDefaultListeners="true" listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter">
<classfileset dir="bin" includes="**/TestFlipKarHomePage.class" />
 <sysproperty key="org.uncommons.reportng.title" value="My Test Report"/>
</testng>
</target>

Monday 26 May 2014

Working Example Of DragAndDrop Using WebDriver

WebDriver uses Actions class to drag and drop.
Actions class is found in package org.openqa.selenium.interactions

There are 2 methods related to drag and drop:
1. dragAndDropBy(WebElement elment,int x, int y)
//This will drag WebElement element to location is defined by x and y coordinate.
2. dragAndDrop(WebElement a, WebElement b)
//This will drag WebElement a and drop to WebElement b


Working Example:

// Initializing FirefoxDriver
 WebDriver driver = new FirefoxDriver();
 /* Loading the site on which we need to perform the
  drag and drop action.*/
 driver.get("http://jqueryui.com/droppable/");
 driver.manage().window().maximize();
 // Waiting for element to load
 Thread.sleep(5000);
 /* Drag and drop features are usually part of the iframes.
 So we first need to switch to the iframe so that selenium
 can locate the web element.*/
 List<WebElement> frames=driver.findElements(By.tagName("iframe"));

 driver.switchTo().frame(frames.get(0));
 //WebElement box = driver.findElement(By.xpath("html/body/div[1]"));
 WebElement box = driver.findElement(By.id("draggable"));

 /* Creating the object of Actions class and passing the
  driver to it. It should be noted that Actions(WebDriver driver)
  is an argument constructor which take driver as argument.*/
 Actions act = new Actions(driver);
 act.dragAndDrop(box, driver.findElement(By.id("droppable"))).build().perform();
 driver.switchTo().defaultContent();
 driver.close();

How to call testNG using Ant

<taskdef resource="testngtasks" classpath="testng.jar" />


<project name="runTestNG" basedir="." default="runTestNGTestCase">
<!--define your location of testng-6.8.jar--->
<path id="classpath.testNG">
<pathelement location="lib/selenium-2.39.0/libs/testng-6.8.jar"/>
</path>

<!--testng is external task...so, we need to define it using taskdef-->
<taskdef resource="testngtasks" classpathref="classpath.testNG"/>

<!--path declaration for all jars to be used for compilation and execution-->
<path id="lib.test">
<fileset dir ="lib">
<include name="**/*.jar"/>
</fileset>
</path>

<!--classpath declaration -->
<path id="classes.test.location">
<path location="bin" />
<fileset dir="lib" includes="**/*.jar" />
</path>

<!-- for compilation-->
<target name="compileFlipKart">
<delete dir="bin" includeEmptyDirs="true" failonerror="false"/>
<mkdir dir="bin" />
<javac srcdir="src" destdir="bin" classpathref="lib.test" />
</target>

<!--for execution of program with main method-->
<target name="runRunner" depends="compileFlipKart" >
<java fork="true" classname="self.learning.automate.test.Runner">
<classpath>
<path refid="lib.test" />
<path location="bin" />
</classpath>
</java>
</target>

<!--execution of test class using testNG-->
<target name="runTestNGTestCase">
<testng classpathref="classes.test.location" outputDir="test-output" useDefaultListeners="true" listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter">
<classfileset dir="bin" includes="**/TestFlipKarHomePage.class" />
 <sysproperty key="org.uncommons.reportng.title" value="My Test Report"/>
</testng>
</target>
</project>

Saturday 17 May 2014

How To Search Text On XML, Ignoring Case

Lets take example:
XML:
<Names>
<Name>Debasish</Name>
<Name>Subhasish</Name>
</Names>

What would be xpath to search Node with text "Debasish".
The xpath=//Name[contains(text(),'Debasish')].
The outcome=Element='<Name>Debasish</Name>'

Lets see the outcome when xpath=//Name[contains(text(),'debasish')].
There is no match. (I have used online tool http://www.freeformatter.com/).

Now, what can i do to search text irrespective of its case.
One general solution, we normally use in programming is to either convert text to lower case or upper case and then compare.
Here, in case of xpath, we will do same.
The xpath would be: //Name[contains(lower-case(text()),'debasish')]
The outcome=Element='<Name>Debasish</Name>'

XML And XPath...Is XML Case Sensitive

Lets See What Is DOM.
DOM stands for document object model which is interpreted by browser and presents the DOM in better human understandable html format. DOM is hierarchy structure of webelements.

Lets see simple DOM presented in html format.

<html><head><body><tr><td><input type="text" value="Debasish" /></td><td><input type="text" value="Subhasish" /></td></tr></body></head></html>.

If you see above html, it will look like XML. Only difference between html and XML is html has fixed set of tags and tags are not case sensitive. Does not matter, if one writes "tr" or "Tr" or "TR". Its all same for html.  But, XML tags are case sensitive. So, in general, XML does have user defined tags which is case sensitive(CASE SENSITIVE) where as html tags are not case sensitive(CASE INSENSITIVE).

Impact Of XPath On:

DOM: outcome of //input is same as //Input. The output is same as //INPUT. So, xpath in case of DOM is not case sensitive.  But, attriubtes such as id,name are case sensitive.
XPath Operators Are Case Sensitive:
But, xpath operators are case sensitive.
Ex: In above case, what will be xpath to look for inputbox with value=Debasish. The xpath would be as shown below.
//Input[contains(@value,'Debasish')]
As shown above. "contains" is the operator which searches for Input box with value="Debasish".
If  "contains" is written as "Contains"(capital C), it will search nothing. Rather, it will syntax error.
So, it confirms xpath operators are case sensitive.

XML: <Names><Name>Debasish</Name><Name>Subhasish</Name></Names>
<Name> is not same as <name>. <Name> is different node and <name> is different node.
outcome of xpath=//Name will return two nodes/Elements
<Name>Debasish</Name>
<Name>Subhasish</Name>

If XML is: <Names><Name>Debasish</Name><name>Subhasish</name></Names>
outcome of xpath=//Name will return one node/Element
<Name>Debasish</Name>
outcome of xpath=//name will return one node/Element
<name>Subhasish</name>

This confirms, XML is case sensitive. So, as xpath on those xmls.



Junit Vs TestNG

Here I did a feature comparison between JUnit 4 and TestNG.
junit-vs-testngjpg

1. Annotation Support

The annotation supports are implemented in both JUnit 4 and TestNG look similar.
FeatureJUnit 4TestNG
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");
}
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? TestiNG is easier to understand, it uses “BeforeMethod”, “AfterMethod” and “ExpectedException” instead.

2. Exception Test

The “exception testing” means what exception throws from the unit test, this feature is 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 is 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.

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.


1. @test annotation can take dependsOnMethods,dependsOnGroups. This causes dependent method is called first, then test method is called.
Example:
@test
public void test1()
{
}
public void test2(dependsOnMethods test1)
{
}
This will cause test2 to follow after test1. If test1 fails, then test2 will skip.
@test
public void test3(groups={sanity})
{
}
public void test4(dependsOnMethods test3)
{
}
if test 3 fails, then test4 will skip

2. DataProvider which is used for parameterization, is easy to configure. Junit implementation is bit complex.