Discover how a few simple annotations can save you hours of tedious test setup and cleanup!
Why TestNG annotations (@Test, @BeforeMethod, @AfterMethod) in Selenium Java? - Purpose & Use Cases
Imagine running a web test where you have to open the browser, log in, check a feature, then close the browser manually every single time you want to test something new.
You write the same setup and cleanup steps again and again for each test.
This manual way is slow and boring.
You might forget to close the browser or skip logging in, causing errors.
Repeating code wastes time and makes tests hard to maintain.
TestNG annotations like @Test, @BeforeMethod, and @AfterMethod automate setup and cleanup.
You write the login once in @BeforeMethod, the logout or close browser in @AfterMethod, and your test logic in @Test.
TestNG runs them in order for each test automatically.
public void testFeature() {
openBrowser();
login();
checkFeature();
logout();
closeBrowser();
}@BeforeMethod
public void setup() {
openBrowser();
login();
}
@Test
public void testFeature() {
checkFeature();
}
@AfterMethod
public void cleanup() {
logout();
closeBrowser();
}This lets you write clean, reusable tests that run smoothly and reliably every time.
Think of testing a shopping cart: you open the site and log in before each test, add items in the test, then log out and close the browser after.
TestNG annotations handle this flow automatically.
Manual test setup and cleanup is repetitive and error-prone.
TestNG annotations automate running setup before and cleanup after each test.
This makes tests easier to write, read, and maintain.
Practice
@BeforeMethod annotation in TestNG?Solution
Step 1: Understand the role of
This annotation is used to run a method before each test method to prepare or set up the environment.@BeforeMethodStep 2: Differentiate from other annotations
@Testmarks test methods,@AfterMethodruns 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 AQuick Check:
@BeforeMethod= setup before each test [OK]
- Confusing @BeforeMethod with @BeforeClass
- Thinking @BeforeMethod runs only once
- Mixing @BeforeMethod with @AfterMethod
Solution
Step 1: Identify the annotation for test methods
@Testis 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 BQuick Check:
@Testmarks test methods [OK]
- Using @BeforeMethod or @AfterMethod instead of @Test
- Missing parentheses in method declaration
- Writing 'test' instead of '@Test'
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?Solution
Step 1: Understand TestNG execution flow
For each@Testmethod,@BeforeMethodruns before it and@AfterMethodruns 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 DQuick Check:
BeforeMethod and AfterMethod wrap each test [OK]
- Assuming BeforeMethod runs once before all tests
- Assuming AfterMethod runs once after all tests
- Mixing order of print statements
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")
}
}Solution
Step 1: Check syntax in cleanup() method
The lineSystem.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 AQuick Check:
Java statements must end with semicolon [OK]
- Confusing @AfterMethod with @AfterTest
- Thinking test methods must return boolean
- Assuming setup() must be static
Solution
Step 1: Identify annotation to run once before all tests
@BeforeClassruns once before any test methods in the class.Step 2: Identify annotation to run after each test method
@AfterMethodruns after each test method to clean up.Final Answer:
@BeforeClass for setup, @AfterMethod for cleanup -> Option CQuick Check:
BeforeClass = once before all, AfterMethod = after each test [OK]
- Using BeforeMethod for setup once only
- Using AfterClass for cleanup after each test
- Confusing BeforeTest with BeforeClass
Ready to prove it?
Pass the test and this pattern moves unknown
