TestNG Annotations
@BeforeSuite: The annotate method will be run before all tests in this suite have run.
@AfterSuite: The annotated method will be run after all tests in this suite have run.
@BeforeClass: The annotated method will be run before the first test method in the current class is
invoked.
@AfterClass: The annotated method will be run 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 tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.
@BeforeMethod: The annotated method will be run before each test method.
@Test: The annotated method is a part of a test case.
@AfterMethod: The annotated method will be run after each test method.
@DataProvider: The annotated method provides data to the @Test methods.
The annotated method returns an Object [][].
@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.
alwaysRun----> For before methods (beforeSuite, beforeTest, beforeTestClass and beforeTestMethod,but not beforeGroups): If set to true, this configuration method will be run regardless of what groups it belongs to. For after methods (afterSuite, afterClass, ...): If set to true, this configuration method will be runeven if one or more methods invoked previously failed or was skipped.
dependsOnGroups----> The list of groups this method depends on.
dependsOnMethods----> The list of methods this method depends on.
enabled -----> Whether methods on this class/method are enabled.
groups ------>The list of groups this class/method belongs to.
inheritGroups ------->If true, this method will belong to groups specified in the @Test annotation
at the class level.
package testng;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestNGAnnotations {
@BeforeSuite
public void instantiate() {
System.out.println("Instantiate Object");
}
@BeforeTest
public void dataBaseConnection() {
System.out.println("Database Connected");
}
@BeforeMethod
public void BeforeMethod() {
System.out.println("Run before each Test Case.");
}
@Test
public void testCase1() {
System.out.println("First Test Case Result..... ");
}
@Test
public void testCase2() {
System.out.println("Second Test Case Result.... ");
}
@AfterMethod
public void AfterMethod() {
System.out.println("Run after each Test Case");
}
@AfterTest
public void dataBaseDisconnection() {
System.out.println("Database Disconnected");
}
@AfterSuite
public void destory() {
System.out.println("Destory Object");
}
}
Create testng.xml:
testng.xml: Right click on Project --> TestNG --> Convert to TestNG.
<!--?xml version="1.0" encoding="UTF-8"?-->
<suite name="Suite" parallel="none">
<test name="Test">
<classes>
<class name="testng.TestNGAnnotations">
</class></classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Execute TestNG:
Right click on testng.xml --> Run As --> TestNG Suite OR Right click on java file -->
Run As --> TestNG Test
OUTPUT:
@BeforeSuite: The annotate method will be run before all tests in this suite have run.
@AfterSuite: The annotated method will be run after all tests in this suite have run.
@BeforeClass: The annotated method will be run before the first test method in the current class is
invoked.
@AfterClass: The annotated method will be run 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 tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.
@BeforeMethod: The annotated method will be run before each test method.
@Test: The annotated method is a part of a test case.
@AfterMethod: The annotated method will be run after each test method.
@DataProvider: The annotated method provides data to the @Test methods.
The annotated method returns an Object [][].
@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.
alwaysRun----> For before methods (beforeSuite, beforeTest, beforeTestClass and beforeTestMethod,but not beforeGroups): If set to true, this configuration method will be run regardless of what groups it belongs to. For after methods (afterSuite, afterClass, ...): If set to true, this configuration method will be runeven if one or more methods invoked previously failed or was skipped.
dependsOnGroups----> The list of groups this method depends on.
dependsOnMethods----> The list of methods this method depends on.
enabled -----> Whether methods on this class/method are enabled.
groups ------>The list of groups this class/method belongs to.
inheritGroups ------->If true, this method will belong to groups specified in the @Test annotation
at the class level.
package testng;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestNGAnnotations {
@BeforeSuite
public void instantiate() {
System.out.println("Instantiate Object");
}
@BeforeTest
public void dataBaseConnection() {
System.out.println("Database Connected");
}
@BeforeMethod
public void BeforeMethod() {
System.out.println("Run before each Test Case.");
}
@Test
public void testCase1() {
System.out.println("First Test Case Result..... ");
}
@Test
public void testCase2() {
System.out.println("Second Test Case Result.... ");
}
@AfterMethod
public void AfterMethod() {
System.out.println("Run after each Test Case");
}
@AfterTest
public void dataBaseDisconnection() {
System.out.println("Database Disconnected");
}
@AfterSuite
public void destory() {
System.out.println("Destory Object");
}
}
Create testng.xml:
testng.xml: Right click on Project --> TestNG --> Convert to TestNG.
<!--?xml version="1.0" encoding="UTF-8"?-->
<suite name="Suite" parallel="none">
<test name="Test">
<classes>
<class name="testng.TestNGAnnotations">
</class></classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Execute TestNG:
Right click on testng.xml --> Run As --> TestNG Suite OR Right click on java file -->
Run As --> TestNG Test
OUTPUT: