Skip to content Skip to navigation
Check Our New Demo Website!

Class level annotations in TestNG

TestNG has great feature to define annotations on a class instead of a each test method.

If say suppose there are 10 test methods, where adding @Test on a class level is simpler than adding @Test for each method.

When we make class level @Test annotation, all the public methods of this class will become test methods even if they are not annotated.

We can still define @Test annotation on of the method if we want to add any attributes to particular test method.

NOTE: In the same way, we can also define groups "ex: @Test(groups = { "regression" })" at the class level and
then add groups at the individual method level:

Below is the sample program using @Test annotation at class level:

package com.example.classlevel;

import org.testng.annotations.Test;

public class ClassLevelAnnotations {

	public void methodOne() {
		System.out.println("Helloo.. Im in method adding numbers");

	public void MethodTwo() {
		System.out.println("Helloo.. Im in method divided by zero");
		int e = 1 / 0;

	public void methodThree() {
		System.out.println("Helloo.. Im in method skip");

In the above example we have added @Test annotation only to the last method with attribute 'dependsOnMethods'. methodThree() will be skipped as it depends on MethodTwo() which is failed due to ArithmeticException.

The below is the output:

Helloo.. Im in method divided by zero
Helloo.. Im in method adding numbers
PASSED: methodOne
FAILED: MethodTwo
java.lang.ArithmeticException: / by zero
	at com.example.classlevel.ClassLevelAnnotations.MethodTwo(
	at org.testng.remote.RemoteTestNG.main(

SKIPPED: methodThree

Default suite
Total tests run: 3, Failures: 1, Skips: 1
Test Frameworks: 


I got a different when performing this:
TestNGPackage.DemoFour.methodThree() is depending on method public void TestNGPackage.DemoFour.methodTwo(), which is not annotated with @Test or not included.

Am I doing something wrong?

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Enter the characters shown in the image.