What is the difference between @BeforeClass and @BeforeTest?

0 votes
What is the difference between @BeforeClass and @BeforeTest in TestNG?
Jun 25 in Selenium by Mithun
26 views

1 answer to this question.

0 votes

Hi Mithun, @BeforeTest method runs before any test method, belonging to the classes inside the tag, is run. While @BeforeClass method runs before the first test method in the current class is invoked. For eg: this TestNGAnnotations.java file will test annotations:

public class TestNGAnnotations {
   // test case 1
   @Test
   public void testCase1() {
      System.out.println("Test case 1");
   }

   // test case 2
   @Test
   public void testCase2() {
      System.out.println("Test case 2");
   }

   @BeforeClass
   public void beforeClass() {
      System.out.println("BeforeClass");
   }

   @BeforeTest
   public void beforeTest() {
      System.out.println("BeforeTest");
   }

}

Now create the file testng.xml to execute testng annotations:

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1">
   <test name = "Test1">
      <classes>
         <class name = "TestNGAnnotations"/>
      </classes>
   </test>
</suite>

​Now, run the testng.xml, which will run the test case and produce following output:

BeforeTest
BeforeClass
Test case 1
Test case 2

===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
answered Jun 25 by Pragya

Related Questions In Selenium

0 votes
1 answer

What is the difference between thoughtworks.selenium and openqa.selenium selenium?

thoughtworks.selenium is the original Selenium (aka Selenium 1, ...READ MORE

answered Apr 13, 2018 in Selenium by king_kenny
• 3,590 points
130 views
0 votes
1 answer

What is difference the between Selenium and Celerity?

If you are talking about speed, use Celerity. If ...READ MORE

answered Apr 20, 2018 in Selenium by king_kenny
• 3,590 points
54 views
0 votes
1 answer

What is the difference between dot and text() in XPath?

Even though there is some difference between ...READ MORE

answered Apr 25, 2018 in Selenium by king_kenny
• 3,590 points
951 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,050 points
259 views
0 votes
1 answer
0 votes
1 answer

What is the role of TestNG & JUnit frameworks in Selenium?

Well TestNG and JUnit are test frameworks ...READ MORE

answered Apr 18, 2018 in Selenium by king_kenny
• 3,590 points
192 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
628 views
0 votes
1 answer

What is the difference between findElement and findElements in Selenium Webdriver?

Hi Nilaabh, Selenium Webdriver uses findElement and ...READ MORE

answered May 8 in Selenium by Anvi
• 10,310 points
119 views
0 votes
1 answer

What is PhantomJS and what is the usage of it?

Hi Ojaswini, PhantomJS is a headless browser with ...READ MORE

answered May 17 in Selenium by Anvi
• 10,310 points
20 views