TestNG Annotations With Concepts
TestNG Annotations With Concepts
1. @Test
Usage: This is the most important annotation in TestNG. The method annotated with @Test
will be executed as a test case.
Example:
@Test
Attributes:
2. @BeforeSuite
Example:
@BeforeSuite
3. @AfterSuite
Example:
@AfterSuite
4. @BeforeTest
Example:
@BeforeTest
5. @AfterTest
Example:
@AfterTest
6. @BeforeClass
Example:
@BeforeClass
7. @AfterClass
Purpose: Executes after all methods in the current class.
Example:
@AfterClass
8. @BeforeMethod
Example:
@BeforeMethod
9. @AfterMethod
Example:
@AfterMethod
10. @DataProvider
Usage: It can be used to run a test method multiple times with different data.
Example:
@DataProvider(name = "data-provider")
public Object[][] dataProviderMethod() {
{ "data1" }, { "data2" }
};
@Test(dataProvider = "data-provider")
11. @Listeners
Example:
@Listeners(MyListener.class)
12. @Factory
Example:
@Factory
}
13. @DependsOnGroups
Purpose: Specifies the groups that must be executed before the current test.
Example:
@Test(dependsOnGroups = "group1")
TestNG Methods
1. setUp()
Example:
@BeforeMethod
// Initialization code
2. tearDown()
Example:
@AfterMethod
// Cleanup code
Configuration Methods
1. setParallel()
Example:
xml
Copy code
<test name="Test">
<classes>
<class name="MyTestClass"/>
</classes>
</test>
</suite>
Assertions
1. Assert.assertEquals(actual, expected)
Example:
Assert.assertEquals(actualValue, expectedValue);
2. Assert.assertTrue(condition)
Example:
3. Assert.assertFalse(condition)
Example:
4. Assert.assertNotNull(object)
Example:
Assert.assertNotNull(object, "Object is null");
Example:
xml
Copy code
<suite name="Suite">
<test name="Test">
<classes>
<class name="MyTestClass"/>
</classes>
</test>
</suite>
Execution Order
1. @BeforeSuite
2. @BeforeTest
3. @BeforeClass
4. @BeforeMethod
5. @Test
6. @AfterMethod
7. @AfterClass
8. @AfterTest
9. @AfterSuite