Open In App

TestNG Annotations - @BeforeSuite

Last Updated : 06 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

@BeforeSuite is one of the TestNG Annotations. As the name suggests, @BeforeSuite is executed before the execution of all the test cases inside a TestNG Suite. This annotation allows developers to specify various actions to be taken before the execution of all the test cases inside a TestNG Suite.

Let’s understand the @BeforeSuite annotation through an example.

Step 1: Open the Eclipse IDE.

Step 2: Create a Maven Project.

Step 3: After creating the Maven Project, the project exploration will look like the image below.

Screenshot-(306)
Package Explorer

Step 4: Create a TestNG Class that contains @BeforeSuite.

Before_Suite.Java (@BeforeSuite)

Java
package com.geeksforgeeks.test;

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

public class Before_Suite {
  
  @BeforeSuite
  public void beforeSuite() {
      System.out.println("TestNG runs the test cases in alphabetical order");
  }
  @Test
  public void signup() {
      System.out.println("Test for signup");
  }
  @Test
  public void login() {
      System.out.println("Test for login");
  }
}

Now, let’s explain what this code does:

Package Declaration: Before_Suite Class is on the com.geeksforgeeks.test package.

Imports: Before_Suite Class imports annotations and classes from the TestNG framework (org.testng.annotations.BeforeMethod and org.testng.annotations.Test).

Before_Suite Class: This is the main test class. It contains test methods and before class methods.

beforeSuite (@BeforeSuite): This method is annotated with @BeforeSuite, indicating that it should be executed before suite execution this suite contains many classes. It prints this ”TestNG runs the test cases in alphabetical order ” statement.

Test Methods (@Test): Each test method is annotated with @Test, indicating that it is a test case.

There are two test methods: signup() and login().

  • Each test method prints its respective statement.
  • Before operating, the result is printed to the console.

Step 5: Now, we create the AnnotationsTest.xml file to configure the Before_Suite Class.

XML
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="suite">
    <test name="test1">
        <classes>
               <class name="com.geeksforgeeks.test.Before_Suite" /> 
                
        </classes>
    </test>
</suite>


Step 6: Run the AnnotationsTest.xml. Right-click on the AnnotationsTest.xml file, move the cursor down to Run As and then click on the TestNG Suite.

BeforeSuite output
BeforeSuite output

Understanding TestNG Annotations - Part 1
Visit Course explore course icon
Video Thumbnail

Understanding TestNG Annotations - Part 1

Video Thumbnail

TestNG Annotations Continued

Article Tags :

Similar Reads