TestNG Annotations and its sequence during tests execution are discussed in this post. Here are the list of annotations TestNG supports:

TestNG Annotations
  • @Test :  To mark a method as a test method
  • @BeforeMethod: Executes before each test (@Test) method
  • @AfterMethod: Executes after each test method
  • @BeforeClass: Executes before the first test method in the current class
  • @AfterClass: Executes after all the test methods in a current class
  • @BeforeTest: Executes before any test methods of available classes which are inside the <test> tag in the testng.xml in this suite
  • @AfterTest: Executes after all the test methods of available classes which are inside the <test> tag in the testng.xml in this suite
  • @BeforeSuite: Executes before all the tests in this suite
  • @AfterSuite: Executes after all tests executed in this current suite
  • @BeforeGroup: Executes before the first test method run in that specified group
  • @AfterGroup: Executes after the end of all the test methods executed in that specified group

TestNG have more annotations like @BeforeGroup, @AfterGroup, @Listeners, @Parameters, @DataProvider, @Factory.  These will be covered in next posts.

Sequence of Execution of the annotations:
  1. BeforeSuite
  2. BeforeTest
  3. BeforeClass
  4. BeforeMethod
  5. Test
  6. AfterMethod
  7. AfterClass
  8. AfterTest
  9. AfterSuite

Here is the code to show the sequence of execution.

  • Class ‘SampleTestngUsage’ contains annotations @BeforeTest, @BeforeSuite
  • Class ‘AnnotationsTest’ contains annotations @Test, @BeforeMethod, @AfterMethod, @beforeClass, @AfterClass, @AfterTest & @AfterSuite

SampleTestngUsage.java  code

  • package com.exploreselenium;
    
    import org.testng.annotations.*;
    
    public class SampleTestngUsage {
    
        @BeforeTest
        public void beforeTest(){
            System.out.println("From BeforeTest annotation in SampleTestngUsage class ..");
        }
    
        @BeforeSuite
        public void beforeSuite(){
            System.out.println("From BeforeSuite annotation in SampleTestngUsage class ..");
        }
    }
    

AnnotationsTest.java code

  • package com.exploreselenium;
    
    import org.testng.annotations.*;
    
    public class AnnotationsTest {
    
        @Test
        public void first ()
        {
           System.out.println("From first test annotated in AnnotationsTest....");
        }
    
        @BeforeMethod
        public void beforeMethod()
        {
            System.out.println("From BeforeMethod annotation in AnnotationsTest....");
        }
    
        @AfterMethod
        public void AfterMethod()
        {
            System.out.println("From AfterMethod annotation in AnnotationsTest....");
        }
    
        @BeforeClass
        public void beforeClass()
        {
            System.out.println("From BeforeClass annotation test in AnnotationsTest class....");
        }
    
        @AfterClass
        public void afterClass()
        {
            System.out.println("From AfterClass annotation test in AnnotationsTest class....");
        }
    
        @AfterTest
        public void afterTest()
        {
            System.out.println("From AfterTest annotation in AnnotationsTest class ....");
        }
    
        @AfterSuite
        public void afterSuite()
        {
            System.out.println("From AfterSuite annotation in AnnotationsTest class ....");
        }
    
    }
    
 testng.xml file
  • Used above two classes in testng.xml(sample.xml) file inside <classes> tag.

Below is the sample.xml file.

  • <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    
    <suite name="TestNG Sample" verbose="1">
        <test name="Sample" preserve-order="true">
            <classes>
                <class name="com.exploreselenium.SampleTestngUsage"/>
                <class name="com.exploreselenium.AnnotationsTest"/>
            </classes>
        </test>
    </suite>
    

Below is the TestNG results (console output) when we execute the sample.xml file.

  • [TestNG] Running:
      G:\ExploreSelenium\Config\Suites\sample.xml
    
    From BeforeSuite annotation in SampleTestngUsage class ..
    From BeforeTest annotation in SampleTestngUsage class ..
    From BeforeClass annotation test in AnnotationsTest class....
    From BeforeMethod annotation in AnnotationsTest....
    From first test annotated in AnnotationsTest....
    From AfterMethod annotation in AnnotationsTest....
    From AfterClass annotation test in AnnotationsTest class....
    From AfterTest annotation in AnnotationsTest class ....
    From AfterSuite annotation in AnnotationsTest class ....
    
    ===============================================
    TestNG Sample
    Total tests run: 1, Failures: 0, Skips: 0
    ===============================================
    
  • Above execution order of the annotations  is same as discussed above.
Summary:

In Summary,  basic and foremost important TestNG annotations are explained and also their sequence during the execution.

Basic Usage of TestNG
Run TestNG Tests
0