Class level annotations in TestNG

Do you know that TestNG provides us with a great feature that allows us to define the annotations at the class level instead of the method level? In this post, we are going to discuss the class-level annotations in detail.

Let’s just say that we have 100 methods in one class and writing @Test annotation on each of these methods could be quite cumbersome. Instead, what we can do is just writing the @Test annotation at the class level and this would make all of the methods present in the class to be the test methods, even if they are not annotated.

Below is our CodekruTest class where we will be using the @Test annotation at the class level

package Test;

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

@Test
public class CodekruTest {

	public void gammaMethod() {
		System.out.println("Executing gammaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	public void alphaMethod() {
		System.out.println("Executing alphaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	public void betaMethod() {
		System.out.println("Executing betaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	public void deltaMethod() {
		System.out.println("Executing deltaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

}

And below is the XML file that would run our CodekruTest class

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

Output after running the above XML file –

Executing alphaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
Executing deltaMethod in CodekruTest class
Executing gammaMethod in CodekruTest class
Now, what if we used an attribute with @Test annotation at the class level? Would that also be propagated to all of the test methods inside the class?

Let’s try to do this while using the invocationCount attribute here with the @Test annotation

package Test;

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

@Test(invocationCount =  2)
public class CodekruTest {

	public void gammaMethod() {
		System.out.println("Executing gammaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	public void alphaMethod() {
		System.out.println("Executing alphaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	public void betaMethod() {
		System.out.println("Executing betaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	public void deltaMethod() {
		System.out.println("Executing deltaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

}

Again run the same XML file and below will be the output –

Executing alphaMethod in CodekruTest class
Executing alphaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
Executing deltaMethod in CodekruTest class
Executing deltaMethod in CodekruTest class
Executing gammaMethod in CodekruTest class
Executing gammaMethod in CodekruTest class

And here we can see that each test method was executed two times.

What if we put the @Test annotation at the class level and at the method level too?

Here the test method will have both the properties of the annotation at the class level and that of the method level as shown with the below example

package Test;

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

@Test
public class CodekruTest {

	@Test(invocationCount =  2)
	public void gammaMethod() {
		System.out.println("Executing gammaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	public void alphaMethod() {
		System.out.println("Executing alphaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	public void betaMethod() {
		System.out.println("Executing betaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	public void deltaMethod() {
		System.out.println("Executing deltaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

}

After running the same XML file

Executing alphaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
Executing deltaMethod in CodekruTest class
Executing gammaMethod in CodekruTest class
Executing gammaMethod in CodekruTest class

Here we can see that only gammaMethod was executed twice.

But now, what if we had the same attribute at the class level and on the method level too? What will happen then?

Here the method level attribute value will override the class level attribute value as shown with the below example

package Test;

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

@Test(invocationCount = 2)
public class CodekruTest {

	@Test(invocationCount =  1)
	public void gammaMethod() {
		System.out.println("Executing gammaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	public void alphaMethod() {
		System.out.println("Executing alphaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	public void betaMethod() {
		System.out.println("Executing betaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

	public void deltaMethod() {
		System.out.println("Executing deltaMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

}

Output after running the same XML file –

Executing alphaMethod in CodekruTest class
Executing alphaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
Executing deltaMethod in CodekruTest class
Executing deltaMethod in CodekruTest class
Executing gammaMethod in CodekruTest class

Here we can see that all of the test methods was executed twice except gammaMethod, which was executed only once.

Hope you have liked the 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.