Curious about TestNG annotations? Watch this video to learn all you need to know!
#TestNG #Testing #QA #Automation
Curious about TestNG annotations? Watch this video to learn all you need to know!
#TestNG #Testing #QA #Automation
TestNG annotations are like special tags or markers in the TestNG framework that define how methods should be treated during test execution. These annotations help manage the order and behavior of test methods, making the test execution process more efficient and flexible.
Basic Overview of Annotations in TestNG
Follow this guide to learn more about Annotation in detail:
Hey, here’s my take on the essential TestNG annotations:
Here’s how I’ve applied TestNG annotations in my real-world projects:
@Test
public void testLogin() {
// test code
}
This is the core of any test suite, defining what should be tested.
For instance:
@BeforeSuite
public void setupSuite() {
// setup code
}
@AfterSuite
public void teardownSuite() {
// cleanup code
}
This ensures the environment is properly prepared and cleaned up before and after the entire suite runs.
For example:
@BeforeTest
public void setupTest() {
// test setup code
}
@AfterTest
public void cleanupTest() {
// test cleanup code
}
This helps in setting up prerequisites and cleaning up resources for test groups.
@BeforeClass
public void setupClass() {
// WebDriver initialization code
}
@AfterClass
public void teardownClass() {
// WebDriver termination code
}
This ensures that the WebDriver is ready before any test methods in the class run and properly closed afterward.
@BeforeMethod
public void setupMethod() {
// setup code
}
@AfterMethod
public void cleanupMethod() {
// cleanup code
}
This is crucial for ensuring that each test runs in a consistent state.
@DataProvider(name = "userData")
public Object[][] getData() {
return new Object[][] { {"user1", "pass1"}, {"user2", "pass2"} };
}
@Test(dataProvider = "userData")
public void testLogin(String username, String password) {
// test code using username and password
}
This enables testing with different sets of input data efficiently.
For example:
@Factory
public Object[] createInstances() {
return new Object[] { new TestClass(1), new TestClass(2) };
}
This is useful for running the same tests with different configurations.
These annotations have been invaluable in organizing and managing my test cases, ensuring each test runs smoothly and effectively.