It is a framework developed with advanced features in it. Similar to Junit and Nunit frameworks, it covers a wide range of tests - unit, functional, end-to-end, integration etc. NG stands for next generation.
Few noticeable features of this framework are :
@test
public static void main()
{
System.out.println("test case 1");
}
@aftermethod
public static void main()
{
System.out.println("will execute after every method");
}
@beforetest
public static void main()
{
System.out.println("this will execute before the test");
}
The output will be as follows :
this will execute before the test
test case 1
will execute after every method
In the above example we have used a very simple strategy to show the flow of execution of the code on the basis of annotations. The "@before" annotation will execute first, then the "@test", and finally the “@after” annotation. If we declare a set of “before” annotations here, they will be the ones to be executed first followed by "test" annotation methods and then the "after" annotations.
TestNG has a list of annotations in it. These are :
eg.
@DataProvider(name="provider1")
public Object[][] createdata()
return new Object[][]{
{ "Alex" , new Integer(36) },
{ "Robin", new Integer(37) }
};
}
@Test(dataProvider="provider1")
public static verify(String n1,String n2)
{
System.out.println(n1+" "+n2);
}
The above code snippet shows how "DataProvider" annotation works. Let's understand in detail.
1. The first method, that is, createData(), is the first method which shall execute. The Object[][] returns the data that is passed to it.
2. The verify() method gets the data from the dataprovider,i.e, provider1.
The use of annotations are primarily to prioritize the flow of execution of various tests.
While we had test frameworks like Junit and Nunit, what provoked one to shift focus to another testing tool? The answer is simple. TestNG has a number of such features which facilitates ease of use. It has support for easy to use annotations which helps to prioritize the tasks in a systematic manner, it allows concurrent execution of test scripts, test case dependencies can be set.
TestNG can be used in Eclipse IDE as follows :
If you wish to create a sample TestNG project, then it can be done by selecting preferences in the Window tab, and select TestNG. After that create a Java Project, click on Add library and include the TestNG file. This way you can explore the TestNG framework and discover the hidden facts by yourself.
Advertisement: