TestNg Annotations – Selenium online Training

TestNg annotations are the most crucial part to understand for the TestNG beginners . One must understand these annotations to write the scripts in TestNg and to build frameworks.

TestNg Annotations

Below are the TestNg Annotations

@BeforeSuite: This method will be invoked before the suite runs.

@AfterSuite: This method will be invoked after all the test cases in the suite runs.

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.

@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.

@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.

@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.

@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.

@AfterClass: The annotated method will be run after all the test methods in the current class have been run.

@BeforeMethod: The annotated method will be run before each test method.

@AfterMethod: The annotated method will be run after each test method.


Lets have a TestNg program to understand all the annotations

Java Program showing TestNg Annotations

package com.TestNg;
 
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
 
public class Annotations {
 
@Test
public void TestCase1() {
System.out.println("---Test case 1 executed---");
System.out.println("---Test case 1 ended---");
}
 
@Test
public void TestCase2() {
System.out.println("---Test case 2 executed---");
System.out.println("---Test case 2 ended---");
}
 
@Test
public void TestCase3() {
System.out.println("---Test case 3 executed---");
System.out.println("---Test case 3 ended---");
}
 
 
@BeforeMethod
public void Before_Method() {
System.out.println("-- Before Method Annotation Called----");
}
 
@AfterMethod
public void amethod() {
System.out.println("-- After Method Annotation End----");
}
 
@BeforeClass
public void bclass() {
System.out.println("-- Before Class Annotation Called----");
}
 
@AfterClass
public void aclass() {
System.out.println("-- After Class Annotation Called----");
}
 
@BeforeTest
public void Before_Test() {
System.out.println("---Before Test Annotation Called---");
}
 
@AfterTest
public void After_Test() {
System.out.println("---After Test Annotation Called---");
}
 
@BeforeSuite
public void Before_Suite() {
System.out.println("---Before Suite Annotation Called---");
}
 
@AfterSuite
public void After_Suite() {
System.out.println("---After Suite Annotation Called ---");
}
 
}
 

TestNg.xml – XMl file to execute the above test cases

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test_run">
<classes>
<class name="com.TestNg.Annotations"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
 

Output – 

---Before Suite Annotation Called---
---Before Test Annotation Called---
-- Before Class Annotation Called----
-- Before Method Annotation Called----
---Test case 1 executed---
---Test case 1 ended---
-- After Method Annotation End----
-- Before Method Annotation Called----
---Test case 2 executed---
---Test case 2 ended---
-- After Method Annotation End----
-- Before Method Annotation Called----
---Test case 3 executed---
---Test case 3 ended---
-- After Method Annotation End----
-- After Class Annotation Called----
---After Test Annotation Called---
---After Suite Annotation Called ---
 
===============================================
Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

Note :

  • Before test annotation method will execute before   <test name=”Test_run”> this .
  • Test Case in TestNg with @Test annotations are called methods.
  • Without @Test annotations test case will not be considered as method of TestNg it will be considered as a normal function.
  • @Test annotations in must for a method to run as TestNg Method ( Common interview question)

Here we have covered all the TestNg annotations and its description from the perspective of selenium interview questions. For any doubt or queary you can simple put your question in comment or mail us. We also provide live selenium online training for the aspirants.

Enroll for the Selenium online Training

Leave a Comment

Your email address will not be published. Required fields are marked *