How to use "onlyForGroups","dependsOnGroups","dependsOnMethods" and "Group" Attribute in @Test Annotation?

How to use "onlyForGroups", "dependsOnGroups", "dependsOnMethods" and "Group" Attribute in @Test Annotation?

onlyForGroups Attribute-

"OnlyForGroups" option is only for BeforeMethods and AfterMethods. “BeforeMethods or AfterMethod get executed before/after each test method.

But when we use “OnlyForGroup” then it will not execute for all the methods.

In this case it will will invoke only for those test which group name is same as OnlyForGroups value in before or After method annotation.

Java Code-

public class Testing_OnlyforGroup {
@BeforeMethod(onlyForGroups= {"selenium"})
public void beforeTestNG1()
{
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);
}
@Test(groups= {"selenium"})
public void testNG3()
{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
}
@Test(groups= {"selenium"})
public void testNG4()
{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
}
@Test
public void testNG5()
{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
}
@AfterMethod
public void afterTestNG6()
{
 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 :testNG2
Method name is :afterTestNG6
Method name is :beforeTestNG1
Method name is :testNG3
Method name is :afterTestNG6
Method name is :beforeTestNG1
Method name is :testNG4
Method name is :afterTestNG6
Method name is :testNG5
Method name is :afterTestNG6
PASSED: testNG2
PASSED: testNG4
PASSED: testNG5
PASSED: testNG3

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


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


dependsOnGroups Attribute-

As it indicate, this method will depend on group name. So once specified group get executed then only this method will get executed.

Here in below example, testNG1 method is depend on group mentioned as TestNG which is testNG3 method. So testNG1will get executed after testNG3 method

Java Code-

@Test(dependsOnGroups={"TestNG"})
public void testNG1()
{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);	
}
@Test(groups={"BDD"})
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);		
}
}

Outcome-

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

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


Related topics-
















dependsOnMethods Attribute-

This method will execute only when dependent methods get executed.

Such as in below example- testNG1 method is dependent on testNG3 method. So testNG1 method will execute only once testNG3 get executed.

Java Code-

public class Testing_dependonMethods {
@Test(dependsOnMethods="testNG3")
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);	
}
@Test()
public void testNG3()
{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);	
}
}

Output

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

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


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


Related topics-















Group Attribute-

In Group attribute, we can pass the group name which we want to execute. This group can be handled from xml file.

In below example, we have mentioned the group name in java class. And same can be handled from xml file while executing the script.

Java Code-

public class Testing_Grouping {
@Test(groups={"testNG","Automation"})
public void testNG1()
{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);	
}
@Test(groups={"BDD"})
public void testNG2()
{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);	
}
@Test(groups={"Automation"})
public void testNG3()
{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);	
}
}

Xml file-

    
<?xml version="1.0" encoding="UTF-8"?>
<suite name="group">
<test name= "name">
<groups>
<run>
<include name= "testNG"></include>
<include name= "BDD"></include>
</run>
</groups>
<classes>
<class name="org.Testngpro.Testing_Grouping"></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 :testNG1
Method name is :testNG2

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

No comments:

Post a Comment