How @Test Annotation In TestNG works At Class Level

@Test Annotation In TestNG: If you have noticed, whenever we have used the @test annotation, we always use it at the method level, but today, we are going to learn how we can use the @test annotation on the class level.

Suppose a TestNG class has 5 methods, and you want to run all 5 methods; then, we can use the @test annotation in all the test methods. But if a class has a huge number of test cases, adding @test annotation is a little bit difficult; in that case, we can use the @test annotation at the class level.

@Test Annotation In Class Level

If you define @test annotation at the class level, then all the methods of that class will be treated as @test annotated methods by default. Let us take a simple example and Find out how this works:

package TestAnnotation;
import org.testng.annotations.Test;
@Test
public class TestEx1 
{
   public void m1()
   {
      System.out.println("M1");
   }
   public void m2()
   {
      System.out.println("M2");
   }
   public void m3()
   {
      System.out.println("M3");
   }
   public void m4()
   {
      System.out.println("M4");
   }
}

Output:

M1
M2
M3
M4

When we executed the suite, we found that all the methods inside the class would be executed. But as TestNg provides so many other TestNG attributes & annotations like @beforemethod, @aftermethod, @Beforeclass, and @afterclass like this,

At that time, one question that will come to our mind is whether the behavior of the methods is as per the mentioned annotation on the method level or whether the methods behave like the @test annotation. Let us try to understand this scenario with a simple example:

package com.softwaretestingo.testng.annotation;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@Test
public class TestAnnotationEx2 
{
	@BeforeClass
	public void m1()
	{
		System.out.println("M1");
	}

	public void m2()
	{
		System.out.println("M2");
	}

	public void m3()
	{
		System.out.println("M3");
	}

	@AfterClass
	public void m4()
	{
		System.out.println("M4");
	}
}

Output:

M1
M2
M3
M4
PASSED: com.softwaretestingo.testng.annotation.TestAnnotationEx2.m2
PASSED: com.softwaretestingo.testng.annotation.TestAnnotationEx2.m3

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

If you run the test suite of the above TestNG class, we will find out the test count is 2. That means if @test annotation is applied at the class level but inside the class for some specific method, if you have used some other annotation, then the method-level annotations override the class-level annotation.

How does @test class level work with Inheritance?

Suppose we have a Super Class with class-level @test annotation.

package com.softwaretestingo.testng.annotation;
import org.testng.annotations.Test;
@Test
public class SuperTestAnnotationClass 
{
	public void m1()
	{
		System.out.println("M1");
	}

	public void m2()
	{
		System.out.println("M2");
	}

	public void m3()
	{
		System.out.println("M3");
	}
	
	public void m4()
	{
		System.out.println("M4");
	}
}

Child Class:

package com.softwaretestingo.testng.annotation;
public class ChildTestAnnotationClass extends SuperTestAnnotationClass 
{
	public void m5()
	{
		System.out.println("M5");
	}

	public void m6()
	{
		System.out.println("M6");
	}
}

Output:

M5
M6
M1
M2
M3
M4

If you see the above code, you can see that we have mentioned @test on the class level for the superclass, and the child class is extending the superclass, but there is no @test annotation in the child class.

But when we run the child class test suite, all the child class methods get executed, which means that because of the inheritance feature, the child class methods are automatically annotated by the @test method.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment