@AfterSuite annotation in TestNG

TestNG is a widely-used testing framework in Java for conducting unit, functional, and integration testing. It includes several annotations that aid in managing the testing lifecycle and reporting outcomes. @AfterSuite is an example of such an annotation that enables you to perform specific actions once all tests in a TestNG suite are finished.

In TestNG, the @AfterSuite annotation has the lowest priority compared to the @BeforeXXX and @AfterXXX annotations.

What is @AfterSuite annotation?

@AfterSuite is a part of the TestNG annotations suite and is used to perform cleanup tasks after all tests in a suite have run. It is a useful annotation for tasks such as closing database connections, deleting temporary files, and generating test reports. The @AfterSuite annotation is executed only once after all tests in the suite have been completed.

Code Example

As we are aware, the execution of the @AfterSuite annotation occurs after all the tests have been executed. The TestNG.xml file mentioned below specifies the exact execution point of the @AfterSuite annotation for better clarity.

<!-- @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
===============================================

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 write us in the comments or mail us at admin@codekru.com.

Other TestNG annotations
Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *