Hi Mithun, @BeforeTest method runs before any test method, belonging to the classes inside the tag, is run. While @BeforeClass method runs before the first test method in the current class is invoked. For eg: this TestNGAnnotations.java file will test annotations:
public class TestNGAnnotations {
// test case 1
@Test
public void testCase1() {
System.out.println("Test case 1");
}
// test case 2
@Test
public void testCase2() {
System.out.println("Test case 2");
}
@BeforeClass
public void beforeClass() {
System.out.println("BeforeClass");
}
@BeforeTest
public void beforeTest() {
System.out.println("BeforeTest");
}
}
Now create the file testng.xml to execute testng annotations:
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1">
<test name = "Test1">
<classes>
<class name = "TestNGAnnotations"/>
</classes>
</test>
</suite>
Now, run the testng.xml, which will run the test case and produce following output:
BeforeTest
BeforeClass
Test case 1
Test case 2
===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================