TestNG Annotations : Part 3 - rohitkrbhardwaj09/TestNG_ByRSA GitHub Wiki
📊 TestNG Annotations: @BeforeSuite, @AfterSuite, @BeforeMethod, @AfterMethod, @BeforeClass, @AfterClass, and Groups
🔹 @BeforeSuite & @AfterSuite
❓ What is a Suite in TestNG?
In TestNG XML, the <suite> tag wraps all `` blocks.
It represents the entire test suite (project-level).
✅ Purpose:
Annotation
Purpose
@BeforeSuite
Runs once before any test classes/methods in the suite.
@AfterSuite
Runs once after all tests in all test folders.
📄 Example Use Cases:
Set global environment variables (e.g., ENV=QA, URL=https://test-env.com)
Start Appium Server before any mobile test execution
Load base URL or API tokens
Initialize global reports/logs
💼 Real-Time Examples:
Selenium WebDriver: Load global browser config or reporting setup before starting any test cases.
REST Assured API Testing: Load API keys or tokens once before tests and clean up after the entire suite.
Appium: Start the Appium server at the beginning and stop it at the end of the suite.
📂 Sample Code:
@BeforeSuitepublicvoidsetupSuiteEnv() {
System.out.println("I am number one (BeforeSuite)");
// Load config.properties, establish DB connection
}
@AfterSuitepublicvoidtearDownSuiteEnv() {
System.out.println("I am the number one from last (AfterSuite)");
// Flush extent reports or close DB connection
}
🔹 @BeforeMethod & @AfterMethod
❓ What is a Method in TestNG?
Each test case in a class is a Java method annotated with @Test.
✅ Purpose:
Annotation
Purpose
@BeforeMethod
Runs before every test method in a class
@AfterMethod
Runs after every test method in a class
📄 Example Use Cases:
Clear cookies (before/after each test)
Re-authenticate API keys for each API test
Open/Close browser tabs per test
Reset environment or user state before every test case
💼 Real-Time Examples:
Selenium WebDriver: Delete cookies and clear session before each test.
REST APIs: Refresh auth tokens or reset request headers.
Mobile Automation: Restart app or clear storage between each test.
📂 Sample Code:
@BeforeMethodpublicvoidbeforeEachTest() {
System.out.println("Before every test method");
// Example: driver.manage().deleteAllCookies();
}
@AfterMethodpublicvoidafterEachTest() {
System.out.println("After every test method");
// Example: driver.quit(); or logout()
}
🔹 @BeforeClass & @AfterClass
✅ Purpose:
Annotation
Purpose
@BeforeClass
Executes once before any test method in class
@AfterClass
Executes once after all test methods
📄 Example Use Cases:
Launch browser before executing tests in class
Set up database connections or configurations
Log class-wide test setup
💼 Real-Time Examples:
Selenium: Launch browser once and reuse it for all tests in that class.
REST Assured: Authenticate with server once for the class scope.
Appium: Start and stop session for class-based tests.
📂 Sample Code:
@BeforeClasspublicvoidsetupClass() {
System.out.println("Before executing any method in the class");
// driver = new ChromeDriver();
}
@AfterClasspublicvoidtearDownClass() {
System.out.println("After executing all the methods in the class");
// driver.quit();
}
🔹 Groups in TestNG
✅ Purpose:
Execute a subset of test methods grouped logically
Useful for creating suites like Smoke, Regression, Sanity
📄 How to Use:
1. Tag test methods:
@Test(groups = {"Smoke"})
publicvoidloginTest() {
System.out.println("Smoke test method");
}