Quick Tips
2 min
TestNG Annotations Cheat Sheet
Quick reference for TestNG annotations and their usage
...
testngjavatestingannotations
TestNG Annotations Quick Reference
Execution Order
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@AfterTest
@AfterSuite1. Test Configuration Annotations
import org.testng.annotations.*;
public class TestNGExample {
@BeforeSuite
public void beforeSuite() {
// Runs once before all tests in suite
System.out.println("Setup test suite");
}
@BeforeTest
public void beforeTest() {
// Runs once before all tests in <test> tag
System.out.println("Setup test");
}
@BeforeClass
public void beforeClass() {
// Runs once before first test in class
System.out.println("Initialize test class");
}
@BeforeMethod
public void beforeMethod() {
// Runs before each @Test method
System.out.println("Setup test method");
}
@Test
public void testExample() {
System.out.println("Running test");
}
@AfterMethod
public void afterMethod() {
// Runs after each @Test method
System.out.println("Cleanup after test");
}
@AfterClass
public void afterClass() {
// Runs once after all tests in class
System.out.println("Cleanup test class");
}
@AfterTest
public void afterTest() {
// Runs once after all tests in <test> tag
System.out.println("Cleanup test");
}
@AfterSuite
public void afterSuite() {
// Runs once after all tests in suite
System.out.println("Cleanup test suite");
}
}2. @Test Annotation Parameters
public class TestParameters {
// Basic test
@Test
public void simpleTest() {
// Test code
}
// Set priority (lower runs first)
@Test(priority = 1)
public void loginTest() { }
@Test(priority = 2, dependsOnMethods = "loginTest")
public void dashboardTest() { }
// Set description
@Test(description = "Verify user login with valid credentials")
public void testLogin() { }
// Enable/disable test
@Test(enabled = false)
public void skipThisTest() { }
// Set timeout (milliseconds)
@Test(timeOut = 5000)
public void testWithTimeout() {
// Must complete within 5 seconds
}
// Expected exception
@Test(expectedExceptions = IllegalArgumentException.class)
public void testException() {
throw new IllegalArgumentException("Expected");
}
// Invocation count (run multiple times)
@Test(invocationCount = 3)
public void testRepeated() {
// Runs 3 times
}
// Thread pool for parallel execution
@Test(invocationCount = 5, threadPoolSize = 2)
public void testParallel() {
// Runs 5 times using 2 threads
}
}3. Dependency Annotations
public class TestDependencies {
@Test
public void serverStarted() {
System.out.println("Start server");
}
// Depends on single method
@Test(dependsOnMethods = "serverStarted")
public void testAPI() {
System.out.println("Test API");
}
// Depends on multiple methods
@Test(dependsOnMethods = {"serverStarted", "testAPI"})
public void cleanup() {
System.out.println("Cleanup");
}
// Depends on groups
@Test(dependsOnGroups = "sanity")
public void fullRegressionTest() {
System.out.println("Full regression");
}
// Always run even if dependencies fail
@Test(dependsOnMethods = "serverStarted", alwaysRun = true)
public void alwaysExecute() {
System.out.println("Always runs");
}
}4. Groups and Categories
public class TestGroups {
@Test(groups = "smoke")
public void loginTest() { }
@Test(groups = "smoke")
public void logoutTest() { }
@Test(groups = "regression")
public void detailedTest() { }
@Test(groups = {"smoke", "regression"})
public void criticalTest() { }
// Run before group
@BeforeGroups("smoke")
public void setupSmoke() {
System.out.println("Setup smoke tests");
}
// Run after group
@AfterGroups("smoke")
public void cleanupSmoke() {
System.out.println("Cleanup smoke tests");
}
}5. Data Provider
public class DataProviderTests {
// Data provider method
@DataProvider(name = "loginData")
public Object[][] getLoginData() {
return new Object[][] {
{"user1", "pass1"},
{"user2", "pass2"},
{"user3", "pass3"}
};
}
// Use data provider
@Test(dataProvider = "loginData")
public void testLogin(String username, String password) {
System.out.println("Testing: " + username);
// Test with provided data
}
// Data provider from another class
@Test(dataProvider = "testData",
dataProviderClass = TestDataProvider.class)
public void testWithExternalData(String data) {
System.out.println("Data: " + data);
}
// Parallel data provider
@DataProvider(name = "parallelData", parallel = true)
public Object[][] getParallelData() {
return new Object[][] {
{"test1"},
{"test2"},
{"test3"}
};
}
}6. Parameters from testng.xml
public class ParameterTests {
@Parameters({"browser", "url"})
@Test
public void testWithParams(String browser, String url) {
System.out.println("Browser: " + browser);
System.out.println("URL: " + url);
}
@Parameters("environment")
@BeforeClass
public void setup(String env) {
System.out.println("Environment: " + env);
}
}testng.xml:
<suite name="Test Suite">
<test name="Chrome Test">
<parameter name="browser" value="chrome"/>
<parameter name="url" value="https://example.com"/>
<parameter name="environment" value="staging"/>
<classes>
<class name="com.example.ParameterTests"/>
</classes>
</test>
</suite>7. Listeners
import org.testng.ITestListener;
import org.testng.ITestResult;
@Listeners(TestListener.class)
public class ListenerExample {
@Test
public void testMethod() { }
}
public class TestListener implements ITestListener {
@Override
public void onTestStart(ITestResult result) {
System.out.println("Test started: " + result.getName());
}
@Override
public void onTestSuccess(ITestResult result) {
System.out.println("Test passed: " + result.getName());
}
@Override
public void onTestFailure(ITestResult result) {
System.out.println("Test failed: " + result.getName());
// Take screenshot, log error, etc.
}
@Override
public void onTestSkipped(ITestResult result) {
System.out.println("Test skipped: " + result.getName());
}
}Quick Reference Table
| Annotation | Scope | Purpose |
|---|---|---|
| @BeforeSuite | Suite | Runs once before all tests |
| @BeforeTest | Test tag | Runs before <test> tag |
| @BeforeClass | Class | Runs before first test in class |
| @BeforeMethod | Method | Runs before each test |
| @Test | Method | Marks method as test |
| @AfterMethod | Method | Runs after each test |
| @AfterClass | Class | Runs after all tests in class |
| @AfterTest | Test tag | Runs after <test> tag |
| @AfterSuite | Suite | Runs once after all tests |
| @DataProvider | Method | Provides test data |
| @Parameters | Method | Gets params from XML |
Key Takeaways
- Use
@BeforeMethodand@AfterMethodfor test setup/teardown - Set
priorityto control test execution order - Use
dependsOnMethodsfor test dependencies - Organize tests with
groupsfor selective execution - Parameterize tests with
@DataProvideror@Parameters - Use
@Listenersfor custom test reporting
Comments (0)
Loading comments...