Site icon SumTips

List of All Annotations in TestNG and their Code Examples

While writing our first testcase with TestNG, we had seen the usage of @Test, @BeforeTest and @AfterTest annotations. Using annotations makes code easier to read, understand, and they also allow test cases to be grouped for efficiency. You could practically use them to execute a set of code for setting up variables or for cleaning up configurations after the execution of tests in a project.

Annotations also accepts parameters just like normal Java methods, making them more the useful in our scripts. Below is a list of annotations available in TestNG Framework.

Now, let’s look at a code example using the above annotations to understand the execution flow. We’ll be covering @DataProvider, @Factory, @Listeners, and @Parameters in their own post.

Annotations: Example 1

File: AnnotationsDemo.java

package TestNGDemo;

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

public class AnnotationsDemo {
	@Test
	public void testCase2() {
		System.out.println("Executed Test Case 2");
	}
	
	@Test
	public void testCase1() {
		System.out.println("Executed Test Case 1");
	}
	
	@BeforeMethod
	public void beforeMethod() {
		System.out.println("Executed Before Every Method");
	}
	
	@AfterMethod
	public void afterMethod() {
		System.out.println("Executed After Every Method");
	}

	@BeforeClass
	public void beforeClass() {
		System.out.println("Executed Before Class Method");
	}

	@AfterClass
	public void afterClass() {
		System.out.println("Executed After Class Method");
	}

	@BeforeTest
	public void beforeTest() {
		System.out.println("Executed Before Test Method");
	}

	@AfterTest
	public void afterTest() {
		System.out.println("Executed After Test Method");
	}

	@BeforeSuite
	public void beforeSuite() {
		System.out.println("Executed Before Test Suite Method");
	}

	@AfterSuite
	public void afterSuite() {
		System.out.println("Executed After Test Suite Method");
	}

}

On running the above code you should get the following as output in console:

As you can see, @Suite methods are executed first and the last. It is then followed by @BeforeTest and @BeforeClass methods, respectively. @BeforeMethod is executed twice before @Test as there are two Tests here. You can also see that testCase1() was executed before testCase2() showing us that @Test methods are executed in alphabetical order.

Extending on the example, say you are running a project that has multiple classes, the execution flow remains the same. Using annotations you can have a common Before and After method for each class or a specific method that has to be executed before every other class. Annotations gives you that control over your project. Let’s see an example that uses an XML file to run multiple test cases together.

Annotations: Example 2

File: A1.java

package TestNGDemo;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;

public class A1 {

	@BeforeSuite()
	public void BeforeSuite() {
		System.out.println("Execute Before Suite");
	}

	@AfterSuite()
	public void AfterSuite() {
		System.out.println("Execute After Suite");
	}

	@BeforeTest()
	public void BeforeTest() {
		System.out.println("Execute Before Test");
	}

	@AfterTest()
	public void AfterTest() {
		System.out.println("Execute After Test");
	}

}

File: A2.java

package TestNGDemo;

import org.testng.annotations.Test;

public class A2 {
	@Test
	public void Test1() {
		System.out.println("Executing Test1");
	}

	@Test
	public void Test2() {
		System.out.println("Executing Test2");
	}
}

File: testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="AnnotationsDemo">

	<test name="test-case1">
		<classes>
			<class name="TestNGDemo.A2" />
			<class name="TestNGDemo.A1" />
		</classes>
	</test>

	<test name="test-case2">
		<classes>
			<class name="TestNGDemo.A2" />
			<class name="TestNGDemo.A1" />
		</classes>
	</test>

</suite> 

Output:

[RemoteTestNG] detected TestNG version 6.14.2
Execute Before Suite
Execute Before Test
Executing Test1
Executing Test2
Execute After Test
Execute Before Test
Executing Test1
Executing Test2
Execute After Test
Execute After Suite

===============================================
AnnotationsDemo
Total tests run: 4, Failures: 0, Skips: 0
===============================================
Exit mobile version