Spread the love

Learn How to Work on Groups in TestNG Effectively

Spread the love

What are the groups in TestNG?

Groups concept in TestNG is a unique feature that allows us to process methods in a certain manner. A group can contain a logical set of test cases. In terms of super flexibility, the group is an awesome concept. It lets us group test cases into separate logical boundaries and allows us to run them in any sequence (partially or sequentially).

In TestNG, groups can be specified with <groups> tag. <groups> tag is a child tag of <test> tag or a <suite> tag.Groups follow a proper parent child relationship. So groups marked under <suite> tag automatically applies to all the <test> tag in the child or grand child category.

TestNG provides an easy way to group our test cases without altering the source code of the test. A test method can be a part of any number of named groups. We can execute a particular set of test methods of a group or multiple groups.

A test method can be part of several groups, we created.

How to group a Test in TestNG?

Lets us create three groups namely

  • smoke
  • regression
  • normal

The test code will look like:

package com.test;

import org.testng.annotations.Test;

public class TestDisableMethods {
	@Test(groups= {"smoke","regression","normal"})
	public void test1() {
		System.out.println("Test1 is part of smoke, regression and normal groups");
	}
	@Test(groups= {"smoke","normal"})
	public void test2() {
		System.out.println("Test2 is part of smoke and normal");
	}@Test(groups= {"regression","normal"})
	public void test3() {
		System.out.println("Test3 is part of regression and normal");
	}@Test(groups= {"normal"})
	public void test4() {
		System.out.println("Test4 is part of normal group");
	}
}

How to execute the groups in Eclipse?

We can execute groups in Eclipse, by providing the groups we want to execute in TestNG configuration. The steps we need to follow:

  • Right-click on the test from tree view or in the test body
  • Select Run As
  • Select Run Configuration
TestNG groups run
TestNG groups run
  • Select Group checkbox and click on the Browse button
Select group and browse button in TestNG
Select the group and browse button in TestNG
  • This will list down all the groups we have used in our test. Check the groups we are interested in. then click on the OK button.
select a group and click on Ok
select a group and click on Ok
  • The confirmation window will tell us the groups we have selected.
The groups are selected click on run
The groups are selected click on run
  • Click on the Run button.

The corresponding testng.xml will be as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name = "MySuite">
   <test name = "MyTest">
   <groups>
   <run>
   <include name="normal"/>
   </run>
   </groups>
      <classes>
         <class name = "com.test.MyGroupTest"/>
      </classes>
   </test>
</suite>

The output will be:

[RemoteTestNG] detected TestNG version 7.0.0
Test1 is part of smoke, regression and normal groups
Test2 is part of smoke and normal
Test3 is part of regression and normal
Test4 is part of normal group

===============================================
MySuite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================

Now let us change the group name as smoke:

Select a group as smoke in the TestNG configuration
Select a group as smoke in the TestNG configuration

The corresponding testng.xml entry will be:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name = "MySuite">
   <test name = "MyTest">
   <groups>
   <run>
   <include name="smoke"/>
   </run>
   </groups>
      <classes>
         <class name = "com.test.MyGroupTest"/>
      </classes>
   </test>
</suite>

The output will be as follows:

READ  Learn How To Install TestNG for IDEs Quickly

[RemoteTestNG] detected TestNG version 7.0.0
Test1 is part of smoke, regression and normal groups
Test2 is part of smoke and normal
PASSED: test1
PASSED: test2

READ  What is TestNG?

===============================================
GRP-smoke
Tests run: 2, Failures: 0, Skips: 0
===============================================

===============================================
MyTestProject by groups
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

What is/are the test group of the group(s) in TestNG?

A group can also part of another group. Upon TestNG invocation, we can configure it to include a certain set of groups. In general, these groups are known as meta groups(the group about groups).

let us assume under the smoke test category, we have tests that run slowly and we have tests that run fast. So we can create a main group called smoke, under which we can create two more groups like “slowRun” and “firstRun”.

group of groups in TestNG
group of groups in TestNG

In the test level, we will only annotate them as the second-level group names.

package com.test;

import org.testng.annotations.Test;

public class MyGroupOfGroups {
	@Test(groups = { "slowRun" })
	public void f1() {
		System.out.println("F1- slow Run");
	}

	@Test(groups = { "slowRun" })
	public void f2() {
		System.out.println("F2- slow Run");
	}

	@Test(groups = { "fastRun" })
	public void f3() {
		System.out.println("F3- fast Run");
	}

	@Test(groups = { "fastRun" })
	public void f4() {
		System.out.println("F4- fast Run");
	}
}

