TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

TestNG @BeforeTest Annotation

TestNG @BeforeTest Annotation with Testng Tutorial, Introduction, Testng Installation, Features of Testng, Suite Test, Testng Exception Test, Group Test, Plug with Ant, Plug with Eclipse, etc.

<< Back to TESTNG

TestNG @BeforeTest Annotation

You have a requirement when you automate the test cases, you want your data to be deleted first which you submitted. For example, when you run the test case, you will fill the details in a form, and the data is saved in a database. When you run the test case again, then you get an error that "data already exists".

@BeforeTest: The method which comes under the @BeforeTest annotation will be executed first before any test belonging to that folder.

Let's understand through an example.

First case: When we place the @BeforeTest annotated method in the beginning.

Step 1: Open the Eclipse.

Step 2: We create two java projects, i.e., it_department.java and hr_department.java.

it_department.java

package com.TheDeveloperBlog;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class it_department 
{
	
  @BeforeTest                                             // annotated method placed in the beginning.
  public void before_test()
  {
	  System.out.println("It will be executed first");
  }
  @Test
  public void software_developers()
  {
	  System.out.println("Software Developers");
  }
  
  @Test
  public void software_testers()
  {
	  System.out.println("Software Testers");
  }
  
  @Test
  public void qa_analyst()
  {
	  System.out.println("QA Analyst");
  }}

In the above code, one method is placed under the @BeforeTest annotation which will be executed first before all the test methods available in the it_department.

hr_department.java

package com.TheDeveloperBlog;

import org.testng.annotations.Test;

public class hr_department 
{
	
	@Test
	public void manager()
	{
		System.out.println("Manager");
	}
	
	@Test
	
	public void hr()
	{
		System.out.println("HR");
	}
	
	@Test
	public void counsellor()
	{
		System.out.println("Counsellor");
	}

}

Step 3: Create the testng.xml file. testng.xml file








 




 
 

Step 4: Run the testng.xml file. Right click on the testng.xml and then move the cursor down to Run As and then click on the 1 TestNG Suite.

Output

TestNG @BeforeTest Annotation

The above output shows that the method in @BeforeTest annotation is executed first before all the test cases of it_department.

Second case: When we place the @BeforeTest annotated method at the end.

Source code

package com.TheDeveloperBlog;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class it_department 
{
	
  @Test
  public void software_developers()
  {
	  System.out.println("Software Developers");
  }
  
  @Test
  public void software_testers()
  {
	  System.out.println("Software Testers");
  }
  
  @Test
  public void qa_analyst()
  {
	  System.out.println("QA Analyst");
	  
  }
  @BeforeTest
  public void before_test()                               // annotated method placed at the end.
  {
	  System.out.println("It will be executed first");
  }
}

In the above code, we place the @BeforeTest annotated method at the end.

Output

TestNG @BeforeTest Annotation

In the above output, we conclude that @BeforeTest annotated method is executed first, So, we conclude that @BeforeTest annotated method is placed anywhere, it will be executed first.


Next TopicTestNG Annotations




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf