Most of the Automation tester are using TestNG Framework just because of its advantages and more supported features. Its similar to JUnit framework but this Framework overcome the limitation of JUnit Framework.
NG Stands for "Next Generation" in TestNG.
Some of the TestNG Farmework features are listed below those are not exists in JUnit.
1. We can perfrom the Parallel Testing with TestNG Framewok.
2. We can perform data driven testing with the use of @DataProvider annotation.
3. We can see the HTML Results with TestNG Framework.
Annotations of TestNG are :
@AfterSuite, @BeforeClass, @AfterClass, @BeforeTest, @AfterTest, @BeforeGroups, @AfterGroups, @BeforeMethod, @AfterMethod, @DataProvider, @Factory, @Listeners, @Parameters, @Test.
Installation of TestNG:
TestNG is not an in build framework in eclipse we have to download it.
Steps to Download:
1. Open the Eclipse and select the Eclipse Marketplace option under the Help option of eclipse
2. It will show the list of various software that can be downloaded in eclipse
3. Search the TestNG option and Install it by clicking on the Next button
4. Once you have been installed please verify its downloaded or not, just go to the other Option of ShowView
Window-->Show View-->Other...
5.TestNG option should be displayed under the Java Option.
Practical Time:
Create the Project of TesNG in your eclipse and add the TestNG Library and write the first program.
1. If we want to execute the test case in TestNG then @Test annotation is used to do that, for example.
C:\Users\zeeshan.khan\AppData\Local\Temp\testng-eclipse--603972738\testng-customsuite.xml
This is the Test1
PASSED: Test1
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
2. If we have more that one test cases and we want to execute those test cases as priority wise then code will be :
NG Stands for "Next Generation" in TestNG.
Some of the TestNG Farmework features are listed below those are not exists in JUnit.
1. We can perfrom the Parallel Testing with TestNG Framewok.
2. We can perform data driven testing with the use of @DataProvider annotation.
3. We can see the HTML Results with TestNG Framework.
Annotations of TestNG are :
@AfterSuite, @BeforeClass, @AfterClass, @BeforeTest, @AfterTest, @BeforeGroups, @AfterGroups, @BeforeMethod, @AfterMethod, @DataProvider, @Factory, @Listeners, @Parameters, @Test.
Installation of TestNG:
TestNG is not an in build framework in eclipse we have to download it.
Steps to Download:
1. Open the Eclipse and select the Eclipse Marketplace option under the Help option of eclipse
2. It will show the list of various software that can be downloaded in eclipse
3. Search the TestNG option and Install it by clicking on the Next button
4. Once you have been installed please verify its downloaded or not, just go to the other Option of ShowView
Window-->Show View-->Other...
5.TestNG option should be displayed under the Java Option.
Practical Time:
Create the Project of TesNG in your eclipse and add the TestNG Library and write the first program.
1. If we want to execute the test case in TestNG then @Test annotation is used to do that, for example.
import org.testng.annotations.*; public class TestNG1 { @Test() public void Test1() { System.out.println("This is the Test1"); } }
OutPut:
[TestNG] Running:C:\Users\zeeshan.khan\AppData\Local\Temp\testng-eclipse--603972738\testng-customsuite.xml
This is the Test1
PASSED: Test1
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
2. If we have more that one test cases and we want to execute those test cases as priority wise then code will be :
import org.testng.annotations.*; public class TestNG1 { @Test(priority=2) public void Test1() { System.out.println("This is the Test1"); } @Test(priority=1) public void Test2() { System.out.println("This is the Test2"); } }
Output:
[TestNG] Running:
C:\Users\zeeshan.khan\AppData\Local\Temp\testng-eclipse--651145255\testng-customsuite.xml
This is the Test2 // Execute the Test 2 First because we have
This is the Test1 provided its priority as "1"
PASSED: Test2
PASSED: Test1
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
3. If we want to perform the Datadriven Testing then will
have to use annotation@DataProvider.
import org.testng.annotations.*; public class TestNG1 { @Test(priority =1,dataProvider="dataextract") public void setData(String userid, String pwd) { System.out.println("you have provided username as::"+userid); System.out.println("you have provided password as::"+pwd); } @DataProvider public Object[][] dataextract() { //Rows - Number of times your test has to be repeated. //Columns - Number of parameters in test data. Object[][] data = new Object[3][2]; // 1st row data[0][0] ="Tester1"; data[0][1] = "123456"; // 2nd row data[1][0] ="Tester2"; data[1][1] = "64665456"; // 3rd row data[2][0] ="Tester3"; data[2][1] = "655435345"; return data; } }
Output:
[TestNG] Running:
C:\Users\zeeshan.khan\AppData\Local\Temp\testng-eclipse-354263273\testng-customsuite.xml
you have provided username as::Tester1 // Extracts all the value from the DataProvider
you have provided password as::123456
you have provided username as::Tester2
you have provided password as::64665456
you have provided username as::Tester3
you have provided password as::655435345
PASSED: setData("Tester1", "123456")
PASSED: setData("Tester2", "64665456")
PASSED: setData("Tester3", "655435345")
===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
TestNG generated own results and its displayed as below:
4. HTML Result is generated after the TestNG Script under the
test-output folder, look into the below screenshot.
Navigate to this folder and open the "index.html" by getting actual path of this
location(We can get this location from below shown window)
HTML Result look like as below shown picture.