Execution Sequence of TestNG Annotations:
- Understand the sequence in which all annotations will be executed.
- TestNG annotations executed in a predefined order.
TestNG Annotations:
@Test: To mark a method as a test method.
@BeforeMethod: performed before each test (@Test) method.
@AfterMethod: executed after each test method.
@BeforeClass: Performed before the first examination method in the current class.
@AfterClass: Executed after all test methods in the current class.
@BeforeTest: performed in this suite before any test methods of classes available inside <test> tag in testng.xml.
@AfterTest: Execute this room in testng.xml after all test methods available in the class <test> inside the tag.
@BeforeSuite: Performed before all tests in this suite.
@AfterSuite: Executed after all tests executed in this current suite.
@BeforeGroup: In the specified group, the first test method is executed before.
@AfterGroup: Executed after the end of all test methods executed in that specific group.
- There are more annotations in TestNG @BeforeGroup, @AfterGroup, @Listeners, @Parameters, @DataProvider, @Factory.
Execution Sequence of TestNG Annotations:
- BeforeSuite
- BeforeTest
- BeforeClass
- BeforeMethod
- Test
- AfterMethod
- AfterClass
- AfterTest
- AfterSuite
Example: Annotation sequence programs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | 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 Annotations_Sequence { @Test public void testCase1() { System.out.println("testCase1"); } @Test public void testCase2() { System.out.println("testCase2"); } @BeforeMethod public void beforeMethod() { System.out.println("beforeMethod"); } @AfterMethod public void afterMethod() { System.out.println("afterMethod"); } @BeforeClass public void beforeClass() { System.out.println("beforeClass"); } @AfterClass public void afterClass() { System.out.println("afterClass"); } @BeforeTest public void beforeTest() { System.out.println("beforeTest"); } @AfterTest public void afterTest() { System.out.println("afterTest"); } @BeforeSuite public void beforeSuite() { System.out.println("beforeSuite"); } @AfterSuite public void afterSuite() { System.out.println("afterSuite"); } } |
Output:
[RemoteTestNG] detected TestNG version 6.9.10 [TestNG] Running: beforeSuite beforeTest beforeClass beforeMethod testCase1 afterMethod beforeMethod testCase2 afterMethod afterClass afterTest PASSED: testCase1 PASSED: testCase2 =============================================== Default test Tests run: 2, Failures: 0, Skips: 0 =============================================== afterSuite =============================================== Default suite Total tests run: 2, Failures: 0, Skips: 0 =============================================== |
TestNG Annotations and Execution sequence and Order of execution in TestNG without using priority and Execution Order of TestNG Annotations and Execution Sequence of Annotations in TestNG Example of annotations sequence