As we know from official TestNG documentation:
@BeforeClass:
The annotated method will be run before the first test method in the current class is invoked.
@BeforeTest:
The annotated method will be run before any test method belonging to the classes inside the <test>
tag is run.
Both the above TestNG annotations look similar in functionality. Can anyone explain the unique difference?
@BeforeTest method executes only once before the first @Test method. @BeforeClass executes before each class. If there are separate @BeforeTest and @BeforeClass methods in different classes, then all the @BeforeTest methods will execute first but @BeforeClass methods will be executing as per the respective classes.
@BeforeTest: The method which comes under the @BeforeTest annotation will be executed first before any test belonging to that folder. ADVERTISEMENT.
The @BeforeClass annotated method will be executed before the first method of the current class is invoked. The @AfterClass annotated method will be invoked after the execution of all the test methods of the current class. The @BeforeMethod annotated method will be executed before each test method will run.
SeleniumAbstractTest.class
public abstract class SeleniumAbstractTest { @BeforeSuite public void beforeSuite() { System.out.println("BeforeSuite"); } @BeforeTest public void beforeTest() { System.out.println("BeforeTest"); } @BeforeClass public void beforeClass() { System.out.println("BeforeClass"); } @BeforeMethod public void beforeMethod() { System.out.println("BeforeMethod"); } @AfterMethod public void afterMethod() { System.out.println("AfterMethod"); } @AfterClass public void afterClass() { System.out.println("AfterClass"); } @AfterTest public void afterTest() { System.out.println("AfterTest"); } @AfterSuite public void afterSuite() { System.out.println("AfterSuite"); } }
MyTestClass1.class
public class MyTestClass1 extends SeleniumAbstractTest { @Test public void myTestMethod1() { System.out.println("myTestMethod1"); } @Test public void myTestMethod2() { System.out.println("myTestMethod2"); } }
MyTestClass2.class
public class MyTestClass2 extends SeleniumAbstractTest { @Test public void myTestMethod3() { System.out.println("myTestMethod3"); } @Test public void myTestMethod4() { System.out.println("myTestMethod4"); } }
If you have the following Test Suite...
<suite name="Suite"> <test name="Test1" > <classes> <class name="MyTestClass2" /> </classes> </test> <test name="Test2"> <classes> <class name="MyTestClass1"/> <class name="MyTestClass2"/> </classes> </test> </suite>
... then the output [indented for easy reading] will be
BeforeSuite ' BeforeTest ' ' BeforeClass ' ' ' BeforeMethod ' ' ' ' myTestMethod3 ' ' ' AfterMethod ' ' ' BeforeMethod ' ' ' ' myTestMethod4 ' ' ' AfterMethod ' ' AfterClass ' AfterTest ' BeforeTest ' ' BeforeClass ' ' ' BeforeMethod ' ' ' ' myTestMethod1 ' ' ' AfterMethod ' ' ' BeforeMethod ' ' ' ' myTestMethod2 ' ' ' AfterMethod ' ' AfterClass ' ' BeforeClass ' ' ' BeforeMethod ' ' ' ' myTestMethod3 ' ' ' AfterMethod ' ' ' BeforeMethod ' ' ' ' myTestMethod4 ' ' ' AfterMethod ' ' AfterClass ' AfterTest AfterSuite
Hope it helps :)
@BeforeMethod - executes before every test method e.g. The Method which uses @Test annotation
@BeforeTest - executes only before tag given in testng.xml file.
In a nutshell, @BeforeMethod works on test defined in Java classes. And @BeforeTest works on test defined in testng.xml i.e XML files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With