TestNG Annotations : Part 1 - rohitkrbhardwaj09/TestNG_ByRSA GitHub Wiki
- In TestNG XML, the
<suite>
tag wraps all `` blocks. - It represents the entire test suite (project-level).
Annotation | Purpose |
---|---|
@BeforeSuite |
Runs once before any test classes/methods in the suite. |
@AfterSuite |
Runs once after all tests in all test folders. |
- 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
- 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.
@BeforeSuite
public void setupSuiteEnv() {
System.out.println("I am number one (BeforeSuite)");
// Example: Load config.properties or start DB connection pool
}
@AfterSuite
public void tearDownSuiteEnv() {
System.out.println("I am the number one from last (AfterSuite)");
// Example: Close DB connections or flush reports
}
-
@BeforeSuite
executes before any `` tag in TestNG XML -
@AfterSuite
executes after all `` tags are completed
Each test case in a class is a Java method annotated with @Test
.
Annotation | Purpose |
---|---|
@BeforeMethod |
Runs before every test method in a class |
@AfterMethod |
Runs after every test method in a class |
- 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
- Selenium WebDriver: Delete cookies before each test to prevent test data overlap.
- REST APIs: Revalidate auth tokens or headers before each request.
- Mobile Automation: Reset the app to home screen before each test method.
@BeforeMethod
public void beforeEachTest() {
System.out.println("Before every test method");
// Example: Clear browser cookies or reset DB state
}
@AfterMethod
public void afterEachTest() {
System.out.println("After every test method");
// Example: Log out user or capture logs
}
Given 4 test methods in a class:
@Test public void login() {}
@Test public void search() {}
@Test public void checkout() {}
@Test public void logout() {}
Execution order:
@BeforeMethod
login()
@AfterMethod
@BeforeMethod
search()
@AfterMethod
...
- Assuming
@BeforeMethod
runs once — it runs before every test method. - Thinking
@AfterSuite
waits for other files — it only runs after entire suite. - Confusing XML scope (suite/test) with class/method scope.
Annotation | Executes... | Scope Type |
---|---|---|
@BeforeSuite |
Once before entire suite | Global (XML level) |
@AfterSuite |
Once after entire suite | Global (XML level) |
@BeforeTest |
Once before a <test> block |
XML <test> tag |
@AfterTest |
Once after a <test> block |
XML <test> tag |
@BeforeMethod |
Before each test method in a class | Class-level |
@AfterMethod |
After each test method in a class | Class-level |
❓"What is the difference between
@BeforeTest
and@BeforeMethod
?"
@BeforeTest |
@BeforeMethod |
---|---|
Runs once before all tests in <test>
|
Runs before each test method |
XML-level scope | Java class-level scope |
-
@BeforeSuite
and@AfterSuite
are for suite-wide setup/teardown -
@BeforeMethod
and@AfterMethod
are for per-test-case setup/teardown - Choose annotations based on where you want the control and scope
Stay tuned for:
-
@BeforeClass
and@AfterClass
explanation - Final piece of understanding annotation hierarchy