- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between BeforeTest and BeforeMethod 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 @BeforeMethod does.
However, if there are multiple TestNG Tests inside a class, the behaviour of these methods is noticeable as @BeforeTest runs only once before the 1st @Test method runs but @BeforeMethod runs each time prior to the run of each @Test.
@BeforeTest: This method will execute only once in entire execution before calling of 1st @Test method. It doesn’t matter how many @Test tag is present or how many classes are there having @Test tags, or multiple classes have multiple test tags. Based on testing.xml file, once the execution starts, @BeforeTest method executes only once before the execution of 1st @Test method and @BeforeClass as well as @BeforeMethod if present.
@BeforeMethod: This method will execute one time per @Test i.e., the number of executions of @BeforeMethod = No. of @Test method to execute. As per testing.xml if 10 @Test is supposed to execute then @BeforeMethod will execute each time before each @Test starts running.
Key points in these differences are
@BeforeTest method executes only once before 1st @Test method.
@BeforeMethod executes before each @Test method.
If there are separate @BeforeTest and @BeforeMethod methods in different classes, then all @BeforeTest methods will execute 1st but @BeforeMethod methods will be executing as per respective class and their @Test methods.
If all test classes extend common @BeforeTest and @BeforeMethod methods present in a separate class, @BeforeTest will execute only once however same @BeforeMethod method will execute before each @Test.
Scenario 1
When there are 2 TestNG classes and each class is having 2 @Test tags. Each class extends common class having @BeforeTest and @BeforeMethod inside it. In this scenario, @BeforeTest will execute only once however same @BeforeMethod will execute 2*2 times, each time before each @Test.
Approach/Algorithm to solve this problem
Step 1 − Create a common TestNG class − beforemethods and write @BeforeTest and @BeforeMethod methods.
Step 2 − Create 2 TestNG classes and each class extends common class (beforemethods) − OrderofTestExecutionInTestNG and NewTestngClass
Step 3 − Write 2 different @Test method in each of these 2 classes − OrderofTestExecutionInTestNG and NewTestngClass.
Step 4 − Now create the testNG.xml as given below to run 2 TestNG classes other than common class.
Step 5 − Now, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.
Example
The following code for common TestNG class − beforemethods.
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeTest; public class beforemethods { @BeforeMethod public void beforeMethod() { System.out.println("in beforeMethod"); } @BeforeTest public void beforeTest() { System.out.println("in beforeTest"); } }
The following code for 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"); } }
The following code for 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 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 beforeMethod in test case 1 of OrderofTestExecutionInTestNG in beforeMethod in test case 2 of OrderofTestExecutionInTestNG in beforeMethod in test case 1 of NewTestngClass in beforeMethod in test case 2 of NewTestngClass =============================================== Suite1 Total tests run: 4, Passes: 4, Failures: 0, Skips: 0 ===============================================
Scenario 2
When there are 2 TestNG classes and each class is having 2 @Test tags and separate @BeforeTest as well as @BeforeMethod. In this scenario, all @BeforeTest methods will execute 1st but @BeforeMethod will be executing as per respective classes and @Test.
Approach/Algorithm to solve this problem
Step 1 − Create 2 TestNG classes − OrderofTestExecutionInTestNG and NewTestngClass
Step 2 − Write 2 different @Test method in each of these 2 classes − OrderofTestExecutionInTestNG and NewTestngClass.
Step 3 − Write different @BeforeTest method in each of these 2 classes − OrderofTestExecutionInTestNG and NewTestngClass.
Step 4 − Write different @BeforeMethod in each of these 2 classes − OrderofTestExecutionInTestNG and NewTestngClass.
Step 5 − Now create the testNG.xml as given below to run 2 TestNG classes
Step 6 − Now, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.
Example
The following code for TestNG class − OrderofTestExecutionInTestNG.
import org.testng.annotations.*; 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"); } @BeforeMethod public void beforeMethod() { System.out.println("in beforeMethod of OrderofTestExecutionInTestNG"); } @BeforeTest public void beforeTest() { System.out.println("in beforeTest of OrderofTestExecutionInTestNG"); } }
The following code for common TestNG class − NewTestngClass.
import org.testng.annotations.*; 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 beforeTest of NewTestngClass"); } @BeforeMethod public void beforeMethod() { System.out.println("in beforeMethod 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 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 of NewTestngClass in beforeTest of OrderofTestExecutionInTestNG in beforeMethod of OrderofTestExecutionInTestNG in test case 1 of OrderofTestExecutionInTestNG in beforeMethod of OrderofTestExecutionInTestNG in test case 2 of OrderofTestExecutionInTestNG in beforeMethod of NewTestngClass in test case 1 of NewTestngClass in beforeMethod of NewTestngClass in test case 2 of NewTestngClass =============================================== Suite1 Total tests run: 4, Passes: 4, Failures: 0, Skips: 0 ===============================================