Difference between @BeforeMethod , @BeforeClass and @BeforeTest

Answered

What is the diffrance between @BeforeMethod , @BeforeClass and @BeforeTest annotation.

Its bit confusing as all seems same(basically all run before @Test method).

Please elaborate.

Train Asked on August 20, 2014 in Selenium WebDriver.
Add Comment
  • 4 Answer(s)
    Best answer

    Hi Rishab,

    Ankit is right above with the basic understanding of TestNG Annotation.

    Yes you are right that it all looks same and you might not get a chance to use all of it together. But when you step in to some big framework then you might need to use many of these. Think of scenarios when you need to Initiate your driver before your test, instantiate your base class before your test class, send some messages before starting your test suite or can be so many things depends on your framework requirement.

    Still if you like to test and practice this, please follow this:

    http://www.toolsqa.com/selenium-webdriver/testng-annotations-groups-depends/

    Best Regards,
    Lakshay Sharma

    Professor Answered on August 20, 2014.
    Add Comment

    hi rishab

    @BeforeMethod: The annotated method will be run before each test method.
    @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.

    Vice Professor Answered on August 20, 2014.
    Add Comment

    Hi Rishab,

    When you work with TestNG Framework you use annotations .  The hierarchy will go like this

    Before Suite
    Beforeclass
    Beforemethod
    BeforeTest
    Test
    Aftertest
    Aftermethod
    Afterclass
    aftersuite

    This will give a idea of testNg framework
    Thanks
    Anu

    Doctor Answered on August 20, 2014.
    Add Comment

    annotation in testng

    if i am using the annotation as beforclass and afterclass
    then for all the test  there is only one pre and post condition  within the class

    and if i m using the annotation as beforemethod then for all the test with in the class there is one pre and post contiotion

    suppose

    @beforeclass

    public void login()
    {

    System.out.println(“this is login)
    }

    @AfterClass

    public void logout ()
    {

    System.out.println(“this is logout)
    }

    @test(priority=1)
    public void Mobilerechrage()
    {

    System.out.println(my mobile recharge method)

    }

    @test(priority=2)
    public void DISHTVrecharge()
    {

    System.out.println(“my DISHTVrecharge method”)
    }

    then the output is for beforeclass and afterclass  annotation is

    this is login
    my mobile recharge
    my DISHTVrecharge method
    this is logout

    if i am using beformethod  and aftermethod  then output is

    this is login
    my mobile recharge
    this is logout

    this is login
    my DISHTVrecharge method
    this is logout

    Default Answered on July 11, 2015.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.