What is the priority of 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 the execution order, @BeforeTest executes first and after that @BeforeClass does. However, if there are multiple TestNG classes and multiple Tests inside each class, the behaviour of these methods is noticeable.

@BeforeTest

This method will execute only once in the entire execution before calling the 1st @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 testNG.xml file, once the execution starts, the @BeforeTest method executes only once before the execution of the 1st @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 = Number of TestNG class to execute. As per testNG.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 those can be executed as per requirement and repeated calling could be avoidable.

Key points in these two methods are −

  • @BeforeTest method executes only once before the 1st @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 1st, but @BeforeClass methods will be executing as per their respective classes.

  • If all the test classes extend the 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

When there are two TestNG classes and each class has 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 (OrderofTestExecutionInTestNG and NewTestngClass) and let each class extend the common class (beforemethods).

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

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

  • Step 5 − Run the testNG.xml or run the testNG class directly 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

When 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 the @BeforeTest methods will execute first, but the @BeforeClass methods will be executing as per their respective classes.

Approach/Algorithm to solve this problem −

  • Step 1 − Create two TestNG classes: OrderofTestExecutionInTestNG and NewTestngClass

  • Step 2 − Write two different @Test methods in each of these two classes.

  • Step 3 − Write different @BeforeTest methods in each of these two classes.

  • Step 4 − Write different @BeforeClass methods in each of these two classes.

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

  • Step 6 − Run the testNG.xml or run the testNG class directly 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: 09-Mar-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements