CLASS LEVEL ANNOTATIONS IN TESTNG

Class level annotations in TestNG:

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

Let’s say there are 20 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.

Example: @Test annotation at class level:

package Annotations;

import org.testng.annotations.Test;@Test
public class ClassLevelAnnotations {

	public void Register() {
		System.out.println("===Register TestCase Executed===");
	}

	public void Login() {
		System.out.println("===Login TestCase Executed===");
		int x = 10 / 0;
	}

	@Test(dependsOnMethods = {
		"Login"
	})
	public void LogOut() {
		System.out.println("===LogOut TestCase Executed===");
	}
}

In the above example we have added @Test annotation only to the last method with attribute ‘dependsOnMethods‘. 

Output:

[TestNG] Running:
  C:\Users\Sujoy\AppData\Local\Temp\testng-eclipse-405249106\testng-customsuite.xml

===Login TestCase Executed===
===Register TestCase Executed===
[Utils] Attempting to create D:\WorkSpace\DemoTestNG\test-output\Default suite\Default test.xml
[Utils]   Directory D:\WorkSpace\DemoTestNG\test-output\Default suite exists: true
PASSED: Register
FAILED: Login
java.lang.ArithmeticException: / by zero

LogOut() is skipped as it depends on Login() which is failed due to ArithmeticException.

Leave a Reply

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