@BeforeTest @BeforeClass @BeforeMethod @Test Annotations in TestNG - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Friday, 26 June 2015

@BeforeTest @BeforeClass @BeforeMethod @Test Annotations in TestNG

package testSuite;


import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class letsdothis {

       @BeforeClass
       public void beforeclass()
       {
              System.out.println("I am in before class");
       }
       @Test
       public void test()
       {
              System.out.println("I am in first Test function");
       }

       @Test
       public void testing(){
              System.out.println("I am in second Test function");
       }


       @BeforeMethod
       public void beforemethod()
       {
              System.out.println("Before method");
       }

       @BeforeTest
       public void beforetest(){
              System.out.println("Before Test");

       }

}

Output
Before Test
I am in before class
Before method
I am in first Test function
Before method
I am in second Test function
PASSED: test
PASSED: testing

Note: All this annotations are used when we want to test a function 
i.e All the functions having @Test because TestNG framework has been designed for testing.

So here Before Test will be always called first because TestNG wants to be sure  before going to @Test.

So BeforeTest will always be executed on priority

Then BeforeClass ----> @BeforeClass: The annotated method will be run before the first test method in the current class is invoked. 


Then BeforeMethod note this will be executed for every Test function---->@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.

Just after @BeforeMethod, @Test will be called

SO from here we conclude @BeforeMethod will be called everytime before @Test 

Explanation: @BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run. 

No comments:

Post a Comment