TestNG
Annotation
Annotation
|
Description
|
@BeforeSuite
|
The
annotated method will be run only once before all tests in this
suite have run.
|
@AfterSuite
|
The
annotated method will be run only once after all tests in this
suite have run.
|
@BeforeClass
|
The
annotated method will be run only once before the first test
method in the current class is invoked.
|
@AfterClass
|
The
annotated method will be run only once after all the test methods
in the current class have been run.
|
@BeforeTest
|
The
annotated method will be run before any test method belong ing to
the classes inside the
|
@AfterTest
|
The
annotated method will be run after all the test methods belong ing
to the classes inside the
|
@BeforeGroups
|
The
list of g roups that this config uration method will run before. T
his method is g uaranteed to run shortly before the first test
method that belong s to any of these g roups is invoked.
|
@AfterGroups
|
The
list of g roups that this config uration method will run after. T
his method is g uaranteed to run shortly after the last test
method that belong s to any of these g roups is invoked.
|
@BeforeMethod
|
The
annotated method will be run before each test method.
|
@AfterMethod
|
The
annotated method will be run after each test method.
|
@DataProvider
|
Marks
a method as supplying data for a test method. The annotated method
must return an Object[][] where each Object[] can be assig ned the
parameter list of the test method. T he @T est method that wants
to receive data from this DataProvider needs to use a dataProvider
name equals to the name of this annotation.
|
@Factory
|
Marks
a method as a factory that returns objects that will be used by T
estNG as Test classes. T he method must return Object[].
|
@Listeners
|
Defines
listeners on a test class.
|
@Parameters
|
Describes
how to pass parameters to a @T est method.
|
@Test
|
Marks
a class or a method as part of the test.
|
Note: alwaysRun, If set to true, this test method will always be run even if it depends on a method that failed.
public class AnnotationsTest {
@BeforeSuite(alwaysRun = true)
public static void beforeSuite() {
System.out.println("@BeforeSuite");
}
@BeforeTest(alwaysRun = true)
public static void beforeTest() {
System.out.println("@BeforeTest");
}
@BeforeClass(alwaysRun = true)
public static void beforeClass() {
System.out.println("@BeforeClass");
}
@BeforeMethod(alwaysRun = true)
public static void beforeMethod() {
System.out.println("@BeforeMethod");
}
@Test
public void test() {
System.out.println("Test");
}
@Test
public void test2() {
System.out.println("Test2");
}
@AfterMethod(alwaysRun = true)
public static void afterMethod() {
System.out.println("@AfterMethod");
}
@AfterClass(alwaysRun = true)
public static void afterClass() {
System.out.println("@AfterClass");
}
@AfterTest(alwaysRun = true)
public static void afterTest() {
System.out.println("@AfterTest");
}
@AfterSuite(alwaysRun = true)
public static void afterSuite() {
System.out.println("@AfterSuite");
}
}
@BeforeSuite(alwaysRun = true)
public static void beforeSuite() {
System.out.println("@BeforeSuite");
}
@BeforeTest(alwaysRun = true)
public static void beforeTest() {
System.out.println("@BeforeTest");
}
@BeforeClass(alwaysRun = true)
public static void beforeClass() {
System.out.println("@BeforeClass");
}
@BeforeMethod(alwaysRun = true)
public static void beforeMethod() {
System.out.println("@BeforeMethod");
}
@Test
public void test() {
System.out.println("Test");
}
@Test
public void test2() {
System.out.println("Test2");
}
@AfterMethod(alwaysRun = true)
public static void afterMethod() {
System.out.println("@AfterMethod");
}
@AfterClass(alwaysRun = true)
public static void afterClass() {
System.out.println("@AfterClass");
}
@AfterTest(alwaysRun = true)
public static void afterTest() {
System.out.println("@AfterTest");
}
@AfterSuite(alwaysRun = true)
public static void afterSuite() {
System.out.println("@AfterSuite");
}
}
Output:
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
Test
@AfterMethod
@BeforeMethod
Test2
@AfterMethod
@AfterClass
@AfterTest
@AfterSuite