Chapter 10_2: TestNG groups

It is testing framework that designed for running different types of test designs like unit testing ,functional testing ,end to end testing and integration testing. We can run single or multiple packages through XML and run through maven.

In this chapter we will learn how to create TestNG groups with Example

 utilization of groups in TestNG when,

  • We don’t want to define test methods separately in different classes  and
  • At the same time want to ignore some test cases as if they does not exist in the code.
  • So to carry out this we have to Group them. This is done by using “include” and “exclude” mechanism supported in TestNG.

In below example, we have shown the syntax of how to use groups in the XML file.

@Test (groups = { "bonding", "strong_ties" })

Here we are using 2 group names i.e. “bonding” and “strong_ties”

<groups> tag defines the starting of groups in XML.

For Example:

Customize your XML to pick the mentioned group from the test classes. Below mentioned is the syntax of how to declare groups in XML file

<groups>		
   <run>		
    <include name="bonding" />		
   </run>		
  </groups>	

Introduction to XML and how to make an XML files

  • XML file in Maven framework contains the information of one or more tests and is defined by the tag <suite>.
  • Test information in XML is represented by tag <test> and can contain one or more TestNG classes.
  • A Java class which contains @Test annotation above test methods is defined as TestNG methods.
  • Multiple tags are used to define sequence of working TestNG XML like<suite> <test> and <class>
  • First is <suite> tag, which holds a logical name which defines  full information to TestNG to generate execution report.
  • Second is <test name=” Smoke Test Demo”>, note it is logical name which holds the information of test execution report like pass, fail, skip test cases and other information like total time for execution and group info
  • Third is <class name=”TP_Class1” />Testology is the package used, and Test Class name is TP_Class1.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">	
 	<suite name="Suite">	
		<test name=" Smoke Test Demo">	
			<groups>	
				<run>	
   					 <include name="strong_ties" />	
        		</run>	
       		</groups>	
			<classes>	
					<class name="Testology.TP_Class1" />	
           	</classes>	
		</test>	
  </suite>	

If we are finding the usage of group mechanism complex then TestNG XML facilitate the functionality to exclude/include a test.

Exclude Tag:  Syntax for exclude tag <exclude name="${TEST_CASE_NAME}" />
Include Tag:  Syntax for include tag <include name="${TEST_CASE_NAME}" />