TestNG Annotations @BeforeTest (original) (raw)

Last Updated : 6 Aug, 2025

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

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

**Step 1: In the Maven project, create a TestNG Class that contains @BeforeTest.

**Before_Test1.Java

Java `

package com.geeksforgeeks.test;

import org.testng.annotations.Test; import org.testng.annotations.BeforeTest;

public class Before_Test1 {

@BeforeTest public void beforeTest() { System.out.println("This method will be executed before the execution of all @Test annotated methods"); }

@Test public void test1() { System.out.println("Test1 Executed"); }

@Test public void test2() { System.out.println("Test2 Executed"); }

}

`

**Now, let’s explain what this code does:

**beforeTest (@BeforeTest):

**Test Methods (@Test):

After performing the operation, the result is printed to the console.

**Step 2: Now, we create the AnnotationsTest.xml file to configure the Before_Test1 Class.

XML `

    </classes>
</test>

`

**Step 3: 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.

Output of BeforeTest

Output of BeforeTest