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;
@Test
public class ClassLevelAnnotations {
@Test
public void methodOne() {
System.out.println("Helloo.. Im in method adding numbers");
}
@Test
public void MethodTwo() {
System.out.println("Helloo.. Im in method divided by zero");
int e = 1 / 0;
}
@Test(dependsOnMethods={"MethodTwo"})
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(ClassLevelAnnotations.java:14)
......
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
SKIPPED: methodThree
===============================================
Default suite
Total tests run: 3, Failures: 1, Skips: 1
===============================================
Comments
different error
Hi,
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