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:

  1. BeforeSuite
  2. BeforeTest
  3. BeforeClass
  4. BeforeMethod
  5. Test
  6. AfterMethod
  7. AfterClass
  8. AfterTest
  9. AfterSuite

 

Example: Annotation sequence programs:

 

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

===============================================