The testng.xml will look like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name = "MySuite">
   <test name = "MyTest">
   <groups>
   <define name="smoke">
   		<include name="slowRun"/>
   		<include name="fastRun"/>
   </define>
   <run>
      <include name="smoke"/>
   </run>
   </groups>
      <classes>
         <class name = "com.test.MyGroupOfGroups"/>
      </classes>
   </test>
</suite>

The output will be as follows:
[RemoteTestNG] detected TestNG version 7.0.0
F1- slow Run
F2- slow Run
F3- fast Run
F4- fast Run

===============================================
MySuite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================

 

Exclusion of group/groups in TestNG

We can instruct TestNG to ignore some groups while executing the group of groups. Assume that we do not need to execute the slowRun category of testcases. That means we have to exclude the testcases that are part of “slowRun” group of the above example.

Now we can omit the group in two ways.

  1. Manual omission of group/groups
  2. Omission of group/groups by using tag

Manual omission

In the testng.xml we can remove the group/groups we do not need. like-

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name = "MySuite">
   <test name = "MyTest">
   <groups>
   <define name="smoke">

   		<include name="fastRun"/>
   </define>
   <run>
      <include name="smoke"/>
   </run>
   </groups>
      <classes>
         <class name = "com.test.MyGroupOfGroups"/>
      </classes>
   </test>
</suite>

The output of the execution :
[RemoteTestNG] detected TestNG version 7.0.0
F3- fast Run
F4- fast Run

===============================================
MySuite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
The above written Manual omission of group/groups has several disadvantages. They are as follows:

  • Manual task that means more prone to error.
  • Later we may forget the existence of the group itself.

The omission of group/groups by using the tag

This is the far most reliable technique while omitting the groups. We need to use the tag called <exclude name= “groupname”> in the testng.xml in order to omit a group.

Note:

  • We will not touch the definition of the smoke group. So the definition will be as it is.
  • <define name=”groupName”> accepts only include tag.
  • We will instruct the TestNG to ignore the desired group only during the run time. Hence we will change the <run> tag and provide the exclusion tag.
READ  What is TestNG?

The actual test code will be as it is:

package com.test;

import org.testng.annotations.Test;

public class MyGroupOfGroups {
	@Test(groups = { "slowRun" })
	public void f1() {
		System.out.println("F1- slow Run");
	}

	@Test(groups = { "slowRun" })
	public void f2() {
		System.out.println("F2- slow Run");
	}

	@Test(groups = { "fastRun" })
	public void f3() {
		System.out.println("F3- fast Run");
	}

	@Test(groups = { "fastRun" })
	public void f4() {
		System.out.println("F4- fast Run");
	}
}

The testng.xml will be changed as per our need:

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> 
 <suite name = "MySuite"> 
    <test name = "MyTest"> 
       <groups> 
       <define name="smoke"> 
           <include name="slowRun"/> 
           <include name="fastRun"/> 
       </define> 
       <run> 
           <include name="smoke"/>
           <exclude name="slowRun"/>
           
            </run> 
       </groups> 
       <classes> 
           <class name = "com.test.MyGroupOfGroups"/> 
       </classes> 
    </test>
 </suite>

The output of the test execution is as follows:

READ  Learn Annotations in TestNG in an Easy Effective and Quick Manner

[RemoteTestNG] detected TestNG version 7.0.0
F3- fast Run
F4- fast Run

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

Dependency of groups:

A test that depends on some other methods execute after the method(s), it is dependent on executes. Similarly a test can depends on a group too. in that case, a test that dependsĀ  on group or groups executes after all nethods of the dependent group or groups get executed.

There are two types of dependency

  • Hard dependency(expressed as @Test(alwaysRun=false))
  • Soft dependency(expressed as @Test(alwaysRun=true))

Hard dependency

Hard dependency implies that the dependant test cases will be ignored and reported as skipped in the test report in case any of the tests depend on fail.

Soft dependency

Soft dependency implies that the method will always be executed irrespective of the fact that the test s on which it depends on pass or fails. However, this test case always runs after the tests it depends on gets executed.

Let us create methods under com.Test2 package in Test2Class. The class will look like:

package com.test2;

import org.testng.annotations.Test;

public class Test2Class {

	
		@Test(groups= {"precheck"})
		public void myTest2() {
			System.out.println("This is test2 and part of precheck");
		}
		@Test(groups= {"precheck"})
		public void myTest4() {
			System.out.println("This is test4 and part of precheck");
		}
		@Test(dependsOnGroups= {"precheck"})
		public void myTest5() {
			System.out.println("This is test5 . It is depending on precheck methods");
		}
	}

The methods say that myTest5() method depends on the precheck group that has two methods, myTest2() and myTest4().

create group dependency in TestNG
create group dependency in TestNG

