TestNG annotations help organize and control test execution steps easily. They tell the test framework what to run and when.
You want to run setup code before each test, like opening a browser.
You want to run cleanup code after each test, like closing the browser.
You want to mark a method as a test case to be executed automatically.
You want to keep test code clean by separating setup, test, and teardown steps.
Syntax
Selenium Java
@BeforeMethod
public void setup() {
// code to run before each test
}
@Test
public void testExample() {
// test code
}
@AfterMethod
public void teardown() {
// code to run after each test
}
@BeforeMethod runs before each test method.
@AfterMethod runs after each test method.
Examples
This example shows simple print statements to simulate opening and closing a browser around a test.
Selenium Java
@BeforeMethod
public void openBrowser() {
System.out.println("Browser opened");
}
@Test
public void testLogin() {
System.out.println("Login test running");
}
@AfterMethod
public void closeBrowser() {
System.out.println("Browser closed");
}
A simple test method marked with @Test to run as a test case.
Selenium Java
@Test
public void testSearch() {
System.out.println("Search test running");
}
Sample Program
This test class uses @BeforeMethod to open the browser before each test, @Test to run the test, and @AfterMethod to close the browser after each test.
Selenium Java
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
public class SampleTest {
@BeforeMethod
public void setup() {
System.out.println("Setup: Open browser");
}
@Test
public void testGoogleTitle() {
System.out.println("Test: Check Google title");
// Imagine test code here
}
@AfterMethod
public void teardown() {
System.out.println("Teardown: Close browser");
}
}
OutputSuccess
Important Notes
Each test method runs with fresh setup and teardown, keeping tests independent.
Use @BeforeMethod and @AfterMethod for actions needed before and after every test.
TestNG runs methods with these annotations automatically; no need to call them yourself.
Summary
@Test marks a method as a test case.
@BeforeMethod runs before each test method to prepare the environment.
@AfterMethod runs after each test method to clean up.
Practice
(1/5)
1. What is the main purpose of the @BeforeMethod annotation in TestNG?
easy
A. To run a method before each test method to set up the test environment
B. To mark a method as a test case
C. To run a method after all test methods have finished
D. To skip a test method during execution
Solution
Step 1: Understand the role of @BeforeMethod
This annotation is used to run a method before each test method to prepare or set up the environment.
Step 2: Differentiate from other annotations
@Test marks test methods, @AfterMethod runs after each test, and skipping tests uses other annotations.
Final Answer:
To run a method before each test method to set up the test environment -> Option A
Quick Check:
@BeforeMethod = setup before each test [OK]
Hint: BeforeMethod always runs before each test method [OK]
Common Mistakes:
Confusing @BeforeMethod with @BeforeClass
Thinking @BeforeMethod runs only once
Mixing @BeforeMethod with @AfterMethod
2. Which of the following is the correct syntax to mark a method as a test case in TestNG?
easy
A. @BeforeMethod void testMethod() {}
B. @Test void testMethod() {}
C. @AfterMethod void testMethod() {}
D. test void testMethod() {}
Solution
Step 1: Identify the annotation for test methods
@Test is the correct annotation to mark a method as a test case in TestNG.
Step 2: Check syntax correctness
The method signature with @Test void testMethod() {} is valid Java and TestNG syntax.
Final Answer:
@Test void testMethod() {} -> Option B
Quick Check:
@Test marks test methods [OK]
Hint: Use @Test to mark test methods in TestNG [OK]
Common Mistakes:
Using @BeforeMethod or @AfterMethod instead of @Test
Missing parentheses in method declaration
Writing 'test' instead of '@Test'
3. Consider the following TestNG class:
public class SampleTest {
@BeforeMethod
public void setup() {
System.out.println("Setup");
}
@Test
public void test1() {
System.out.println("Test1");
}
@Test
public void test2() {
System.out.println("Test2");
}
@AfterMethod
public void cleanup() {
System.out.println("Cleanup");
}
}
What will be the order of printed lines when this test class runs?
medium
A. Test1
Test2
Setup
Cleanup
B. Setup
Cleanup
Test1
Setup
Cleanup
Test2
C. Setup
Test1
Test2
Cleanup
D. Setup
Test1
Cleanup
Setup
Test2
Cleanup
Solution
Step 1: Understand TestNG execution flow
For each @Test method, @BeforeMethod runs before it and @AfterMethod runs after it.
Step 2: Trace the print statements
First test: Setup -> Test1 -> Cleanup. Second test: Setup -> Test2 -> Cleanup.
Final Answer:
Setup
Test1
Cleanup
Setup
Test2
Cleanup -> Option D
Quick Check:
BeforeMethod and AfterMethod wrap each test [OK]
Hint: BeforeMethod and AfterMethod run around each test [OK]
Common Mistakes:
Assuming BeforeMethod runs once before all tests
Assuming AfterMethod runs once after all tests
Mixing order of print statements
4. Identify the error in the following TestNG code snippet:
public class TestExample {
@BeforeMethod
public void setup() {
System.out.println("Setup");
}
@Test
public void testMethod() {
System.out.println("Test");
}
@AfterMethod
public void cleanup() {
System.out.println("Cleanup")
}
}
medium
A. Missing semicolon after System.out.println("Cleanup")
B. Incorrect annotation @AfterMethod should be @AfterTest
C. setup() method should be static
D. testMethod() must return boolean
Solution
Step 1: Check syntax in cleanup() method
The line System.out.println("Cleanup") is missing a semicolon at the end.
Step 2: Verify other parts
Annotations are correct, methods do not need to be static, and test methods can be void.
Final Answer:
Missing semicolon after System.out.println("Cleanup") -> Option A
Quick Check:
Java statements must end with semicolon [OK]
Hint: Check for missing semicolons in Java code [OK]
Common Mistakes:
Confusing @AfterMethod with @AfterTest
Thinking test methods must return boolean
Assuming setup() must be static
5. You want to run a setup method once before all tests in a TestNG class and a cleanup method after each test method. Which annotations should you use for these methods?
hard
A. @BeforeMethod for setup, @AfterMethod for cleanup
B. @BeforeMethod for setup, @AfterClass for cleanup
C. @BeforeClass for setup, @AfterMethod for cleanup
D. @BeforeTest for setup, @AfterTest for cleanup
Solution
Step 1: Identify annotation to run once before all tests
@BeforeClass runs once before any test methods in the class.
Step 2: Identify annotation to run after each test method
@AfterMethod runs after each test method to clean up.
Final Answer:
@BeforeClass for setup, @AfterMethod for cleanup -> Option C
Quick Check:
BeforeClass = once before all, AfterMethod = after each test [OK]
Hint: Use BeforeClass once, AfterMethod after each test [OK]
Common Mistakes:
Using BeforeMethod for setup once only
Using AfterClass for cleanup after each test
Confusing BeforeTest with BeforeClass
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.