What is the difference between BeforeClass and BeforeTest

0 votes
What is the difference between @BeforeClass and @BeforeTest in TestNG?
Jun 25, 2019 in Selenium by Mithun
5,603 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, 2019 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,710 points
2,984 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,710 points
1,018 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,710 points
8,334 views
0 votes
2 answers
0 votes
2 answers

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,500 points
13,674 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,143 views
0 votes
2 answers

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

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

answered Sep 4, 2020 in Selenium by Sri
• 3,190 points
2,891 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,910 points
7,221 views
+1 vote
2 answers

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

FindElement Command -This method locates for the ...READ MORE

answered Feb 11, 2020 in Selenium by anonymous
8,368 views
0 votes
2 answers

What is PhantomJS and what is the usage of it?

@Ojaswini, Phantomjs is used for automating webpage ...READ MORE

answered Aug 1, 2019 in Selenium by Abha
• 28,140 points
2,151 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP