Test Groups in TestNG

Let’s see the concept of Test Groups in TestNG. Grouping Test Methods in TestNG is one of the most important features of TestNG. We can group the Test Methods or Test Classes in TestNG. This feature allows the test methods to be segregated into different sections or modules.

 

With the help of Grouping, you can make the groups of Test cases like you can divide the test cases into multiple groups like Smoke, Sanity, and Regression. You can also execute specified test group using TestNG.

 

Test Groups in TestNG

You can assign the single Test to multiple groups and this test will run when you execute any associated group.

 

Video Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

 

Create & Execute Test Groups in TestNG

Here we are taking an example, In which we will create the two class “TestGroupExample” and “MultipleTestGroupExample” and later we will execute these classes group wise.

We can execute the Test Groups using amendments in our “testng.xml” file. To execute the Test group, you need to include the group in the testng.xml file.
Let’s see our Test Classes —

 

We can execute the Test Groups using amendments in our “testng.xml” file. To execute the Test group, you need to include the group in the testng.xml file.
Let’s see our Test Classes —

 

We have created our test Classes with “Groups” , Now Let’s see what amendment we need to do in our “testng.xml” to execute the specific group.

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd” >
  
<suite name=”TestGroupSuite” >

  <listeners>
      <listener class-name=”org.uncommons.reportng.HTMLReporter”/>
      <listener class-name=”org.uncommons.reportng.JUnitXMLReporter”/>
  </listeners>

  <test name=”Single Test Group” >
  

<!–   Add Groups in XML file –>
  <groups>
      <run>
        <include name=”Smoke” />
      </run>
    </groups>

    <classes>
       <class name=”com.Seleniumbix.TestNG.TestGroupExample” />
       <class name=”com.Seleniumbix.TestNG.MultipleTestGroupExample” />
    </classes>
  </test>

</suite>

 

Now let’s execute this as a suite and look at the report. As per our Test Class, 3 Test cases will run because we are including “Smoke” group and using both of the Test Classes and “Smoke” group is declared once in TestGroupExampleclass and twice in MultipleTestGroupExample class. So total 3 test case will execute. Lets have a look at the report.

TestGroup in TestNG

 

Execute Multiple Test Groups – To execute multiple groups, we need to add these groups in our “testng.xml” file and execute testng.xml as a suite.

 

Let’s understand this with the simple example. In our example, we are including the Smoke and Regression Tests in one group and executing these. Let’s see the “testng.xml” structure and execution report. (ReportNG report)

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd” >
  
<suite name=”TestGroupSuite” >

  <listeners>
      <listener class-name=”org.uncommons.reportng.HTMLReporter”/>
      <listener class-name=”org.uncommons.reportng.JUnitXMLReporter”/>
  </listeners>

  <test name=”Multiple Test Group” >
  
  <!–   Add Groups in XML file –>
  <groups>
      <run>
        <include name=”Smoke” />
        <include name=”Regression” />
      </run>
    </groups>
    
    <classes>
       <class name=”com.Seleniumbix.TestNG.TestGroupExample” />
       <class name=”com.Seleniumbix.TestNG.MultipleTestGroupExample” />
    </classes>
  </test>

</suite>

TestGroup in TestNG

So in this way, we can create and execute the groups in TestNG. You also can execute different groups in different Test cases.

 

Including and excluding groups

This is used to include and exclude the certain groups from the Execution. Suppose you have tagged Test case with multiple groups. Now you want to execute only specific group test cases and exclude other groups test cases. We can perform this using include and exclude in our “testng.xml” file.

 

Let’s understand this with a simple example. In our Test Classes(mentioned above) we have associated “Smoke” tag with 3 test cases, out of them one test is with “Smoke + Regression” now we have the requirement to execute smoke only. So we need to exclude Regression tests but if we run our file with “Smoke” only then all smoke and Smoke + other tag test cases will run.

 

So to overcome from this situation we need to use “Exclude” tag in our testng.xml and execute the testng.xml file. See the example –

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd” >
  
<suite name=”TestGroupSuite” >

  <listeners>
      <listener class-name=”org.uncommons.reportng.HTMLReporter”/>
      <listener class-name=”org.uncommons.reportng.JUnitXMLReporter”/>
  </listeners>

  <test name=”Multiple Test Group” >
  
  <!–   Add Groups in XML file –>
  <groups>
      <run>
        <include name=”Smoke” />
        <exclude name=”Regression” />
      </run>
    </groups>
    
    <classes>
       <class name=”com.Seleniumbix.TestNG.TestGroupExample” />
       <class name=”com.Seleniumbix.TestNG.MultipleTestGroupExample” />
    </classes>
  </test>

</suite>

Test Group in TestNG

Have you noticed, only tests with Smoke and Smoke + Sanity executed? “Smoke + Regression” test was skipped.

This is the way, How we can use the include and exclude with “Groups” in TestNG.

0 Comment

Leave a Reply