{"id":9735,"date":"2023-09-14T08:09:18","date_gmt":"2023-09-14T08:09:18","guid":{"rendered":"https:\/\/testgrid.io\/blog\/?p=9735"},"modified":"2025-05-30T15:41:59","modified_gmt":"2025-05-30T15:41:59","slug":"testng-groups","status":"publish","type":"post","link":"https:\/\/testgrid.io\/blog\/testng-groups\/","title":{"rendered":"The Ultimate Guide to TestNG Groups"},"content":{"rendered":"\n<p>Software testing is a crucial part of the software development lifecycle which ensures the quality and usability of the application. Automating the tests brings in a lot of ease, adding the efficiency and the organisation of tests to the entire process. Selenium automation is used with the TestNG framework widely, to enhance the maintenance and execution of the scripts. This widely used TestNG framework provides a very powerful feature known as \u201cTestNG Groups\u201d. These groups provide an efficient and a structured way to categorise and prioritise test cases.<\/p>\n\n\n\n<p>In this article, we will dive into the depths of TestNG groups, try to understand their importance, syntax and look at some practical use-case for the same. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are TestNG Groups?<\/h2>\n\n\n\n<p>TestNG Groups allow a way to group multiple test cases written in Selenium to be grouped under a common name. Using groups in TestNG you can run multiple test cases which are tagged using a group name together, thereby separating the test cases as per their tagged groups.<\/p>\n\n\n\n<p>Consider a scenario, where there are multiple test cases in a test suite. These test cases are a part of different test types like unit test, smoke test, regression test, sanity test, etc. Now, if you need to maintain your Selenium test cases, you can use TestNG groups using which you can segregate these test cases using groups for these test methods. This way you would not need to maintain multiple test suites for the different tests.<\/p>\n\n\n\n<p>Now, we know that TestNG groups can help us maintain our tests as per the different test categories, let us see the advantages offered by using TestNG Groups.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Advantages Of TestNG Groups<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Selective Test Executions<\/h3>\n\n\n\n<p>TestNG Groups allows execution of specific test clusters. This can be very beneficial when you need to test your application against the different quality parameters like functionality, or security. You can cherry pick your tests using groups and see the behaviour of the application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Streamlined Test Suite Management<\/h3>\n\n\n\n<p>Automation test suites are bound to expand. It can be very challenging to maintain the tests individually with this growth. TestNG Groups provide ease in maintenance of these tests in a systematic manner.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parallel Test Execution<\/h3>\n\n\n\n<p>Tests that are tagged within the same group in TestNG can be executed in parallel. Using these capabilities you can marginally bring down the execution time for your tests, thereby increasing efficiency.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Adaptability and Customization<\/h3>\n\n\n\n<p>Using TestNG groups you can have the flexibility to design and manage your tests as per the need of your project. This capability allows you to ensure that your test strategy is well aligned to the demands of your project.<\/p>\n\n\n\n<p>Also Read: <a href=\"https:\/\/testgrid.io\/blog\/testng-listeners-in-selenium\/\" data-type=\"URL\" data-id=\"https:\/\/testgrid.io\/blog\/testng-listeners-in-selenium\/\">TestNG Listeners in Selenium<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to group test cases in TestNG?<\/h2>\n\n\n\n<p>To specify groups in TestNG, you need to use the \u201c<em>group\u201d <\/em>attribute with the <a href=\"https:\/\/testgrid.io\/blog\/testng-annotations\/\" data-type=\"URL\" data-id=\"https:\/\/testgrid.io\/blog\/testng-annotations\/\">@Test annotation<\/a>. Additionally, you need to specify&nbsp; groups in the testng.xml under the &lt;suite&gt; or &lt;test&gt; tag. We will now walk through the steps you will need to follow to group your test cases in testNG.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Annotating the test methods<\/h3>\n\n\n\n<p>To begin with the steps of using TestNG Groups, you will have to group the test cases as per their type. Parameter <strong><em>groups <\/em><\/strong>&nbsp;is passed to the @Test annotation which specifies which group the test belongs to. The syntax to do so is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>@Test(groups =\"group_name\")\n\tpublic void testMethod() {\n\t\t\/\/Test code here\n\t}<\/code><\/pre>\n\n\n\n<p>You also have the flexibility to assign a test method to multiple test groups. An example for the same is:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>@Test(groups ={\"smoke\", \"regression\"})\n\tpublic void testLogin() {\n\t\t\/\/Test code here\n\t}\n<\/code><\/pre>\n\n\n\n<p>In the above code, the testLogin() method will be a part of both the groups viz, smoke and regression.<\/p>\n\n\n\n<p>In the above code, the testLogin() method will be a part of both the groups viz, smoke and regression.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Specifying the test groups in testng xml<\/h3>\n\n\n\n<p>You can define the test groups in the testNG XML configuration. You can flexibly include or exclude the test groups you would like to execute during a test run. To do so, you need to configure the &lt;groups&gt; tag within the &lt;test&gt;. The syntax to do so is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;!DOCTYPE suite SYSTEM \"https:\/\/testng.org\/testng-1.0.dtd\"&gt;\n&lt;suite name=\"Suite\"&gt;\n&lt;test thread-count=\"5\" name=\"Test\"&gt;\n\t &lt;groups&gt;\n\t\t &lt;run&gt;\n\t\t\t &lt;include name=\"smoke\"&gt; &lt;\/include&gt;\n\t         &lt;\/run&gt;\n\t &lt;\/groups&gt;\n&lt;classes&gt;\n&lt;class name=\"testngGroups.groups.groupTests\"\/&gt;\n&lt;\/classes&gt;\n&lt;\/test&gt; &lt;!-- Test --&gt;\n&lt;\/suite&gt; &lt;!-- Suite --&gt;<\/code><\/pre>\n\n\n\n<p>In the above example, all the test methods that are grouped as <em>smoke<\/em> will be executed when you try to run the testng suite. If you need to exclude a particular group in an execution you can define it within the &lt;exclude&gt; tag as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;!DOCTYPE suite SYSTEM \"https:\/\/testng.org\/testng-1.0.dtd\"&gt;\n&lt;suite name=\"Suite\"&gt;\n&lt;test thread-count=\"5\" name=\"Test\"&gt;\n\t &lt;groups&gt;\n\t\t &lt;run&gt;\n\t\t\t &lt;include name=\"smoke\"&gt; &lt;\/include&gt;\n\t\t\t &lt;exclude name=\"regression\"&gt; &lt;\/exclude&gt;\n\t\t &lt;\/run&gt;\n\t &lt;\/groups&gt;\n&lt;classes&gt;\n&lt;class name=\"testngGroups.groups.groupTests\"\/&gt;\n&lt;\/classes&gt;\n&lt;\/test&gt; &lt;!-- Test --&gt;\n&lt;\/suite&gt; &lt;!-- Suite --&gt;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Executing the TestNG tests with the group configuration<\/h2>\n\n\n\n<p>After setting up the code as well as the xml configuration file, you can simply execute your tests either through an IDE, or through command line, or even the build tools like Maven. Upon execution you will see that the groups you specified in the &lt;include&gt; tag would have executed and if there was any group within the &lt;exclude&gt; that it would not have been executed.<\/p>\n\n\n\n<p>Now that we know the steps to implement TestNG Groups, we will see the demonstrations corresponding to different scenarios we might want to execute using them.<\/p>\n\n\n\n<p><strong><em><a href=\"https:\/\/testgrid.io\/blog\/junit-vs-testng\/\" data-type=\"URL\" data-id=\"https:\/\/testgrid.io\/blog\/junit-vs-testng\/\">Compare JUnit and TestNG<\/a> side-by-side in this comprehensive article<\/em><\/strong> <strong>to learn which testing framework is right for your project.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Demonstrations<\/strong>(Testng Groups Example)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Running Test Cases Of Same Group<\/strong><\/h3>\n\n\n\n<p>We will now create a test class where we will be creating two test methods and assign them to a group named <strong><em>url.<\/em><\/strong> Let us look at the Selenium Java code for the same:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>package pomBatchOne.tests;\nimport org.testng.annotations.Test;\nimport org.openqa.selenium.WebDriver;\nimport org.openqa.selenium.chrome.ChromeDriver;\npublic class urlTest {\n\t\n\tWebDriver driver;\n\t\n\t@Test(groups=\"url\")\n\tpublic void testgrid() {\n\t\tdriver = new ChromeDriver();\n\t\tdriver.manage().window().maximize();\n\t\tdriver.get(\"https:\/\/testgrid.io\/\");\n\t\tSystem.out.println(driver.getTitle());\n\t\tdriver.quit();\n\t}\n\t\n\t@Test(groups=\"url\")\n\tpublic void google() {\n\t\tdriver = new ChromeDriver();\n\t\tdriver.manage().window().maximize();\n\t\tdriver.get(\"https:\/\/www.google.com\");\n\t\tSystem.out.println(driver.getTitle());\n\t\tdriver.quit();\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p>As you can see, groups have been assigned to the two tests. Let us have a look at the testng xml now-<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;!DOCTYPE suite SYSTEM \"https:\/\/testng.org\/testng-1.0.dtd\"&gt;\n&lt;suite name=\"Suite\"&gt;\n&lt;test thread-count=\"5\" name=\"Test\"&gt;\n\t &lt;groups&gt;\n\t\t &lt;run&gt;\n\t\t\t &lt;include name=\"url\"&gt;&lt;\/include&gt;\n\t\t &lt;\/run&gt;\n\t &lt;\/groups&gt;\n&lt;classes&gt;\n&lt;class name=\"pomBatchOne.tests.urlTest\"\/&gt;\n&lt;\/classes&gt;\n&lt;\/test&gt; &lt;!-- Test --&gt;\n&lt;\/suite&gt; &lt;!-- Suite --&gt;<\/code><\/pre>\n\n\n\n<p>Since we need to include the group \u201curl\u201d for execution, we have written it to be included within the test tag as can be seen from the xml above. Let us now execute the testng xml and see the result-<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"970\" height=\"324\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image3.png\" alt=\"\" class=\"wp-image-9736\" loading=\"lazy\" title=\"\" srcset=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image3.png 970w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image3-300x100.png 300w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image3-768x257.png 768w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image3-150x50.png 150w\" sizes=\"auto, (max-width: 970px) 100vw, 970px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"437\" height=\"149\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image7.png\" alt=\"\" class=\"wp-image-9737\" loading=\"lazy\" title=\"\" srcset=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image7.png 437w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image7-300x102.png 300w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image7-150x51.png 150w\" sizes=\"auto, (max-width: 437px) 100vw, 437px\" \/><\/figure>\n\n\n\n<p>As can be seen from the above snapshot, the execution was successful and our test was executed where the group \u201curl\u201d was included while execution.<\/p>\n\n\n\n<p>Next, we will see a demonstration to execute tests that are within multiple groups.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Running Test Cases Of Multiple Groups<\/strong><\/h3>\n\n\n\n<p>We just saw how to write and execute the test methods that are a part of a group. There may be scenarios where a test method might be a part of multiple groups. Additionally, you might want to execute your tests so that the test methods belonging to smoke and regression tests are executed in one go. For example you might want to execute a login test case in the smoke as well as a regression test suite. TestNG groups provide you the flexibility to assign a test case to multiple groups as per the project requirements. Along with that you may include multiple groups to be executed through testng xml.<\/p>\n\n\n\n<p>Below code shows example for different test methods corresponding to different groups-<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>package pomBatchOne.tests;\nimport org.testng.annotations.Test;\nimport org.testng.annotations.Test;\nimport org.openqa.selenium.By;\nimport org.openqa.selenium.WebDriver;\nimport org.openqa.selenium.WebElement;\nimport org.openqa.selenium.chrome.ChromeDriver;\npublic class urlTest {\n\t\n\tWebDriver driver;\n\t\n\t@Test(groups=\"url\")\n\tpublic void testgrid() {\n\t\tdriver = new ChromeDriver();\n\t\tdriver.manage().window().maximize();\n\t\tdriver.get(\"https:\/\/testgrid.io\/\");\n\t\tSystem.out.println(driver.getTitle());\n\t\tdriver.quit();\n\t}\n\t\n\t@Test(groups=\"url\")\n\tpublic void google() {\n\t\tdriver = new ChromeDriver();\ndriver.manage().window().maximize();\n\t\tdriver.get(\"https:\/\/www.google.com\");\n\t\tSystem.out.println(driver.getTitle());\n\t\tdriver.quit();\n\t}\n\t\n\t@Test(groups=\"search\")\n\tpublic void googleSearchOne() {\n\t\tdriver = new ChromeDriver();\n\t\tdriver.manage().window().maximize();\n\t\tdriver.get(\"https:\/\/www.google.com\");\n\t\tdriver.findElement(By.name(\"q\")).sendKeys(\"testng groups in selenium\");\n\t\tdriver.findElement(By.name(\"btnK\")).click();\n\t\tSystem.out.println(driver.getTitle());\n\t\tdriver.quit();\n\t}\n\t\n\t@Test(groups=\"search\")\n\tpublic void googleSearchTwo() {\n\t\tdriver = new ChromeDriver();\n\t\tdriver.manage().window().maximize();\n\t\tdriver.get(\"https:\/\/www.google.com\");\n\t\tdriver.findElement(By.name(\"q\")).sendKeys(\"testgrid features and benefits\");\n\t\tdriver.findElement(By.name(\"btnK\")).click();\n\t\tSystem.out.println(driver.getTitle());\n\t\tdriver.quit();\n\t}\n\t\n\t@Test(groups= {\"url\",\"search\"})\n\tpublic void groupMethod() {\n\t\tSystem.out.println(\"This is a sample method that is part of both url and search group...\");\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p>The testng.xml that includes both the groups for execution will look like below-<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;!DOCTYPE suite SYSTEM \"https:\/\/testng.org\/testng-1.0.dtd\"&gt;\n&lt;suite name=\"Suite\"&gt;\n&lt;test thread-count=\"5\" name=\"Test\"&gt;\n\t &lt;groups&gt;\n\t\t &lt;run&gt;\n\t\t\t &lt;include name=\"url\"&gt;&lt;\/include&gt;\n\t\t\t &lt;include name=\"search\"&gt;&lt;\/include&gt;\n\t\t &lt;\/run&gt;\n\t &lt;\/groups&gt;\n&lt;classes&gt;\n&lt;class name=\"pomBatchOne.tests.urlTest\"\/&gt;\n&lt;\/classes&gt;\n&lt;\/test&gt; &lt;!-- Test --&gt;\n&lt;\/suite&gt; &lt;!-- Suite --&gt;<\/code><\/pre>\n\n\n\n<p>Upon executing the xml you will see execution results like below-<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"977\" height=\"153\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image10.png\" alt=\"\" class=\"wp-image-9738\" loading=\"lazy\" title=\"\" srcset=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image10.png 977w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image10-300x47.png 300w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image10-768x120.png 768w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image10-150x23.png 150w\" sizes=\"auto, (max-width: 977px) 100vw, 977px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"916\" height=\"244\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image1.png\" alt=\"\" class=\"wp-image-9739\" loading=\"lazy\" title=\"\" srcset=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image1.png 916w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image1-300x80.png 300w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image1-768x205.png 768w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image1-150x40.png 150w\" sizes=\"auto, (max-width: 916px) 100vw, 916px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"437\" height=\"228\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image9.png\" alt=\"\" class=\"wp-image-9740\" loading=\"lazy\" title=\"\" srcset=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image9.png 437w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image9-300x157.png 300w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image9-363x188.png 363w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image9-150x78.png 150w\" sizes=\"auto, (max-width: 437px) 100vw, 437px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Groups of Groups in TestNG<\/strong><\/h3>\n\n\n\n<p>In TestNG, groups can be included in other groups. This type of group set up is called \u201cMetaGroups\u201d. Using these MetaGroups you can create a collection of tests such that you can execute tests tagged within different groups as a single test. In the below example, we are tagging the different test methods as \u201curl1\u201d, \u201curl2\u201d, \u201csearch1\u201d and \u201csearch2\u201d respectively.<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>package pomBatchOne.tests;\nimport org.testng.annotations.Test;\nimport org.testng.annotations.Test;\nimport org.openqa.selenium.By;\nimport org.openqa.selenium.WebDriver;\nimport org.openqa.selenium.WebElement;\nimport org.openqa.selenium.chrome.ChromeDriver;\npublic class urlTest {\n\t\n\tWebDriver driver;\n\t\n\t@Test(groups=\"url1\")\n\tpublic void testgrid() {\n\t\tdriver = new ChromeDriver();\n\t\tdriver.manage().window().maximize();\n\t\tdriver.get(\"https:\/\/testgrid.io\/\");\n\t\tSystem.out.println(driver.getTitle());\n\t\tdriver.quit();\n\t}\n\n\n\t@Test(groups=\"url2\")\n\tpublic void google() {\n\t\tdriver = new ChromeDriver();\n\t\tdriver.manage().window().maximize();\n\t\tdriver.get(\"https:\/\/www.google.com\");\n\t\tSystem.out.println(driver.getTitle());\n\t\tdriver.quit();\n\t}\n\t\n\t@Test(groups=\"search1\")\n\tpublic void googleSearchOne() {\n\t\tdriver = new ChromeDriver();\n\t\tdriver.manage().window().maximize();\n\t\tdriver.get(\"https:\/\/www.google.com\");\n\t\tdriver.findElement(By.name(\"q\")).sendKeys(\"testng groups in selenium\");\n\t\tdriver.findElement(By.name(\"btnK\")).click();\n\t\tSystem.out.println(driver.getTitle());\n\t\tdriver.quit();\n\t}\n\t\n\t@Test(groups=\"search2\")\n\tpublic void googleSearchTwo() {\n\t\tdriver = new ChromeDriver();\n\t\tdriver.manage().window().maximize();\n\t\tdriver.get(\"https:\/\/www.google.com\");\n\t\tdriver.findElement(By.name(\"q\")).sendKeys(\"testgrid features and benefits\");\n\t\tdriver.findElement(By.name(\"btnK\")).click();\n\t\tSystem.out.println(driver.getTitle());\n\t\tdriver.quit();\n\t}\n\t\n}\n<\/code><\/pre>\n\n\n\n<p>Now, in the testng.xml, we define two new group names, i.e, \u201curlGroup\u201d and \u201csearchGroup\u201d which would include the individual groups as shown below-<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;!DOCTYPE suite SYSTEM \"https:\/\/testng.org\/testng-1.0.dtd\"&gt;\n&lt;suite name=\"Suite\"&gt;\n&lt;test thread-count=\"5\" name=\"Test\"&gt;\n\t &lt;groups&gt;\n\t\t &lt;define name=\"urlGroup\"&gt;\n\t\t\t &lt;include name=\"url1\"&gt;&lt;\/include&gt;\n\t\t\t &lt;include name=\"url2\"&gt;&lt;\/include&gt;\n\t\t &lt;\/define&gt;\n\t\t &lt;define name=\"searchGroup\"&gt;\n\t\t\t &lt;include name=\"search1\"&gt;&lt;\/include&gt;\n\t\t\t &lt;include name=\"search2\"&gt;&lt;\/include&gt;\n\t\t &lt;\/define&gt;\n\t\t\n\t\t &lt;run&gt;\n\t\t\t &lt;include name=\"urlGroup\"&gt;&lt;\/include&gt;\n\t\t\t &lt;include name=\"searchGroup\"&gt;&lt;\/include&gt;\n\t\t &lt;\/run&gt;\n\t &lt;\/groups&gt;\n&lt;classes&gt;\n&lt;class name=\"pomBatchOne.tests.urlTest\"\/&gt;\n&lt;\/classes&gt;\n&lt;\/test&gt; &lt;!-- Test --&gt;\n&lt;\/suite&gt; &lt;!-- Suite --&gt;<\/code><\/pre>\n\n\n\n<p>As you can see, we can include these groups of group directly to execute. Additionally you may create a super group including all the groups you would want to run in an execution if you make a few changes in the xml like below-<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;!DOCTYPE suite SYSTEM \"https:\/\/testng.org\/testng-1.0.dtd\"&gt;\n&lt;suite name=\"Suite\"&gt;\n&lt;test thread-count=\"5\" name=\"Test\"&gt;\n\t &lt;groups&gt;\n\t\t &lt;define name=\"urlGroup\"&gt;\n\t\t\t &lt;include name=\"url1\"&gt;&lt;\/include&gt;\n\t\t\t &lt;include name=\"url2\"&gt;&lt;\/include&gt;\n\t\t &lt;\/define&gt;\n\t\t &lt;define name=\"searchGroup\"&gt;\n                         &lt;include name=\"search1\"&gt;&lt;\/include&gt;\n\t\t\t &lt;include name=\"search2\"&gt;&lt;\/include&gt;\n\t\t &lt;\/define&gt;\n\t\t &lt;define name=\"superGroup\"&gt;\n\t\t\t &lt;include name=\"urlGroup\"&gt;&lt;\/include&gt;\n\t\t\t &lt;include name=\"searchGroup\"&gt;&lt;\/include&gt;\n\t\t &lt;\/define&gt;\n\t\t &lt;run&gt;\n\t\t\t &lt;include name=\"superGroup\"&gt;&lt;\/include&gt;\n\t\t &lt;\/run&gt;\n\t &lt;\/groups&gt;\n&lt;classes&gt;\n&lt;class name=\"pomBatchOne.tests.urlTest\"\/&gt;\n&lt;\/classes&gt;\n&lt;\/test&gt; &lt;!-- Test --&gt;\n&lt;\/suite&gt; &lt;!-- Suite --&gt;\n<\/code><\/pre>\n\n\n\n<p>When you execute the above testng suite, you will see that all the groups that have been defined under superGroup would be executed-<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"478\" height=\"196\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image4.png\" alt=\"\" class=\"wp-image-9741\" loading=\"lazy\" title=\"\" srcset=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image4.png 478w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image4-300x123.png 300w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image4-150x62.png 150w\" sizes=\"auto, (max-width: 478px) 100vw, 478px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Include and Exclude Groups in TestNG<\/strong><\/h2>\n\n\n\n<p>As you have already seen that we can include the groups we want to execute in our test. In a similar way you may explicitly exclude a specific group from execution. Let us take a simple example, where we will be creating multiple test methods with different groups and a couple of test methods which have multiple groups.<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>package pomBatchOne.tests;\nimport org.testng.annotations.Test;\npublic class IncExc {\n\t\n\t@Test(groups=\"gpOne\")\n\tpublic void testOne() {\n\t\tSystem.out.println(\"The test belongs to Group One\");\n\t}\n\t@Test(groups=\"gpTwo\")\n\tpublic void testTwo() {\n\t\tSystem.out.println(\"The test belongs to Group Two\");\n\t}\n\t\n\t@Test(groups=\"gpOne\")\n\tpublic void testThree() {\n\t\tSystem.out.println(\"The test belongs to Group One\");\n\t}\n\t\n\t@Test(groups=\"gpTwo\")\n\tpublic void testFour() {\n\t\tSystem.out.println(\"The test belongs to Group Two\");\n\t}\n\t\n\t@Test(groups={\"gpOne\",\"gpTwo\"})\n\tpublic void testFive() {\n\t\tSystem.out.println(\"The test belongs to Group One as well as Group Two\");\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p>As you can see from the code above, we have created five test methods. Of these two belong to \u201cgpOne\u201d only, two belong to \u201cgpTwo\u201d only and one belong to both \u201cgpOne\u201d and \u201cgpTwo\u201d. We will now create testng.xml for the class, so that we include \u201cgpOne\u201d in execution and exclude\u201dgpTwo\u201d in execution and then see the result.<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;!DOCTYPE suite SYSTEM \"https:\/\/testng.org\/testng-1.0.dtd\"&gt;\n&lt;suite name=\"Suite\"&gt;\n&lt;test thread-count=\"5\" name=\"Test\"&gt;\n\t &lt;groups&gt;\n\t\t &lt;run&gt;\n\t\t\t &lt;include name=\"gpOne\"&gt;&lt;\/include&gt;\n\t\t\t &lt;exclude name=\"gpTwo\"&gt;&lt;\/exclude&gt;\n\t\t &lt;\/run&gt;\n\t\t\n\t &lt;\/groups&gt;\n&lt;classes&gt;\n&lt;class name=\"pomBatchOne.tests.IncExc\"\/&gt;\n&lt;\/classes&gt;\n&lt;\/test&gt; &lt;!-- Test --&gt;\n&lt;\/suite&gt; &lt;!-- Suite --&gt;<\/code><\/pre>\n\n\n\n<p>Upon execution, we see that only the test methods that belong to \u201cgpOne\u201d are executed. No test method that has the tag of \u201cgpTwo\u201d is executed. This means that even the test method that was a part of multiple groups, viz \u201cgpOne\u201d and \u201cgpTwo\u201d is also not executed. The results from console are as below-<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"803\" height=\"236\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image8.png\" alt=\"\" class=\"wp-image-9742\" loading=\"lazy\" title=\"\" srcset=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image8.png 803w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image8-300x88.png 300w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image8-768x226.png 768w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image8-150x44.png 150w\" sizes=\"auto, (max-width: 803px) 100vw, 803px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"376\" height=\"172\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image6.png\" alt=\"\" class=\"wp-image-9743\" loading=\"lazy\" title=\"\" srcset=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image6.png 376w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image6-300x137.png 300w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image6-150x69.png 150w\" sizes=\"auto, (max-width: 376px) 100vw, 376px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using Regular Expressions in TestNG Groups<\/strong><\/h2>\n\n\n\n<p>You can use regular expressions like \u201c.*\u201d which implies anything that matches the given pattern, in testng xml for executing specific groups. Note that you should not confuse regular expressions with Wildmats, i.e \u201c*\u201d while using TestNG. We will take the example of below code where there are multiple test methods tagged with groups like: \u201cgpOne\u201d,\u201dgpTwo\u201d and \u201dGroupOne\u201d.<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>package pomBatchOne.tests;\nimport org.testng.annotations.Test;\npublic class RegularExp {\n\t@Test(groups=\"gpOne\")\n\tpublic void testOne() {\n\t\tSystem.out.println(\"The test belongs to Gp One\");\n\t}\n\t@Test(groups=\"gpTwo\")\n\tpublic void testTwo() {\n\t\tSystem.out.println(\"The test belongs to Gp Two\");\n\t}\n\t\n\t@Test(groups=\"gpOne\")\n\tpublic void testThree() {\n\t\tSystem.out.println(\"The test belongs to Gp One\");\n\t}\n\t\n\t@Test(groups=\"gpTwo\")\n\tpublic void testFour() {\n\t\tSystem.out.println(\"The test belongs to Gp Two\");\n\t}\n\t\n\t@Test(groups={\"gpOne\",\"gpTwo\"})\n\tpublic void testFive() {\n\t\tSystem.out.println(\"The test belongs to Gp One as well as Gp Two\");\n\t}\n\t@Test(groups=\"GroupOne\")\n\tpublic void testSix() {\n\t\tSystem.out.println(\"The test belongs to Group One\");\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p>We will not create the testng.xml to include the execution of tests that begin with \u201cgp\u201d in their name. For doing so we will use regular expressions.<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;!DOCTYPE suite SYSTEM \"https:\/\/testng.org\/testng-1.0.dtd\"&gt;\n&lt;suite name=\"Suite\"&gt;\n&lt;test thread-count=\"5\" name=\"Test\"&gt;\n\t &lt;groups&gt;\n\t\t &lt;run&gt;\n\t\t\t &lt;include name=\"gp.*\"&gt;&lt;\/include&gt;\n\t\t &lt;\/run&gt;\n\t\t\n\t &lt;\/groups&gt;\n&lt;classes&gt;\n&lt;class name=\"pomBatchOne.tests.IncExc\"\/&gt;\n&lt;\/classes&gt;\n&lt;\/test&gt; &lt;!-- Test --&gt;\n&lt;\/suite&gt; &lt;!-- Suite --&gt;\n\n<\/code><\/pre>\n\n\n\n<p>Upon executing the above xml, you will see the results where five test methods will be executed and only one method which is from the \u201cGroupOne\u201d will not be executed.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"914\" height=\"256\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image2.png\" alt=\"\" class=\"wp-image-9744\" loading=\"lazy\" title=\"\" srcset=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image2.png 914w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image2-300x84.png 300w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image2-768x215.png 768w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image2-150x42.png 150w\" sizes=\"auto, (max-width: 914px) 100vw, 914px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"443\" height=\"212\" src=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image5.png\" alt=\"\" class=\"wp-image-9745\" loading=\"lazy\" title=\"\" srcset=\"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image5.png 443w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image5-300x144.png 300w, https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/image5-150x72.png 150w\" sizes=\"auto, (max-width: 443px) 100vw, 443px\" \/><\/figure>\n\n\n\n<p><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>And with this, we wrap up the understanding of using TestNG Groups in our test executions. We now know how beneficial and efficient using TestNG Groups is when we want to maintain our test suite as per the testing needs. By identifying groups for the test methods it becomes easier to execute tests corresponding to a specific type. Additionally you may leverage the use of parallel execution to achieve quick results. Just by simply, using groups as a parameter in the @Test annotation you can make use of this feature by including or excluding the group from the testng xml. Additionally, you may create flexible test scripts by using single or multiple or even combinations of groups for your test methods. You can group these groups for better efficiency of execution and also use regular expressions to simplify using groups with similar names.<\/p>\n\n\n\n<p>If you\u2019re looking for a comprehensive testing platform that lets you make the most of AI for codeless automation, app testing, and cross browser testing then TestGrid is your best shot.\u00a0<a href=\"https:\/\/public.testgrid.io\/signup\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/public.testgrid.io\/signup\" rel=\"noreferrer noopener\">Sign up for free<\/a>\u00a0and check TestGrid today or\u00a0<a href=\"https:\/\/testgrid.io\/request-demo\" target=\"_blank\" rel=\"noreferrer noopener\">book a demo<\/a>\u00a0to get an in-depth walkthrough of \u00a0 100+ features.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Software testing is a crucial part of the software development lifecycle which ensures the quality and usability of the application. Automating the tests brings in a lot of ease, adding the efficiency and the organisation of tests to the entire process. Selenium automation is used with the TestNG framework widely, to enhance the maintenance and [&hellip;]<\/p>\n","protected":false},"author":29,"featured_media":9758,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[209,579],"tags":[],"class_list":["post-9735","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-test-automation","category-guide"],"acf":[],"images":{"medium":"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/TestNG-Groups-300x157.jpg","large":"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2023\/09\/TestNG-Groups-1024x535.jpg"},"_links":{"self":[{"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/posts\/9735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/comments?post=9735"}],"version-history":[{"count":6,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/posts\/9735\/revisions"}],"predecessor-version":[{"id":14078,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/posts\/9735\/revisions\/14078"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/media\/9758"}],"wp:attachment":[{"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/media?parent=9735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/categories?post=9735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/tags?post=9735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}