C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
TestNG @BeforeTest AnnotationYou 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 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 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
|