TestNG Annotations with examples.

TestNG annotations with examples.

TestNG is an automation testing framework which use to handle the automation scripts.

Below are the list of TestNG annotations with examples

@BeforeSuite
The method below this annotation will run before the execution of all the test methods in the suite.

@AfterSuite
The method below annotation will run after the execution of all the test methods in the suite.

@BeforeTest
The method below this annotation will execute before the execution of all the test methods of available classes belonging to that folder.

@AfterTest
The method below this annotation will execute after the execution of all the test methods of available classes belonging to that folder.

@BeforeClass
The method below this annotation will execute before the first method of the current class is invoked.

@AfterClass
This method below this annotation will invoke after the execution of all the test methods of the current class.

@BeforeMethod
The method below this annotation will execute before each test method will run.

@AfterMethod
The method below this annotation will run after the execution of each test method.

Java Code-

public class Testing_testNGAnnotation {

@BeforeSuite
public void beforeSuite_testNG(){
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
	}
@BeforeClass
public void beforeClass_testNG(){
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
	}
@BeforeMethod
public void beforeMethod_testNG(){
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
	}
@BeforeTest 
public void beforeTest_testNG(){
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
	}
@Test
public void testNG1(){
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
	}
@Test
public void testNG2(){
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);	
	}	
@AfterTest 
public void afterTest_testNG(){
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
	}
@AfterMethod
public void afterMethod_testNG(){
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
	}
@AfterClass 
public void afterClass_testNG(){
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
	}
@AfterSuite
public void afterSuite_testNG(){
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
	}
	
}

XML Code-

  
<?xml version="1.0" encoding="UTF-8"?>
<suite name="group">
<test name= "name">
<classes>
<class name="org.Testngpro.Testing_testNGAnnotation"></class>
</classes>
</test>
</suite>

Output-

 
[RemoteTestNG] detected TestNG version 7.4.0
Method name is :beforeSuite_testNG
Method name is :beforeTest_testNG
Method name is :beforeClass_testNG
Method name is :beforeMethod_testNG
Method name is :testNG1
Method name is :afterMethod_testNG
Method name is :beforeMethod_testNG
Method name is :testNG2
Method name is :afterMethod_testNG
Method name is :afterClass_testNG
Method name is :afterTest_testNG
Method name is :afterSuite_testNG

===============================================
group
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================


@BeforeGroups
This annotation method run only once for a group before the execution of all test cases belonging to that group.

@AfterGroups
This annotation method run only once for a group after the execution of all test cases belonging to that group.

Java Code-

public class Testing_beforeAfterGroup {
	
	@BeforeGroups("testNG")
	public void beforeGroup_testNG()
	{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
	}
@Test(groups= {"automation"})
public void testNG1()
	{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
	}
 @Test(groups= {"selenium"})
public void testNG2()
	{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
	}
@Test(groups= {"testNG"})
public void testNG3()
	{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
	}
@AfterGroups("selenium")
public void afterGroup_testNG()
	{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
	}

}

XML Code-

    
<?xml version="1.0" encoding="UTF-8"?>
<suite name="group">
<test name= "name">
<groups>
<run>
<include name= "testNG"></include>
<include name= "selenium"></include>
</run>
</groups>
<classes>
<!--  <class name="org.Testngpro.Testing_Grouping"></class>-->
<class name="org.Testngpro.Testing_beforeAfterGroup"></class>
</classes>
</test>
</suite>
    

Output-

[RemoteTestNG] detected TestNG version 7.4.0
[TestNGContentHandler] [WARN] It is strongly recommended to add "" at the top of the suite file [F:\eclipse\Practice_2022\TestNGMethod\Group.xml] otherwise TestNG may fail or not work as expected.
Method name is :testNG2
Method name is :afterGroup_testNG
Method name is :beforeGroup_testNG
Method name is :testNG3

===============================================
group
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0

In above example- Beforegroup executed before it’s group method and aftergroup get executed after it’s group method get executed.


Related topic :-
















@Factory Annotation
This annotation is used to invoke methods from multiple classes. In below example, we have invoked 2 different classes methods by calling those classes.

Java Code-

public class Testing_Factory {
	@Factory
	public Object[] getTestClasses()  
	{  
	Object tests[]=new Object[2];  
	tests[0]=new Testing_Ignore();  
	tests[1]=new Testing_Enabled();  
	return tests;  
	}  
}

Outcome-


[RemoteTestNG] detected TestNG version 7.4.0
Method name is :testNG1
Method name is :testNG1
PASSED: testNG1
PASSED: testNG1

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================


@Ignore Annotation
This annotation is used to ignore the method.

Java Code-

public class Testing_Ignore {
    @Test
	public void testNG1()
	{
		String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
		System.out.println("Method name is :"+methodName);
	}
    @Ignore
	@Test
	public void testNG2()
	{
		String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
		System.out.println("Method name is :"+methodName);
	}

}
    

Outcome-

[RemoteTestNG] detected TestNG version 7.4.0
Method name is :testNG1
PASSED: testNG1

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================