TestNG annotations – @AfterSuite annotation

@AfterSuite annotation in TestNG

Earlier, we talked about the @BeforeSuite annotation, and in this post, we will discuss the @AfterSuite annotation in TestNG.

@AfterSuite annotation has the lowest priority of all the @BeforeXXX and @AfterXXX annotations in TestNG. So, the method annotated with this annotation will run after all of the tests in the suite.

When is the @AfterSuite annotated method going to execute?

All @BeforeXXX and @AfterXXX annotations execution points can easily be understood via the testng.xml file.

<!-- @BeforeSuite annoated method will execute here  -->
<suite name="codekru">
    <test name="codekru">
        <classes>
            <class name="Test.CodekruTestFirst" />
            <class name="Test.CodekruTestSecond" />
        </classes>
    </test>
</suite>
<!-- @AfterSuite annoated method will execute here -->

So, the @AfterSuite annotated method will execute after the <suite> tag.

Let’s take two classes (CodekruTestOne and CodekruTestSecond), and we will define the @AfterSuite annotated method in CodekruTestSecond class.

CodekruTestOne.java

package Test;
 
import org.testng.Assert;
import org.testng.annotations.Test;
 
public class CodekruTestFirst {
 
    @Test
    public void test() {
        System.out.println("Executing the test in CodekruTestFirst class");
        Assert.assertTrue(true);
    }
     
}

CodekruTestSecond.java

package Test;

import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;

public class CodekruTestSecond {

	@AfterSuite
	public void afterSuite() {
		System.out.println("In afterSuite method");
	}

	@Test
	public void test() {
		System.out.println("Executing the test in CodekruTestSecond class");
		Assert.assertTrue(true);
	}

}

Now, we will run the below testng.xml file and see what happens

<suite name="codekru">
	<test name="codekru">
		<classes>
			<class name="Test.CodekruTestSecond" />
			<class name="Test.CodekruTestFirst" />
		</classes>
	</test>
</suite>

Output –

Executing the test in CodekruTestSecond class
Executing the test in CodekruTestFirst class
In afterSuite method

===============================================
codekru
Total tests run: 2, Failures: 0, Skips: 0
===============================================

Here in the result, we can see that the afterSuite method ran after all the tests in the suite.

So, time for some brainstorming.

Can we use more than one @AfterSuite annotation method in one class?

The answer is yes. We can use more than one @AfterSuite annotated method in a class, and then all those methods will run after all tests in that suite. Let’s look at it with an example.

package Test;

import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;

public class CodekruTestSecond {

	@AfterSuite
	public void afterSuite1() {
		System.out.println("In afterSuite1 method");
	}

	@AfterSuite
	public void afterSuite2() {
		System.out.println("In afterSuite2 method");
	}

	@Test
	public void test() {
		System.out.println("Executing the test in CodekruTestSecond class");
		Assert.assertTrue(true);
	}

}

And now run the below XML file.

<suite name="codekru">
	<test name="codekruTest">
		<classes>
			<class name="Test.CodekruTestSecond" />
		</classes>
	</test>
</suite>
<!-- @AfterSuite annoated method will execute here -->

Output –

Executing the test in CodekruTestSecond class
In afterSuite1 method
In afterSuite2 method

===============================================
codekru
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
How does @AfterSuite work when placed on a superclass?

Let’s have a look at this with an example.

package Test;

import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;

class CodekruTest {

	@AfterSuite
	public void afterSuiteMethod() {
		System.out.println("afterSuite method called");
	}

}

public class CodekruTestSubclass extends CodekruTest {

	@Test()
	public void test() {
		System.out.println("Executing the subclass test");
		Assert.assertTrue(true);
	}

}

And now, we will run below testng.xml and see what happens.

<suite name="codekru">
	<test name="codekru">
		<classes>
			<class name="Test.CodekruTestSubclass" />
		</classes>
	</test>
</suite>

Output –

Executing the subclass test
afterSuite method called

===============================================
codekru
Total tests run: 1, Failures: 0, Skips: 0

So, the afterSuiteMethod ran even if it was placed on the superclass as the subclass inherited all the methods. But if we make afterSuiteMethod private, the afterSuiteMethod method won’t run as private methods cannot be inherited by the subclass.

If you want to look at the hierarchy of the @BeforeXXX and @AfterXXX annotations in testng, this post might help you.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Other TestNG annotations
Liked the article? Share this on

Leave a Comment

Your email address will not be published.