TestNG annotations – @BeforeSuite annotation

In this post, we will discuss the @BeforeSuite annotation in TestNG.

@BeforeSuite annotation has the highest priority of all the @BeforeXXX and @AfterXXX annotations in TestNG. So, the method annotated with this annotation will run before any of the tests in the suite.
What is the use of @BeforeSuite annotation? It is mostly used to set up test cases. It is very helpful in building a test automation framework. All the common functionality to fetch some variables or read configuration files can go in the method annotated with this annotation.

When is the @BeforeSuite 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>

So, the @BeforeSuite annotated method will execute before the <suite> tag.

We will take two classes ( CodekruTestOne and CodekruTestSecond) and define the @BeforeSuite annotated method in CodekruTestSecond class.

CodekruTestFirst.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.BeforeSuite;
import org.testng.annotations.Test;

public class CodekruTestSecond {

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

We will now run the below XML to execute the two classes, and let’s see what happens.

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

Output –

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

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

So, time for some brainstorming 🙂

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

The answer is yes. We can use more than one @BeforeSuite annotated method in a class, and all methods will run before test cases in the suite. We will keep two @BeforeSuite annotated methods in CodekruTestSecond class. Let’s see it with a demonstration.

package Test;

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

public class CodekruTestSecond {

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

and now run the below XML file

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

</suite>

Output –

In beforeSuite1 method
In beforeSuite2 method
Executing the test in CodekruTestSecond class

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

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

package Test;

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

class CodekruTest {

	@BeforeSuite
	public void beforeSuiteMethod() {
		System.out.println("beforeSuite 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 –

beforeSuite method called
Executing the subclass test

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

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

You can look at this article if you want to know more about the TestNG annotations.

We hope that you 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.