TestNG Annotations Exercies
TestNG Annotations Exercies
@Test
public void testMethod() {
System.out.println("This is a simple test method.");
}
}
📝 Output:
This is a simple test method.
import org.testng.annotations.*;
@BeforeMethod
public void setUp() {
System.out.println("Setting up before each test.");
}
@Test
public void testOne() {
System.out.println("Executing Test One.");
}
@Test
public void testTwo() {
System.out.println("Executing Test Two.");
}
@AfterMethod
public void tearDown() {
System.out.println("Cleaning up after each test.\n");
}
}
📝 Output:
Setting up before each test.
Executing Test One.
Cleaning up after each test.
import org.testng.annotations.*;
@BeforeClass
public void beforeClass() {
System.out.println("Runs once before all tests in this class.");
}
@Test
public void testA() {
System.out.println("Running Test A.");
}
@Test
public void testB() {
System.out.println("Running Test B.");
}
@AfterClass
public void afterClass() {
System.out.println("Runs once after all tests in this class.");
}
}
📝 Output:
Runs once before all tests in this class.
Running Test A.
Running Test B.
Runs once after all tests in this class.
@BeforeTest
public void beforeTest() {
System.out.println("Before <test> tag execution.");
}
@Test
public void sampleTest() {
System.out.println("Executing sample test.");
}
@AfterTest
public void afterTest() {
System.out.println("After <test> tag execution.");
}
}
import org.testng.annotations.*;
@BeforeSuite
public void beforeSuite() {
System.out.println("Before the entire suite.");
}
@Test
public void testCase() {
System.out.println("Executing test case in the suite.");
}
@AfterSuite
public void afterSuite() {
System.out.println("After the entire suite.");
}
}
📝 Output:
Before the entire suite.
Executing test case in the suite.
After the entire suite.
import org.testng.annotations.Test;
@Test(priority = 2)
public void secondTest() {
System.out.println("Second test executed.");
}
@Test(priority = 1)
public void firstTest() {
System.out.println("First test executed.");
}
@Test(priority = 3)
public void thirdTest() {
System.out.println("Third test executed.");
}
}
📝 Output:
First test executed.
Second test executed.
Third test executed.
@Test
public void startApp() {
System.out.println("Application started.");
}
@Test(dependsOnMethods = {"startApp"})
public void login() {
System.out.println("Login successful.");
}
@Test(dependsOnMethods = {"login"})
public void performAction() {
System.out.println("Performed actions after login.");
}
}
📝 Output:
Application started.
Login successful.
Performed actions after login.