TestNG is a popular testing framework utilized by many in the Java ecosystem. It offers a variety of annotations that help regulate the test execution. One of the most important annotations is the @BeforeSuite
annotation.
This article will discuss the @BeforeSuite annotation and its usage in TestNG. We will cover the following topics:
- What is the @BeforeSuite annotation?
- What are the benefits of using the @BeforeSuite annotation?
- And then some fun questions
What is the @BeforeSuite annotation?
@BeforeSuite annotation is the highest priority of all the @BeforeXXX and @AfterXXX annotations in TestNG. This means any method annotated with @BeforeSuite will execute before any other tests in the suite.
When is the @BeforeSuite annotated method going to execute?
The execution points for @BeforeXXX and @AfterXXX annotations can be easily understood by referring to 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
What is the use of @BeforeSuite annotation?
- It allows you to perform common setup tasks for all tests in the suite, such as initializing the data or setting up the environment.
- It reduces code duplication by allowing you to define common setup tasks in a single method.
- It improves the readability of your test code by separating the common setup tasks from the actual test code.
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 to learn more about the TestNG annotations.
We hope that you 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
- TestNG annotations – @AfterSuite annotation
- TestNG annotations – @BeforeTest annotation
- TestNG annotations – @AfterTest annotation
- TestNG annotations – @BeforeClass annotation
- TestNG annotations – @AfterClass annotation
- TestNG annotations – @BeforeMethod annotation
- TestNG annotations – @AfterMethod annotation
- TestNG annotations – @BeforeGroups annotation
- TestNG annotations – @AfterGroups annotation