Every Testing framework have some annotation to run each and every unit test.TestNG is inspired by JUnit so it's annotation have some similarity with JUnit and also have some extra feature by which TestNG is more robust and can overcome the difficulties of JUnit.
Here we can start the learn some annotation of TestNG and later we know some TestNG xml configuration by which we can run multiple test with the one configuration file.
Let's start to know some Annotation of TestNG
In below a sample code example is given
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package excelfilereadapachepoi;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
*
* @author Administrator
*/
public class TestNGAnotationClass1 {
@BeforeSuite
public void beforeSuite() {
System.out.println("!.......BeforeSuite From Class TestAnnotation.......!");
}
@BeforeClass
public void beforeClass() {
System.out.println("!.......BeforeClass From Class TestAnnotation.......!");
}
@BeforeTest
public void beforeTest() {
System.out.println("!.......BeforeTest From Class TestAnnotation.......!");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("!.......BeforeMethod From Class TestAnnotation.......!");
}
@DataProvider(name = "dp")
public static Object[][] dataprovider() {
return new Object[][]{{1, "Test 1"}, {1, "Test 2"}};
}
@Test(dataProvider = "dp")
public void dataConsumer(int i, String j) {
System.out.println("This INTEGER data " + i + " Consumed and " + " This STRING data " + j + " Consumed");
}
@AfterSuite
public void afterSuite() {
System.out.println("!.......AfterSuite From Class TestAnnotation.......!");
}
@AfterClass
public void afterClass() {
System.out.println("!.......AfterClass From Class TestAnnotation.......!");
}
@AfterMethod
public void afterMethod() {
System.out.println("!.......AfterMethod From Class TestAnnotation.......!");
}
@AfterTest
public void afterTest() {
System.out.println("!.......AfterTest From Class TestAnnotation.......!");
}
@BeforeGroups(groups = {"Group-One", "Group-Two"})
public void beforeGroup() {
System.out.println("!.......BeforeGroups From Class TestAnnotation.......!");
}
@Test(groups = "Group-One")
public void gpOneMethodOne() {
System.out.println("This is gpOneMethodOne() from Group-Two");
}
@Test(groups = "Group-One")
public void gpOneMethodSecond() {
System.out.println("This is gpOneMethodSecond() from Group-One");
}
@Test(groups = "Group-Two")
public void gpSecondMethodOne() {
System.out.println("This is gpSecondMethodOne() from Group-Two");
}
@Test(groups = "Group-Two")
public void gpSecondMethodSecond() {
System.out.println("This is gpSecondMethodSecond() from Group-Two");
}
@AfterGroups
public void afterGroups() {
System.out.println("!.......AfterGroups From Class TestAnnotation.......!");
}
}
OUTPUT
[TestNG] Running:
Command line suite
!.......BeforeSuite From Class TestAnnotation.......!
!.......BeforeTest From Class TestAnnotation.......!
!.......BeforeClass From Class TestAnnotation.......!
!.......BeforeMethod From Class TestAnnotation.......!
This INTEGER data 1 Consumed and This STRING data Test 1 Consumed
!.......AfterMethod From Class TestAnnotation.......!
!.......BeforeMethod From Class TestAnnotation.......!
This INTEGER data 1 Consumed and This STRING data Test 2 Consumed
!.......AfterMethod From Class TestAnnotation.......!
!.......BeforeGroups From Class TestAnnotation.......!
!.......BeforeMethod From Class TestAnnotation.......!
This is gpOneMethodOne() from Group-Two
!.......AfterMethod From Class TestAnnotation.......!
!.......BeforeMethod From Class TestAnnotation.......!
This is gpOneMethodSecond() from Group-One
!.......AfterMethod From Class TestAnnotation.......!
!.......BeforeGroups From Class TestAnnotation.......!
!.......BeforeMethod From Class TestAnnotation.......!
This is gpSecondMethodOne() from Group-Two
!.......AfterMethod From Class TestAnnotation.......!
!.......BeforeMethod From Class TestAnnotation.......!
This is gpSecondMethodSecond() from Group-Two
!.......AfterMethod From Class TestAnnotation.......!
!.......AfterClass From Class TestAnnotation.......!
!.......AfterTest From Class TestAnnotation.......!
!.......AfterSuite From Class TestAnnotation.......!
===============================================
Command line suite
Total tests run: 6, Failures: 0, Skips: 0
===============================================
Here we can start the learn some annotation of TestNG and later we know some TestNG xml configuration by which we can run multiple test with the one configuration file.
Let's start to know some Annotation of TestNG
Annotation | Description | ||
---|---|---|---|
@BeforeSuite | The annotated method will be run only once before all tests in this suite have run. | ||
@AfterSuite | The annotated method will be run only once after all tests in this suite have run. | ||
@BeforeClass | The annotated method will be run only once before the first test method in the current class is invoked. | ||
@AfterClass | The annotated method will be run only once after all the test methods in the current class have been run. | ||
@BeforeTest | The annotated method will be run before any test method belonging to the classes inside the @Test tag is run. | ||
@AfterTest | The annotated method will be run after all the test methods belonging to the classes inside the @Test tag have run. | ||
@BeforeGroups | The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked. | ||
@AfterGroups | The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked. | ||
@BeforeMethod | The annotated method will be run before each test method. | ||
@AfterMethod | The annotated method will be run after each test method. | ||
@DataProvider | Marks a method as supplying data for a test method. The annotated method must return an Object[ ][ ] where each Object[ ] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation. | ||
@Factory | Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[ ]. | ||
@Listeners | Defines listeners on a test class. | ||
@Parameters | Describes how to pass parameters to a @Test method. | ||
@Test | Marks a class or a method as part of the test. |
In below a sample code example is given
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package excelfilereadapachepoi;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
*
* @author Administrator
*/
public class TestNGAnotationClass1 {
@BeforeSuite
public void beforeSuite() {
System.out.println("!.......BeforeSuite From Class TestAnnotation.......!");
}
@BeforeClass
public void beforeClass() {
System.out.println("!.......BeforeClass From Class TestAnnotation.......!");
}
@BeforeTest
public void beforeTest() {
System.out.println("!.......BeforeTest From Class TestAnnotation.......!");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("!.......BeforeMethod From Class TestAnnotation.......!");
}
@DataProvider(name = "dp")
public static Object[][] dataprovider() {
return new Object[][]{{1, "Test 1"}, {1, "Test 2"}};
}
@Test(dataProvider = "dp")
public void dataConsumer(int i, String j) {
System.out.println("This INTEGER data " + i + " Consumed and " + " This STRING data " + j + " Consumed");
}
@AfterSuite
public void afterSuite() {
System.out.println("!.......AfterSuite From Class TestAnnotation.......!");
}
@AfterClass
public void afterClass() {
System.out.println("!.......AfterClass From Class TestAnnotation.......!");
}
@AfterMethod
public void afterMethod() {
System.out.println("!.......AfterMethod From Class TestAnnotation.......!");
}
@AfterTest
public void afterTest() {
System.out.println("!.......AfterTest From Class TestAnnotation.......!");
}
@BeforeGroups(groups = {"Group-One", "Group-Two"})
public void beforeGroup() {
System.out.println("!.......BeforeGroups From Class TestAnnotation.......!");
}
@Test(groups = "Group-One")
public void gpOneMethodOne() {
System.out.println("This is gpOneMethodOne() from Group-Two");
}
@Test(groups = "Group-One")
public void gpOneMethodSecond() {
System.out.println("This is gpOneMethodSecond() from Group-One");
}
@Test(groups = "Group-Two")
public void gpSecondMethodOne() {
System.out.println("This is gpSecondMethodOne() from Group-Two");
}
@Test(groups = "Group-Two")
public void gpSecondMethodSecond() {
System.out.println("This is gpSecondMethodSecond() from Group-Two");
}
@AfterGroups
public void afterGroups() {
System.out.println("!.......AfterGroups From Class TestAnnotation.......!");
}
}
OUTPUT
[TestNG] Running:
Command line suite
!.......BeforeSuite From Class TestAnnotation.......!
!.......BeforeTest From Class TestAnnotation.......!
!.......BeforeClass From Class TestAnnotation.......!
!.......BeforeMethod From Class TestAnnotation.......!
This INTEGER data 1 Consumed and This STRING data Test 1 Consumed
!.......AfterMethod From Class TestAnnotation.......!
!.......BeforeMethod From Class TestAnnotation.......!
This INTEGER data 1 Consumed and This STRING data Test 2 Consumed
!.......AfterMethod From Class TestAnnotation.......!
!.......BeforeGroups From Class TestAnnotation.......!
!.......BeforeMethod From Class TestAnnotation.......!
This is gpOneMethodOne() from Group-Two
!.......AfterMethod From Class TestAnnotation.......!
!.......BeforeMethod From Class TestAnnotation.......!
This is gpOneMethodSecond() from Group-One
!.......AfterMethod From Class TestAnnotation.......!
!.......BeforeGroups From Class TestAnnotation.......!
!.......BeforeMethod From Class TestAnnotation.......!
This is gpSecondMethodOne() from Group-Two
!.......AfterMethod From Class TestAnnotation.......!
!.......BeforeMethod From Class TestAnnotation.......!
This is gpSecondMethodSecond() from Group-Two
!.......AfterMethod From Class TestAnnotation.......!
!.......AfterClass From Class TestAnnotation.......!
!.......AfterTest From Class TestAnnotation.......!
!.......AfterSuite From Class TestAnnotation.......!
===============================================
Command line suite
Total tests run: 6, Failures: 0, Skips: 0
===============================================
No comments:
Post a Comment