What is the difference between BeforeClass and BeforeTest in TestNG - Selenium WebDriver, please suggest me best link or site for this.


This is a duplicate question of below :


since you asked site to refer please consider below websites:

You can refer below link:



@BeforeTest method runs before the test method, belonging to the classes inside the tag. It is called once for each test tag. It is used to initialize common objects between different test classes inside one test tag.

@BeforeClass method runs before the class is invoked. It is called once for each test class inside the test tag. It is used to initialize common objects between different test methods inside one test class.

For example:

   <suite name="suit1">
      <test name="TestTag1">
          <class name="TestClass1"/>
      <test name="TestTag2">
          <class name="TestClass2"/>
          <class name="TestClass3"/>

Here, @BeforeTest will be called twice, one time before TestTag1 the second time before TestTag2 while @BeforeClass will be called three times, one time before TestClass1 the second time before TestClass2 and the third time before TestClass3

Site for Reference

  • thank you for valuable answer.
    – sushen
    Mar 9 '20 at 4:07
  • @sushen Do accept the answer which is most applicable to you, by clicking the tick sign near to the answer.
    – JAINAM
    Mar 9 '20 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.