TestNG annotations

According to TestNG documentation

TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages, and even several external frameworks, such as application servers).

In TestNG, NG stands for Next Generation 🙂

TestNG provides us with many annotations which allow performing some Java logic before and after a certain point and a lot more.
Now, what is an annotation? Annotations are a form of metadata that provides data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.

TestNG Annotations List

Annotation NameWhat does it do?
@BeforeSuiteThe annotated method will be run before all tests in this suite have run
@AfterSuiteThe annotated method will be run after all tests in this suite have run.
@BeforeTestThe annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@AfterTestThe annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
@BeforeGroupsThe list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroupsThe list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
@BeforeClassThe annotated method will be run before the first test method in the current class is invoked.
@AfterClassThe annotated method will be run after all the test methods in the current class have been run.
@BeforeMethodThe annotated method will be run before each test method.
@AfterMethodThe annotated method will be run after each test method.
@DataProviderMarks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.
@FactoryMarks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[].
@ListenersDefines listeners on a test class.
@ParametersDescribes how to pass parameters to a @Test method.
@TestMarks a class or a method as part of the test.
How does @BeforeXXX and @AfterXXX annotations work when placed on a superclass?

These annotations will be also be inherited by the subclass if placed on the superclass. This is useful to centralize test setup for multiple test classes in a common superclass. In this case, TestNG guarantees that the @Before methods are executed in inheritance order (highest superclass first, then going down the inheritance chain), and the @After methods in reverse order (going up the inheritance chain).

Now, we will make two class where CodekruTest will be superclass to CodekruTestSubclass and has below structure

superClass and subclass

CodekruTest.java

package Test;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

public class CodekruTest {

	@BeforeClass
	public void beforeSuperClassMethod() {
		System.out.println("In before method of Superclass");
		Assert.assertTrue(true);
	}

	@AfterClass
	public void AfterSuperClassMethod() {
		System.out.println("In after method of Superclass");
		Assert.assertTrue(true);
	}

}

CodekruTestSubclass.java

package Test;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class CodekruTestSubclass extends CodekruTest {

	@BeforeClass
	public void beforeSubClassMethod() {
		System.out.println("In before method of Subclass");
		Assert.assertTrue(true);
	}

	@AfterClass
	public void AfterSubClassMethod() {
		System.out.println("In after method of Subclass");
		Assert.assertTrue(true);
	}

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

}

and now, we will run the CodekruTestSubclass only and see what happens. Below is the testng.xml for that

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

Output –

In before method of Superclass
In before method of Subclass
Executing the test
In after method of Subclass
In after method of Superclass

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

So, we can see that before method of the super class ran first and its after method ran afterwards.

Hirerachy of @BeforeXXX and @AfterXXX TestNG annotations

The hirerachy would be –

  1. @BeforeSuite
  2. @BeforeTest
  3. @BeforeClass
  4. @BeforeMethod
  5. @Test
  6. @AfterMethod
  7. @AfterClass
  8. @AfterTest
  9. @AfterSuite

Below is a little example to demostrate the above hirerachy –

package Test;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
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 CodekruTest {

	@BeforeSuite
	public void beforeSuiteMethod() {
		System.out.println("before Suite");
		Assert.assertTrue(true);
	}

	@BeforeTest
	public void beforeTestMethod() {
		System.out.println("before Test");
		Assert.assertTrue(true);
	}

	@BeforeClass
	public void beforeClassMethod() {
		System.out.println("before Class");
		Assert.assertTrue(true);
	}

	@BeforeMethod
	public void beforeMethod() {
		System.out.println("before Method");
		Assert.assertTrue(true);
	}

	@AfterSuite
	public void afterSuiteMethod() {
		System.out.println("after Suite");
		Assert.assertTrue(true);
	}

	@AfterTest
	public void afterTestMethod() {
		System.out.println("after Test");
		Assert.assertTrue(true);
	}

	@AfterClass
	public void afterClassMethod() {
		System.out.println("after Class");
		Assert.assertTrue(true);
	}

	@AfterMethod
	public void afterMethod() {
		System.out.println("after Method");
		Assert.assertTrue(true);
	}

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

}

Now, we will run below testng.xml file and will see what happens and the order in which the output is printed

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

Output –

before Suite
before Test
before Class
before Method
Executing the test
after Method
after Class
after Test
after Suite

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

Reference – https://testng.org/doc/

Hope you liked this article. If you have any doubts or concerns, please feel free to write us in comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *