Home » Enterprise Java » TestNG » TestNG beforeTest example

About Ram Mokkapaty

Ram Mokkapaty
Ram holds a master's degree in Machine Design from IT B.H.U. His expertise lies in test driven development and re-factoring. He is passionate about open source technologies and actively blogs on various java and open-source technologies like spring. He works as a principal Engineer in the logistics domain.

TestNG beforeTest example

In this article, I will show you an example of the TestNG @beforeTest annotation. In TestNG, you can configure your tests using annotations that start with @Before or @After. TestNG defines many configuration annotations, each one belonging to a specific event in its lifecycle.

@beforeTest is one such annotation. A method with @beforeTest annotation will run, before any test method belonging to the classes inside the test tag is run. We will know more about the test tag as the example progresses.

Before we proceed with the demonstration, a bit about the setup:

  • I am using Eclipse as the IDE, version Luna 4.4.1.
  • I will be running the tests using eclipse TestNG plugin so you need to install TestNG Eclipse Plugin.

1. Configure our tests in testng.xml file

In this example, we are going to run TestNG using a testng.xml file, so let’s first configure the tests to be run.

testng.xml is the configuration file for TestNG where we will define the test suite, tests and the classes that form the test. Remember a suite is the top-level element of a testng.xml file. Each XML file defines one test suite. If you have more than one suite, you will need to define multiple testng.xml files, of course with different file names.


<?xml version="1.0" encoding="UTF-8"?>
<suite name="Feature1" parallel="false">
  <test name="UnitLevelTest">
      <class name="com.javacodegeeks.testng.TestClass1"/>
  <test name="AcceptanceTest">
      <class name="com.javacodegeeks.testng.TestClass2"/>
      <class name="com.javacodegeeks.testng.TestClass3"/>

I will now brief you about the structure of testng.xml. As you can see, our test suite is called Feature1. It contains two tests, where each test is declared using a <test> tag.

We have two tests, one for the unit level testing and the other for acceptance level testing. The first test UnitLevelTesting contains class TestClass1 and the second test AcceptanceTest contains class TestClass2 and TestClass3.

In order to understand @beforeTest it is important that we understand its order of execution in context to the other annotated methods like @beforeSuite, @beforeClass and @beforeMethod. Which is why I have defined my classes in such a way that we have a mix of @beforeSuite, @beforeTest, @beforeClass and @beforeMethod annotated methods.

TestClass1 is for unit level testing. It has two @Test methods and a @BeforeTest method.


package com.javacodegeeks.testng;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestClass1 {
	public void doBeforeSuite() {
		System.out.println("testClass1: before suite");
	public void doBeforeTest() {
		System.out.println("testClass1: before test");
	public void unitLevel1() {
		System.out.println("testClass1: Unit level1 testing");
	public void unitLevel2() {
		System.out.println("testClass1: Unit level2 testing");
	public void doBeforeMethod() {
		System.out.println("testClass1: before method");
	public void doAfterMethod() {
		System.out.println("testClass1: after method");
	public void doBeforeClass() {
		System.out.println("testClass1: before class");
	public void doAfterClass() {
		System.out.println("testClass1: after class");

The following two classes represent acceptance level testing. Each has a @Test method as well as one @BeforeTest method.


package com.javacodegeeks.testng;

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

public class TestClass2 {

	public void doBeforeTest() {
		System.out.println("testClass2: before test");
	public void scenario1() {
		System.out.println("testClass2: scenario1");


package com.javacodegeeks.testng;

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

public class TestClass3 {
	public void doBeforeTest() {
		System.out.println("testClass3: before test");
	public void scenario2() {
		System.out.println("testClass3: scenario2");
	public void doAfterSuite() {
		System.out.println("testClass3: after suite");

2. How do we run TestNG?

Once the test classes and the configuration file testng.xml are defined, its time to run the tests. Select the configuration file testng.xml and right click on it.  A pop-up menu appears, click on ‘Run As’ and then click on ‘TestNG Suite’.


Run TestNG in Eclipse

3. When does a @beforeTest annotated method run?

A method with @beforeTest annotation will run before any test method belonging to the classes inside the test tag is run.
In a testing framework like smoke testing, @BeforeTest can be used to create initial set of data, and @AfterTest can be used to clean up the data, once all the tests are run.

When you run the TestNG, you get the below output.


[TestNG] Running:

testClass1: before suite
testClass1: before test
testClass1: before class
testClass1: before method
testClass1: Unit level1 testing
testClass1: after method
testClass1: before method
testClass1: Unit level2 testing
testClass1: after method
testClass1: after class
testClass2: before test
testClass3: before test
testClass2: scenario1
testClass3: scenario2
testClass3: after suite

Total tests run: 4, Failures: 0, Skips: 0

You can see, the @beforeTest annotated methods run before the first test is run. Lets understand the exact event order to know when it gets fired, and how is it different from other annotations like @BeforeSuite, @BeforeClass and @BeforeMethod.

If you remember, the testng.xml has following components:

  1. A suite – Consists of one or more test tags.
  2. Each test tag is made of one or more classes.
  3. A class – Consists of one or more methods

@beforeTest methods run after @beforeSuite and before @beforeClass whereas @beforeMethod runs before each @Test method. In our example, we have two test tags, tests defined for UnitLevelTest and tests defined for AcceptanceLevelTest so @beforeTest methods fire in two different events. At acceptance level test, we have defined @BeforeTest for each class, TestClass2 and TestClass3, so you will see it fires twice, one for each class.

Download the Eclipse Project

This was an example of the TestNG @beforeTest annotation.

You can download the full source code of this example here: testNgBeforeTest.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Leave a Reply


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Notify of