testng @Test Annotation Quick example on @Test annotation


Example

@Test annotation can be applied to any class or method. This annotation marks a class or a method as part of the test.

  1. @Test at method level - mark annotated method as test method
  2. @Test at class level
    • The effect of a class level @Test annotation is to make all the public methods of the class to become test methods even if they are not annotated.
    • @Test annotation can also be repeated on a method if you want to add certain attributes.

Example of @Test at method level:

import org.testng.annotations.Test;

public class TestClass1 {
    public void notTestMethod() {
    }

    @Test
    public void testMethod() {
    }
}

Example of @Test at class level:

import org.testng.annotations.Test;

@Test
public class TestClass2 {
    public void testMethod1() {
    }

    @Test
    public void testMethod2() {
    }
}