Tuesday, December 18, 2012

Difference between @BeforeTest and @BeforeMethod annotations in TestNG



@BeforeTest
@BeforeMethod
To execute a set-up method before any of the test methods included in the < test > tag in the testng.xml file.
To execute a set-up method before any of the test methods annotated as @Test.

Example Testng.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">

 
  <test name="test">
      <classes>
          <class name=".BeforetestandBeforemethod">
              <methods>
                  <include name="toBeforeTest"></include>
                  <include name="toBeforeMethod"></include>
              </methods>
          </class>
      </classes>
  </test>

 <methods>
</suite> <!-- Suite -->

Example Java code such as:
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class BeforetestandBeforemethod {

        @BeforeTest
         public void beforeTest() {
           System.out.println("@BeforeTest");
         }
        @BeforeMethod
        public void beforemethod(){
               System.out.print("@BeforeMethod");
        }
        
        public void helper() {
                  System.out.println("Flow");
                }

                @Test
                public void toBeforeMethod() {
                  helper();
                  System.out.println("BeforeMethod");
                }

                @Test
                public void toBeforeTest() {
                  helper();
                  System.out.println("BeforeTest");
                }
      
}

Execute code by TestNG, then the Output would be:

When run  <include name="toBeforeTest"></include>


Run  <include name="toBeforeMethod"></include>


17 comments:

  1. Nice. TestNg could have used better names here.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi,
    For Clarification
    @BeforeTest Will be called before any Test only..
    @BeforeMethod will be called before every method

    Note that, not every method in a file is Test, but every Test in a file is a method.

    In the above case the helper() method is only a method not a Test. Whereas toBeforeTest() and toBeforeMethod() are Test, since they are preceded with "@Test" annotation.

    @BeforeTest will be executed only for the blocks of toBeforeTest() and toBeforeMethod().
    Whereas @BeforeMethod will be executed for all the methods of toBeforeTest(), toBeforeMethod() as well as the helper() method

    ReplyDelete
  4. Abhishek is absolutely correct, according to the output shown above.

    1) @BeforeTest is executed twice since there are 2 @Test.
    2) @BeforeMethod is executed 5 times, since there are totally 5 methods.

    ReplyDelete
  5. Abhishek is explaining perfectly. You guys can try using the below example as well for better understanding

    package testNgTests;
    import org.testng.annotations.*;

    public class TestngAnnotations {

    @BeforeClass
    public void connectDb(){
    System.out.println("Db connection is successfull");
    }

    @BeforeMethod
    public void openBrowser(){
    System.out.println("Browser opened");
    }

    @AfterMethod
    public void closeBrowser(){
    System.out.println("Browser Closed");
    }

    @Test
    public void testChrome(){
    System.out.println("Chrome Test is Successful");
    }

    @Test
    public void testMozilla(){
    System.out.println("Firefox Test is Successful");
    }

    public void noAnno(){
    System.out.println("No Annotation method**************");
    }

    @Test
    public void testIE(){
    System.out.println("IE Test is Successful");
    }
    @AfterClass
    public void closeDb(){
    System.out.println("Db Connection is closed");
    }

    @AfterTest
    public void aft(){
    System.out.println("After Test is executed");
    }
    @BeforeTest
    public void bft(){
    System.out.println("Before Test is executed");
    }

    }

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete


  9. @BeforeTest is used to do setup related to bunch of tests written inside the tag as the author has mentioned.

    Consider below testng.xml

    Please excuse the XML format, had to remove the delimiters as XML start and end tag made the whole code disappear in my earlier responses.

    suite name="TestAccess"

    @BeforeTest
    test name="Test Login, refresh and Logout"
    classes
    class name="com.yogi.testng.example.Login"
    class name="com.yogi.testng.example.Refresh"
    class name="com.yogi.testng.example.Logout"
    classes
    test
    @AfterTest

    @BeforeTest
    test name="Test Login and Logout"
    classes
    class name="com.yogi.testng.example.Login"
    class name="com.yogi.testng.example.Logout"
    classes
    test
    @AfterTest

    suite
    @AfterSuite




    @BeforeSuite will run only once before TestAccess. We are clear on this.

    @BeforeTest will run twice, once before each set of test.

    @BeforeMethod will run before each @Test method in test classes.

    @BeforeClass will run before "each" test class mentioned in the testng.xml.

    Typical use of testng would be to setup the driver at landing page, then not having to go back to do login again in the series of tests mentioned inside the Tag.

    ReplyDelete
  10. great differentiation of @BeforeTest and @BeforeMethod examples are very helpful to understand referred below link also
    Selenium Training
    Selenium Training in Chennai

    ReplyDelete
  11. what is helper() function doing under toBeforeMethod() and toBeforeTest() test/methods,why its not getting printed

    ReplyDelete