@Test Annotation In TestNG: If you have noticed whenever we have used the @test annotation, we always use in 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 have a huge number of test cases then adding @test annotation is a little bit difficult in that case we can use the @test annotation in the class level.
@Test Annotation In Class Level
If you define @test annotation in class level then all the method of that class will be treated as @test annotated method 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"); } }
When we execute the suite we have found that all the methods inside the class will be executed. But as TestNg provides so many other TestNG attributes & annotations like @beforemethod, @aftermethod, @Beforeclass, @afterclass like this.
So that time one question will come to on our mind that is the behaviour of the methods is as per the mentioned annotation on method level or methods behave like the @test annotation. Let us try to understand this scenario with a simple example:
package TestAnnotation; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @Test public class TestEx1 { @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"); } }
If you run the test suite of above TestNG class then we will find out the test count is 2. That means if @test annotation is applied in class level but inside the class for some specific method if you have used some other annotation then the method level annotations are overriding over the class-level annotation.
How does @test class level work with Inheritance?
package TestAnnotation; import org.testng.annotations.Test; @Test class superCLass { } public class TestEx1 extends superCLass { 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"); } }
If you see the above code then you can see that we have mentioned @test on class level for the super class and the child class is extending the super class but there is no @test annotation in child class.
But when we run the test suite of the child class, all the methods of child class got executed that means because of inheritance feature the child class methods are automatically annotated by @test method.
Source: Link
Leave a Reply