TestNG Basics


TestNG Annotations
  
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
  
@Test: Denotes a test method.
@BeforeClass: Runs once before the first test method in the current class.
@AfterClass: Runs once after all the test methods in the current class.
@BeforeMethod: Runs before each test method.
@AfterMethod: Runs after each test method.
@BeforeSuite: Runs once before all tests in the suite.
@AfterSuite: Runs once after all tests in the suite.
@BeforeTest: Runs once before all tests within the current  tag.
@AfterTest: Runs once after all tests within the current  tag.
  
  
TestNG Assertions:
  
import org.testng.Assert;

Assert.assertEquals(actual, expected);
Assert.assertTrue(condition);
Assert.assertFalse(condition);
Assert.assertNull(object);
Assert.assertNotNull(object);
  
  
TestNG Parameters:
  
import org.testng.annotations.Parameters;
import org.testng.annotations.Optional;

@Parameters({ "parameterName" })
@Test
public void testMethod(@Optional("defaultValue") String parameterValue) {
    // Test method implementation
}
  
  
TestNG Data Providers:
import org.testng.annotations.DataProvider;

@DataProvider(name = "dataProviderName")
public Object[][] dataProviderMethod() {
    // Return test data as a 2D array
    return new Object[][] { { data1 }, { data2 }, ... };
}

@Test(dataProvider = "dataProviderName")
public void testMethod(Object data) {
    // Test method implementation
}
  
  
TestNG Groups:
@Test(groups = "groupName")
public void testMethod() {
    // Test method implementation
}
  
TestNG Dependencies:
@Test(dependsOnMethods = "methodName")
public void testMethod() {
    // Test method implementation
}