TestNG Annotations order

TestNG Annotations order

TestNg is more powerful and efficient as compared to the Junit. TestNG is a Testing framework. It is inspired from Junit and Nunit. TestNG is created to cover all the categories of the test like unit, functional, end2end ,Integration etc.

There are lots of drawbacks of Junit which have been eliminated in testNG . TestNG required JDK 7 or higher.If you want to you TestNG inside eclipse you need to install TestNG plugin.

There are different types of test annotations as well in TestNG.

TestNG Annotations order Is very important when you are writing a test scripts and it is a very important part of any framework. so all the testers should take proper care of TestNG Annotations order. and it is advisable for all to remember the execution sequence of annotations. TestNG Annotations order​ will also help you to manage your execution cycle.

There is a Testng.xml where we define which test suit and test cases should be Run. TestNG also supports @dataprovder which is used for data driven testing. you can also pass parameters through TestNG XML file.

Sequence of Execution of the TestNG annotations:

BeforeSuite
BeforeTest
BeforeClass
BeforeMethod
Test
AfterMethod
AfterClass
AfterTest
AfterSuite

So mention about sequence is execution sequence of TestNG. Tt doesn’t matter in which sequence you write it they will executed as per the above sequence.

For testing interview questions you can click on this link

Below is the sample code

So below tags will be executed as per TestNG Annotations order

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;

public class SampleTest {

// test case 1

@Test
public void testCase1() {
System.out.println("Executing test case 1");
}

// test case 2

@Test
public void testCase2() {
System.out.println("Executing test case 2");
}

@BeforeMethod
public void beforeMethod() {
System.out.println("Executing beforeMethod");
}

@AfterMethod
public void afterMethod() {
System.out.println("Executing afterMethod");
}

@BeforeClass
public void beforeClass() {
System.out.println("Executing beforeClass");
}

@AfterClass
public void afterClass() {
System.out.println("Executing afterClass");
}

@BeforeTest
public void beforeTest() {
System.out.println("Executing beforeTest");
}

@AfterTest
public void afterTest() {
System.out.println("Executing afterTest");
}

@BeforeSuite
public void beforeSuite() {
System.out.println("Executing beforeSuite");
}

@AfterSuite
public void afterSuite() {
System.out.println("Executing afterSuite");
}
}

Author: Saad Mansuri

Leave a Reply

Your email address will not be published. Required fields are marked *