testng @Test Annotation Quick example on @Test annotation

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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() {
    }
}


Got any testng Question?