TestNG Annotations : Part 1 - rohitkrbhardwaj09/TestNG_ByRSA GitHub Wiki

📊 TestNG Annotations: @BeforeSuite, @AfterSuite, @BeforeMethod, @AfterMethod


🔹 @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:

@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
}

📜 Scope:

  • @BeforeSuite executes before any `` tag in TestNG XML
  • @AfterSuite executes after all `` tags are completed

🔹 @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 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.

📂 Sample Code:

@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
}

🧰 Output Sample:

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
...

🚫 Common Mistakes

  • 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.

🌟 Real-World Comparison Table

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

📅 Interview Tip:

❓"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

🚀 Summary

  • @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

🔮 Next Topic

Stay tuned for:

  • @BeforeClass and @AfterClass explanation
  • Final piece of understanding annotation hierarchy
⚠️ **GitHub.com Fallback** ⚠️