How to pass a variable from BeforeTest to Test annotation in TestNG?

Last Updated : 23 Jul, 2025

Testing flows in TestNG often require initialization before you start running tests; for example, you might want to put down some data in the test for that set up some data in a @BeforeTest and then use that data in your @Test methods. One of the possible ways of dealing with this is by using class-level (instance) variables. Here you initialize the variable in @BeforeTest and your @Test method accesses and uses it later.

Approach to pass a variable from BeforeTest to Test annotation in TestNG

Use a class-level variable if you want to pass something from a @BeforeTest method down to a @Test method in testng. You can initialize this variable in the @BeforeTest method before your test methods run. Since these methods are on the same instance of the class, after the variable is set in the @BeforeTest method, it's available in both the @BeforeTest and the @Test methods.

  • Define a class-level variable: This variable will store the data that needs to be passed from the @BeforeTest method to the @Test method.
  • Initialize the variable in the @BeforeTest method: This method runs before any test in the current test class. You can initialize the class-level variable here.
  • Use the variable in the @Test method: Once the @BeforeTest method is executed, the initialized variable will be available for use in the @Test method.

Example Code

Here is the code to pass a variable from BeforeTest to Test annotation in TestNG:

Java
package test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class StudentTest {
    // Step 1: Define class-level variables for student information
    private String studentName;
    private int studentGrade;

    // Step 2: Initialize the variables in @BeforeTest method
    @BeforeTest
    public void fetchStudentInfo() {
        // Simulate fetching student data (name and grade)
        studentName = "John Doe";
        studentGrade = 85;
        System.out.println("Student Info in @BeforeTest: " + studentName + ", Grade: " + studentGrade);
    }

    // Step 3: Use the variables in @Test method
    @Test
    public void verifyStudentGrade() {
        // Check if the student has passed
        boolean hasPassed = studentGrade >= 60;
        System.out.println("Verifying " + studentName + "'s grade...");
        if (hasPassed) {
            System.out.println(studentName + " has passed with a grade of " + studentGrade);
        } else {
            System.out.println(studentName + " has failed with a grade of " + studentGrade);
        }
    }
}

Explanation

  • Class-level variable (testVariable): The variable is declared as a private instance field in the class. This makes it accessible across both the @BeforeTest and @Test methods within the same test class instance.
  • @BeforeTest method: This method runs before the test methods in the current test class.
  • In the setUp() method, we assign the value "TestNG Variable Value" to the class-level variable testVariable.
  • This value can now be used in subsequent test methods.
  • @Test method: The test method testMethod() accesses the testVariable and prints its value.
  • Since the @BeforeTest method runs before the test method, the testVariable will hold the value that was initialized in the setUp() method.

Output

Output
Output

Conclusion

Passing a variable from a @BeforeTest method to a @Test method is quite easy by the use of class level variables. The @BeforeTest and @Test methods can share the same instance of the class. This way, the variable initialized in the @BeforeTest method is available for use in the @Test method. This avoids complicated test data management mechanisms and it ensures that any pre-test setup is as accessible across the test methods.

Comment

Explore