Welcome to BhauAutomation!
Want to make your Selenium automation tests easier to manage? TestNG is a Java testing framework that helps you write, organise, and execute test cases.
What Is TestNG?
TestNG is a testing framework used to run automated test cases and check whether they pass or fail.
For example, you can use TestNG to test login functionality, search features, and other website functions.
Why Do We Use TestNG?
- Organise test cases.
- Run multiple tests together.
- Check results using assertions.
- Execute tests in a specific order.
- Perform data-driven testing.
- Generate test reports.
- Support parallel and cross-browser testing.
Simple TestNG Example
Let’s create a simple test that checks a login message.
Java Code
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
public class MyFirstTestNG {
@BeforeMethod
public void setUp() {
System.out.println("Opening Browser");
}
@Test
public void loginTest() {
System.out.println("Login Successful");
}
@AfterMethod
public void tearDown() {
System.out.println("Closing Browser");
}
}
Expected Output
Opening Browser
Login Successful
Closing Browser
Simple Explanation
@BeforeMethod– Runs before each test method.@Test– Marks a method as a test case.@AfterMethod– Runs after each test method.
This helps us prepare the test, execute it, and clean up afterwards.
What Should You Learn Next?
- TestNG Installation
- TestNG Annotations
- TestNG Assertions
- Test Priorities
- DataProvider
- Test Dependencies
- Cross-Browser Testing
- TestNG Reports
Conclusion
TestNG makes automation testing easier by helping you organise and manage test cases. Start with simple examples and gradually practise with Selenium projects.
Keep Learning with BhauAutomation!