testingautomated-teststestngtestng-eclipse

Why is @AfterSuite getting executed for every other annotation present without any body?


I created all the TestNG annotations in a file before providing them any method, and ran just to see the order. The method annotated with @AfterSuite is being run for every annotation present in the file.

public class ChronologicalOrder {
    
    @BeforeSuite
    
    @BeforeTest     
    
    @BeforeClass
    
    @BeforeMethod
    
    @Test   
    
    @AfterMethod
    
    @AfterClass
    
    @AfterTest
    
    @AfterSuite
    public void afterSuite() {
        System.out.println("After suite");
    }
}

Here is the output :

After suite
After suite
After suite
After suite
After suite
After suite
After suite
After suite
PASSED: testNg.ChronologicalOrder.afterSuite

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================

After suite

===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

Solution

  • That is because you have the same method but annotated with different TestNG annotations. So TestNG ends up executing the same method but for different annotations.

    Here's an annotated explanation of what each of those outputs means

    After suite <-- BeforeSuite
    After suite <-- BeforeTest
    After suite <-- BeforeClass
    After suite <-- BeforeMethod
    After suite <-- Test
    After suite <-- AfterMethod
    After suite <-- AfterClass
    After suite <-- AfterTest
    After suite <-- AfterSuite
    

    If you run this modified sample it should be more clear

    import org.testng.ITestNGMethod;
    import org.testng.Reporter;
    import org.testng.annotations.*;
    
    public class ChronologicalOrder {
        @BeforeSuite
        @BeforeTest
        @BeforeClass
        @BeforeMethod
        @Test
        @AfterMethod
        @AfterClass
        @AfterTest
        @AfterSuite
        public void afterSuite() {
            ITestNGMethod method = Reporter.getCurrentTestResult().getMethod();
            if (method.isBeforeSuiteConfiguration()) {
                System.err.println("Before <suite> mode");
            }
            if (method.isBeforeTestConfiguration()) {
                System.err.println("Before <test> mode");
            }
            if (method.isBeforeClassConfiguration()) {
                System.err.println("Before class mode");
            }
            if (method.isAfterClassConfiguration()) {
                System.err.println("After class mode");
            }
            if (method.isBeforeMethodConfiguration()) {
                System.err.println("Before method mode");
            }
            if (method.isTest()) {
                System.err.println("Test case mode");
            }
            if (method.isAfterMethodConfiguration()) {
                System.err.println("After method mode");
            }
            if (method.isAfterTestConfiguration()) {
                System.err.println("After <test> mode");
            }
            if (method.isAfterSuiteConfiguration()) {
                System.err.println("After <suite> mode");
            }
        }
    }
    

    The output would now look like:

    Before <suite> mode
    Before <test> mode
    Before class mode
    Before method mode
    Test case mode
    After method mode
    After class mode
    After <test> mode
    After <suite> mode
    
    ===============================================
    Default Suite
    Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
    ===============================================