06. TestNG Annotations – Hierarchy or Levels

TestNG Annotations – Hierarchy or Levels

Annotations hierarchy or Annotations levels in TestNG.
It says that @Test is the smallest annotation here. @Method will be executed first, before and after the execution of @Test. The same way @Class will be executed first, before and after the execution of @Method and so on.

Easily understand once we run the below code.

package Others;
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 TestNG_Annotations {

                        @Test
                        public void testCase1() {
                                    System.out.println("This is the Test Case 1");
                        }

                        @Test
                        public void testCase2() {
                                    System.out.println("This is the Test Case 2");
                        }

                        @BeforeMethod
                        public void beforeMethod() {
                                    System.out.println("This will execute before every Method");
                        }

                        @AfterMethod
                        public void afterMethod() {
                                    System.out.println("This will execute after every Method");
                        }

                        @BeforeClass
                        public void beforeClass() {
                                    System.out.println("This will execute before the Class");
                        }

                        @AfterClass

                        public void afterClass() {
                                    System.out.println("This will execute after the Class");
                        }

                        @BeforeTest
                        public void beforeTest() {
                                    System.out.println("This will execute before the Test");
                        }

                        @AfterTest
                        public void afterTest() {
                                    System.out.println("This will execute after the Test");
                        }

                        @BeforeSuite
                        public void beforeSuite() {
                                    System.out.println("This will execute before the Test Suite");
                        }

                        @AfterSuite
                        public void afterSuite() {
                                    System.out.println("This will execute after the Test Suite");
                        }

            }

To Get the below response once we run the above code.

Output:
This will execute before the Test Suite
This will execute before the Test
This will execute before the Class
This will execute before every Method
This is the Test Case 1
This will execute after every Method
This will execute before every Method
This is the Test Case 2
This will execute after every Method
This will execute after the Class
This will execute after the Test
PASSED: testCase1
PASSED: testCase2

In Output,clearly visible that the @Suite annotation is the very first and the very lastly executed. Then @Test followed by @Class.

Now if you notice, the @Method has executed twice. As @Test is a method in the class, hence @Method will always executed for each @Test method.

No comments:

Post a Comment