TestNG

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.

testng.xml:

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

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.

TestClass1:

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 {
	@BeforeSuite
	public void doBeforeSuite() {
		System.out.println("testClass1: before suite");
	}		
	
	@BeforeTest
	public void doBeforeTest() {
		System.out.println("testClass1: before test");
	}
	
	@Test
	public void unitLevel1() {
		System.out.println("testClass1: Unit level1 testing");
	}
	
	@Test
	public void unitLevel2() {
		System.out.println("testClass1: Unit level2 testing");
	}
	
	@BeforeMethod
	public void doBeforeMethod() {
		System.out.println("testClass1: before method");
	}
	
	@AfterMethod
	public void doAfterMethod() {
		System.out.println("testClass1: after method");
	}
	
	@BeforeClass
	public void doBeforeClass() {
		System.out.println("testClass1: before class");
	}
	
	@AfterClass
	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.

TestClass2:

package com.javacodegeeks.testng;

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

public class TestClass2 {

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

TestClass3:

package com.javacodegeeks.testng;

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

public class TestClass3 {
	@BeforeTest
	public void doBeforeTest() {
		System.out.println("testClass3: before test");
	}
	
	@Test
	public void scenario2() {
		System.out.println("testClass3: scenario2");
	}
	
	@AfterSuite
	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’.

runTestNGInEclipse
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.

Output:

[TestNG] Running:
  C:\javacodegeeks_ws\testNgBeforeTest\test\com\javacodegeeks\testng\testng.xml

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

===============================================
Feature1
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.

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

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.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button