 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the order of execution of TestNG methods?
A TestNG class can have various TestNG methods such as −
- @BeforeTest 
- @AfterTest 
- @BeforeSuite 
- @BeforeClass 
- @BeforeMethod 
- @test, etc. 
In this article, we will take a look at the order of execution of different TestNG methods.
TestNG provides the following methods to support the main @Test method. The order of execution should be as following −
<beforeSuite> <beforeTest> <beforeClass> <beforeMethod> <test1> <afterMethod> <afterClass> <afterTest> <afterSuite>
Key points in this order are −
- First of all, beforeSuite() method is executed only once. 
- The afterSuite() method executes only once. 
- Even the methods beforeTest(), beforeClass(), afterClass(), and afterTest() methods are executed only once. 
- beforeMethod() executes for each test case (every time for a new @Test) but before executing the test case. 
- afterMethod() executes for each test case (every time for a new @Test) but after executing the test case. 
- In between beforeMethod() and afterMethod(), each test case (@Test annotation's method) executes. 
Approach/Algorithm to solve this problem −
- Step 1 − import org.testng.annotations.* for TestNG. 
- Step 2 − Write an annotation as @test 
- Step 3 − Create a method for the @test annotation as test1. 
- Step 4 − Repeat the steps for test2 and test3. 
- Step 5 − Write different annotations and their respective methods, for example, @beforeSuite, @afterSuite, @beforeTest, @afterTest, @beforeClass, @afterClass, @beforeMethod, @afterMethod 
- Step 6 − Now create the testNG.xml as given below. 
- Step 7 − Run the testNG.xml or run the testNG class directly in IDE or compile and run it using command line. 
Example
Use the following code to show the order of different TestNG methods −
import org.testng.annotations.*; import org.testng.annotations.Test; public class OrderofTestExecutionInTestNG {    // test case 1    @Test    public void testCase1() {       System.out.println("in test case 1");    }    // test case 2    @Test    public void testCase2() {       System.out.println("in test case 2");    }    @BeforeMethod    public void beforeMethod() {       System.out.println("in beforeMethod");    }    @AfterMethod    public void afterMethod() {       System.out.println("in afterMethod");    }    @BeforeClass    public void beforeClass() {       System.out.println("in beforeClass");    }    @AfterClass    public void afterClass() {       System.out.println("in afterClass");    }    @BeforeTest    public void beforeTest() {       System.out.println("in beforeTest");    }    @AfterTest    public void afterTest() {       System.out.println("in afterTest");    }    @BeforeSuite    public void beforeSuite() {       System.out.println("in beforeSuite");    }    @AfterSuite    public void afterSuite() {       System.out.println("in afterSuite");    } } testng.xml
This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name = "Suite1"> <test name = "test1"> <classes> <class name = "OrderofTestExecutionInTestNG"/> </classes> </test> </suite>
Output
in beforeSuite in beforeTest in beforeClass in beforeMethod in test case 1 in afterMethod in beforeMethod in test case 2 in afterMethod in afterClass in afterTest in afterSuite
