TestNG is an open-source test automation framework for Java. It is developed on the same lines of JUnit and NUnit. Few advanced and useful features provided by TestNG makes it a more robust framework compared to its peers. The NG in TestNG stands for ‘Next Generation’.
TestNG: Annotations
An annotation is a tag that provides information about the method, class, and suite. It helps to define the execution approach of your test cases and the different features associated with it. Below are the major annotations used:
- @Test– This is the root of TestNG test cases. In order to use TestNG, all methods should be annotated with this annotation
@Test
public void setupTestNG()
{
System.out.println(“this is a test annotation method”)
}
Few attributes associated with the TestNG annotation are:
Description: You can describe your test case under the description, stating what it does
@Test(description=”This test validates login functionality”)
Priority: You can prioritize the order of your test methods by defining a priority. Based on the defined priority, the test shall execute in that order.
@Test(priority=1)
DependsOnMethod: This attribute works miracles if one test case is dependent on the other test case. For example, to view your profile details, you need to login to the application. So, your profile test case is dependent on the login test case
@Test(dependsOnMethod=”Login”)
Enabled: Using this attribute, you can choose to execute or skip the execution of this test case. Setting it to true execute it and setting it to false will skip the test from the execution cycle
@Test(enabled=’true’)
Groups: Using this attribute, you can club your test cases into a single group and specify the specified group you wish to execute in your TestNG XML file. The test cases clubbed to that group will only be executed and the rest will be skipped
@Test(groups=”Smoke Suite”)
While the above ones should help you get started, other major annotations are:
- @BeforeMethod and @AfterMethod – These annotations run before and after each test method
- @BeforeClass and @AfterClass – These annotations run once before and after the first @test method in a class
- @BeforeTest and @AfterTest – The BeforeTest annotation runs before the @BeforeClass annotation and the AfterTest annotation runs after the @AfterClass annotation
- @BeforeSuite and @AfterSuite– These annotations run before and after any test annotated method in a class respectively. These annotations start the beginning of a test and the end of it, for all the classes in a suite
Talking about the execution order of these annotations, they execute in the below order:
@BeforeSuite -> @BeforeTest -> @BeforeClass -> @BeforeMethod -> @Test -> @AfterMethod -> @AfterClass -> @AfterCTest -> @AfterSuite
Reference:
https://www.browserstack.com/guide/testng-framework-with-selenium-automation