TestNG annotations – @BeforeTest annotation

In this post, we will study the @BeforeTest annotation in TestNG. What is its use? and how to use it?

What is the use of @BeforeTest annotation in TestNG? @BeforeTest annotation is used to do some prerequisite tasks needed to execute the test cases, like preparing some test data.

So, when does this @BeforeTest annotated method will be executed?

Below XML will help in understanding the execution point.

<suite name="codekru">
	<test name="codekru"> <!-- @BeforeTest will execute here -->
		<classes>  <!-- @BeforeClass will execute here -->
			<class name="Test.CodekruTestFirst" />
			<class name="Test.CodekruTestSecond" />
		</classes>
	</test>
</suite>

So, @BeforeTest annotated method will execute before the <test> tag. And we can define our @BeforeTest annotated method in any of the classes mentioned inside the <test></test> tag.

CodekruTestFirst.java
package Test;

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

public class CodekruTestFirst {

	@BeforeTest
	public void beforeTest() {
		System.out.println("before test method in CodekruTestFirst class");
	}
	
	@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.Test;

public class CodekruTestSecond {

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

}

Now, we will run the below testng.xml file and see what happens

<suite name="codekru">
	<test name="codekru"> <!-- @BeforeTest will execute here -->
		<classes>
			<class name="Test.CodekruTestFirst" />
			<class name="Test.CodekruTestSecond" />
		</classes>
	</test>
</suite>

Output –

before test method in CodekruTestFirst class
Executing the test in CodekruTestFirst class
Executing the test in CodekruTestSecond class

===============================================
codekru
Total tests run: 2, Failures: 0, Skips: 0
But what will happen if we keep @BeforeTest annotated methods in both classes?

Let’s keep @BeforeTest annotated method in the CodekruTestSecond class as well.

package Test;

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

public class CodekruTestSecond {

	@BeforeTest
	public void beforeTest() {
		System.out.println("before test method in CodekruTestSecond class");
	}

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

}

And now, we will run the same testng.xml file as we did earlier and see what happens now.

before test method in CodekruTestFirst class
before test method in CodekruTestSecond class
Executing the test in CodekruTestFirst class
Executing the test in CodekruTestSecond class

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

So, we can see that both annotated methods ran before the actual execution of the test cases in the suite.

The next question is, how will @BeforeTest annotation behave if placed on a superclass?
class CodekruTest{
	
	@BeforeTest
	public void beforeTest() {
		System.out.println("beforeTest method in superclass");
	}
}

public class CodekruTestSubclass extends CodekruTest{

	@Test()
	public void test() {
		System.out.println("Executing the test in subclass");
		Assert.assertTrue(true);
	}

}

Below is the XML for the same, where we will only execute the subclass.

<suite name="codekru">
	<test name="codekruTest">
		<classes>
			<class name="Test.CodekruTestSubclass">
			</class>
		</classes>
	</test>
</suite>

Output –

beforeTest method in superclass
Executing the test in subclass

Here you can see that the beforeTest() method present in the superclass is executed before the cases of the subclass. This is because the subclass inherited the superclass method, but if we would make the beforeTest() method private, then it won’t be executed as the subclass won’t inherit it.

If you want to know about other TestNG annotations and the hierarchy of @BeforeXXX and @AfterXXX annotations. Please visit this link.

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