The corresponding testng.xml will look like below:

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> 
 <suite name = "MyGroupDependencyCheck" verbose="1"> 
    <test name = "MyGroupDependencyCheck"> 
       <classes>
            <class name="com.test2.Test2Class">
       </class>
       </classes>
	</test>
 </suite>

The execution report is as follows:

groups dependency check
groups dependency check

The console output is as follows:

[RemoteTestNG] detected TestNG version 7.0.0
This is test2 and part of precheck
This is test4 and part of precheck
This is test5 . It is depending on precheck methods

===============================================
MyGroupDependencyCheck
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================

Conclusion

In TestNG, groups are the controller for test execution in a way that groups can include or exclude test cases from our long test suites. Also, the priority attribute governs the sequence of the group method execution.

READ  Learn Annotations in TestNG in an Easy Effective and Quick Manner

If you enjoyed this post, please share it on social media ?What am I missing here? Let me know in the comments and I’ll add it in!”

How to Test Expected Exceptions in TestNG?

TestNG can track the exception handling of the development code. In this case, we have to specify the desired exception in the @Test as a parameter.

The syntax is as follows:

@Test(expectedException.class)
public void f()
{
}

Let us check an example:
We will create a development code that will try to load a file. And intentionally, we will not provide a correct code (just to create the FileNotFound exception).
Our objective is to get a pass result from the test as we know this test is going to throw the FileNotFoundException.

package com.dev;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileLoader {
	private String filepath;
	public FileLoader() {}
	public FileLoader(String path) throws IOException {
		filepath=path;

	}
	public void loadAFile(String filepath) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(filepath)); 
		  
		  String st; 
		  while ((st = br.readLine()) != null) 
		    System.out.println(st); 
		  } 
	

}

Our test code goes like below:

package com.test;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.testng.annotations.Test;

import com.dev.FileLoader;

public class MyExpectedExceptionTest {
	
String filePath="E:\\Test.txt";

@Test
public void testFileLoad() throws IOException {
	FileLoader fl= new FileLoader(filePath);
	fl.loadAFile(filePath);
}
}

This is just to create the Exception. Note here @Test we have not given anything.
The testng.xml will look like

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> 
 <suite name = "MySuite"> 
    <test name = "MyTest"> 
      
       <classes> 
           <class name = "com.test.MyExpectedExceptionTest"/> 
       </classes> 
    </test>
 </suite>

The output of the execution from console is as follows:

READ  Learn How To Install TestNG for IDEs Quickly

[RemoteTestNG] detected TestNG version 7.0.0
FAILED: testFileLoad
java.io.FileNotFoundException: E:\Test.txt (The system cannot find the file specified)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:213)
at java.base/java.io.FileInputStream.(FileInputStream.java:155)
at java.base/java.io.FileInputStream.(FileInputStream.java:110)
at java.base/java.io.FileReader.(FileReader.java:60)
at com.dev.FileLoader.loadAFile(FileLoader.java:17)
at com.test.MyExpectedExceptionTest.testFileLoad(MyExpectedExceptionTest.java:17)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
at org.testng.TestRunner.privateRun(TestRunner.java:770)
at org.testng.TestRunner.run(TestRunner.java:591)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
at org.testng.SuiteRunner.run(SuiteRunner.java:304)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
at org.testng.TestNG.runSuites(TestNG.java:1032)
at org.testng.TestNG.run(TestNG.java:1000)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

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

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

The output after running from the testng.xml as follows:
[RemoteTestNG] detected TestNG version 7.0.0

===============================================
MySuite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
===============================================

Our objective does not go through. Now let us provide the actual exception handling code.

The test code will look like-

package com.test;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.testng.annotations.Test;

import com.dev.FileLoader;

public class MyExpectedExceptionTest {
	
String filePath="E:\\Test.txt";

@Test(expectedExceptions=FileNotFoundException.class)
public void testFileLoad() throws IOException {
	FileLoader fl= new FileLoader(filePath);
	fl.loadAFile(filePath);
}
}

There is no change in the testng.xml or the development code. Now let us check the output.
The console output is as follows:
[RemoteTestNG] detected TestNG version 7.0.0
PASSED: testFileLoad

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

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

The testng.xml execution is as follows:
[RemoteTestNG] detected TestNG version 7.0.0

===============================================
MySuite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================




Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

Learn Quickly on How to Work With TestNG XML

What is TestNG XML? how does it look? TestNG XML being the...

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent Posts

Learn Quickly About DevOps Tools in a Fast and Efficient Manner

Introduction to DevOps tools DevOps tools are a collection of...

How to Collaborate between Development and Operations Team Fast and Effectively

Collaborate between Development and Operations Teams are the key...

Learn How to Use Jacoco Java code Coverage

Introduction to Jacoco Jacoco provides a wide range of coverage(analysis...

EDITOR PICKS


Spread the love