In my last blog on TestNG, we had covered installation of TestNG and did some setup to work on our projects. Now lets see about annotations available in TestNG.
Annotations Available In TestNG
@Test
This is the test annotation, used for every test method. Means, before every test method, we need to give this annotation.
@BeforeSuite
It will run before execution of test suite. The method under this annotation will execute before starting execution of any of the testcases of that testsuite. Means, it will executed first in the java file.
@AfterSuite
It will run after execution of test suite. The method under this annotation will execute after execution of all the testcases of that testsuite. Means, it will be executed at the end, after running other methods under other annotations.
@BeforeTest
It will run before the testcase. The method under this annotation will execute before execution of test method.
And this will be executed before @BeforeClass annotation.
@AfterTest
It will run after the testcase. The method under this annotation will execute after execution of test method.
And this will be executed after @AfterClass annotation.
@BeforeMethod
It will run before each test method. The method under this annotation will execute before execution of each and every test method. Means, if there are 'n' test methods, then it will execute n times i.e. before every test method.
@AfterMethod
It will run after each test method. The method under this annotation will execute after execution of each and every test method. Means, if there are 'n' test methods, then it will execute n times i.e. after every test method.
@BeforeClass
It will run before execution of 1st test method in the class.
@AfterClass
It will run after execution of all test methods in the class.
@BeforeGroup
It will run before execution of 1st testcase of the group.
@AfterGroup
It will run after all testcases get executed of that group.
@DataProvider
It will be used, when we want to fetch/read data from excel sheet and use in testcase.
Lets see one example with having most of the annotations in one class and there we can analyze the execution priority.
package testOne;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestClass{
@BeforeClass
public static void beforeclass(){
System.out.println("Executed before class");
}
@BeforeSuite
public static void beforesuite(){
System.out.println("Executed before suite");
}
@BeforeTest
public static void beforetest(){
System.out.println("Executed before test");
}
@BeforeMethod
public static void beforemethod(){
System.out.println("Executed before method");
}
@Test
public static void testMethod1(){
System.out.println("1st test case executed.");
}
@Test
public static void testMethod2(){
System.out.println("2nd test case executed");
}
@AfterMethod
public static void aftermethod(){
System.out.println("Executed after method");
}
@AfterTest
public static void aftertest(){
System.out.println("Executed after test");
}
@AfterSuite
public static void aftersuite(){
System.out.println("Executed after suite");
}
@AfterClass
public static void afterclass(){
System.out.println("Executed after class");
}
}
Here we can see two testmethods were highlighted in screenshot. And as we saw in definition, @BeforeMethod & @AfterMethod, both were executed twice as we have twotest methods. And we can see the rest methods execution order as per annotation.
Execution doesn't occur as per order of annotation present in coding. It follows its own order of execution as we see in the output.
In next blog we will see about assertion types and use with practical example using TestNG.




