Difference between BeforeClass and BeforeTest methods in TestNG


A TestNG class can have various TestNG methods such as @BeforeTest, @AfterTest, @BeforeSuite, @BeforeClass, @BeforeMethod, @test, etc. As per execution order, @BeforeTest executes first and after that, @BeforeClass does. However, if there are multiple TestNG classes and multiple Tests inside each class, then the behaviour of these methods is noticeable.

@BeforeTest

This method will execute only once in the entire execution before calling the first @Test method. It doesn’t matter how many @Test tags are present or how many classes are there having @Test tags, or multiple classes have multiple test tags. Based on the testing.xml file, once the execution starts, @BeforeTest method executes only once before the execution of the first @Test method and @BeforeClass as well as @BeforeMethod if present.

@BeforeClass

This method will execute once per class, i.e., the number of executions of @BeforeClass is equal to the number of TestNG classes to execute. As per testing.xml, if 10 TestNG classes are supposed to execute, then @BeforeClass will execute each time before each class starts running. It doesn’t matter how many @Test tags are present inside each class.

As per best practices, it is always advisable to create a separate class for all the common @Before methods so that those can be executed as per requirement and repeated calling could be avoided.

Key points in these differences are:

  • @BeforeTest method executes only once before the first @Test method.

  • @BeforeClass executes before each class.

  • If there are separate @BeforeTest and @BeforeClass methods in different classes, then all the @BeforeTest methods will execute first but @BeforeClass methods will be executing as per the respective classes.

  • If all test classes extend common @BeforeTest and @BeforeClass methods present in a separate class, then @BeforeTest will execute only once, however the same @BeforeClass method will execute before each class.

Scenario 1

Suppose there are two TestNG classes and each class is having two @Test tags. Each class extends the common class having @BeforeTest and @BeforeClass method inside it. In this scenario, @BeforeTest will execute only once, however the same @BeforeClass method will execute twice, each time before each class.

Approach/Algorithm to solve this problem

  • Step 1 − Create a common TestNG class beforemethods and write the @BeforeTest and @BeforeClass methods.

  • Step 2 − Create two TestNG classes and each class extends the common class (beforemethods) - OrderofTestExecutionInTestNG and NewTestngClass

  • Step 3 − Write two different @Test methods in each of these two classes - OrderofTestExecutionInTestNG and NewTestngClass.

  • Step 4 − Now create the testNG.xml as given below to run the two TestNG classes other than the common class.

  • Step 5 − Finally, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.

Example

Use the following code for the common TestNG class, beforemethods

import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
public class beforemethods {
   @BeforeClass
   public void beforeClass() {
      System.out.println("in beforeClass");
   }
   @BeforeTest
   public void beforeTest() {
      System.out.println("in beforeTest");
   }
}

Use the following code for the TestNG class, OrderofTestExecutionInTestNG

import org.testng.annotations.Test;
public class OrderofTestExecutionInTestNG extends beforemethods{
   // test case 1
   @Test
   public void testCase1() {
      System.out.println("in test case 1 of OrderofTestExecutionInTestNG");
   }
   // test case 2
   @Test
   public void testCase2() {
      System.out.println("in test case 2 of OrderofTestExecutionInTestNG");
   }
}

Use the following code for the common TestNG class, NewTestngClass

import org.testng.annotations.Test;
public class NewTestngClass extends beforemethods{
   @Test
   public void testCase1() {
      System.out.println("in test case 1 of NewTestngClass");
   }
   @Test
   public void testCase2() {
      System.out.println("in test case 2 of NewTestngClass");
   }
}

testng.xml

This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.

<?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 = "OrderofTestExecutionInTestNG"/>
         <class name = "NewTestngClass"/>
      </classes>
   </test>
</suite>

Output

in beforeTest
in beforeClass
in test case 1 of OrderofTestExecutionInTestNG
in test case 2 of OrderofTestExecutionInTestNG
in beforeClass
in test case 1 of NewTestngClass
in test case 2 of NewTestngClassin afterSuite

Scenario 2

Suppose there are two TestNG classes and each class is having two @Test tags and separate @BeforeTest as well as @BeforeClass methods. In this scenario, all @BeforeTest methods will execute first but @BeforeClass methods will be executing as per their respective classes.

Approach/Algorithm to solve this problem

  • Setp 1 − Create two TestNG classes, OrderofTestExecutionInTestNG and NewTestngClass

  • Setp 2 − Write two different @Test methods in each of these two classes, OrderofTestExecutionInTestNG and NewTestngClass.

  • Setp 3 − Write different @BeforeTest methods in each of these two classes, OrderofTestExecutionInTestNG and NewTestngClass.

  • Setp 4 − Write different @BeforeClass methods in each of these two classes, OrderofTestExecutionInTestNG and NewTestngClass.

  • Setp 5 − Now create the testNG.xml as given below to run the two TestNG classes.

  • Setp 6 − Now, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.

Example

Use the following code for the TestNG class, OrderofTestExecutionInTestNG

import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
public class OrderofTestExecutionInTestNG {
   // test case 1
   @Test
   public void testCase1() {
      System.out.println("in test case 1 of OrderofTestExecutionInTestNG");
   }
   // test case 2
   @Test
   public void testCase2() {
      System.out.println("in test case 2 of OrderofTestExecutionInTestNG");
   }
   @BeforeClass
   public void beforeClass() {
      System.out.println("in beforeClass of OrderofTestExecutionInTestNG");
   }
   @BeforeTest
   public void beforeTest() {
      System.out.println("in beforeTest of OrderofTestExecutionInTestNG");
   }
}

Use the following code for the common TestNG class, NewTestngClass

import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
public class NewTestngClass {
   @Test
   public void testCase1() {
      System.out.println("in test case 1 of NewTestngClass");
   }
   @Test
   public void testCase2() {
      System.out.println("in test case 2 of NewTestngClass");
   }
   @BeforeTest
   public void beforeTest() {
      System.out.println("in beforTest of NewTestngClass");
   }
   @BeforeClass
   public void beforeClass() {
      System.out.println("in beforeClass of NewTestngClass");
   }
}

testng.xml

This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.

<?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 = "OrderofTestExecutionInTestNG"/>
         <class name = "NewTestngClass"/>
      </classes>
   </test>
</suite>

Output

in beforTest of NewTestngClass
in beforeTest of OrderofTestExecutionInTestNG
in beforeClass of OrderofTestExecutionInTestNG
in test case 1 of OrderofTestExecutionInTestNG
in test case 2 of OrderofTestExecutionInTestNG
in beforeClass of NewTestngClass
in test case 1 of NewTestngClass
in test case 2 of NewTestngClassin

Updated on: 12-Jan-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements