{"id":2775,"date":"2023-11-25T11:34:00","date_gmt":"2023-11-25T11:34:00","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=2775"},"modified":"2023-11-19T16:34:41","modified_gmt":"2023-11-19T16:34:41","slug":"groups-attribute-in-testng","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2023\/11\/25\/groups-attribute-in-testng\/","title":{"rendered":"Groups attribute in TestNG\u00a0"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li>Include groups at test tag level<\/li>\n\n\n\n<li>Exclude groups at test tag level<\/li>\n<\/ol>\n\n\n\n<p><strong>Include groups at test tag level<\/strong><\/p>\n\n\n\n<p>Let us now see how to group @Tests using groups attribute in TestNG.<\/p>\n\n\n\n<p>Let us create \u2018LoginUser\u2019 class under below package<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/EJVe_SXQgPBf9L-iUL-V3f1_FoKUeL4ufGmaYuLjz2zNEbPes9LJBgW3pYmlRhhcRI8UslfWTFH5lSxt7QYBdr9par55tj1s1MbPo95DKihpphkIuRsEWuqNg1S9EL_MK9zoVM2U4penyZfIA6Df\" alt=\"\"\/><\/figure>\n\n\n\n<p>Add 5 @Test methods<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/pnR4cqDBHAWsURbCD95b12ieUldZSrxT1R5zRU34XOKBg1I93Km_Kta5ewtUpLdiIzw8cxm3vm5toMGHKGUl7GwnT3lQ9fJ1q4V5CIQeic5q5j89s-sVQurNwIlvfMPPaLRPdop9okCMvwsbGneO\" alt=\"\"\/><\/figure>\n\n\n\n<p>Add <em>groups={&#8220;smoke&#8221;}<\/em> for first and third @Test, see below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/MHDwnqyE0A4eDgmHPRJcvp__IhaBTPBaaJcAT4af5EQLY6Ld3kppRabyEwgskeZFsYxdF4aX4MCdge3ajOSg3-rIvMk8hHUZcrOT5UA4awznWrOfN13A1CTJL8eX15J8MQCLPaPBu2E9UWn6_dRC\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let all 5 @Test methods fall into sanity group<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/D1UTs3ggGTJtiHcsrr2wGpn5pC9O_75h3TLI2elojjSHHmS72-eqtJeYNR-bDhpTMsD6NJLPeXj6alOqoXnjR_bRNG1CQb0X8UC3Q6WwV0qhBpnuLap907nSwYkafvaZMEU0Hpb6y6TxYLPKmKi9\" alt=\"\"\/><\/figure>\n\n\n\n<p>Right click src\/main\/java &gt; TestNG &gt; Convert to TestNG<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/xIM6ybsNUMJYghMIsVKUmCyaNNzkVogjk9P6CyLtq1CikJi2gs82xcGYPGJUkvP5juHULPj9R9RuJbY4xPUE7EC_ekgz6ZOIjSN6D0XVVaQVt6wj4RSVXDANhlgKhJHaB4LYNblj6qimFyrHDSKe\" alt=\"\"\/><\/figure>\n\n\n\n<p>The xml gets created<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/ZMcNdyyP6fMVhwOP9U8H3yH37QhR097f2b-QoiROs_TNlEM6t-JJQHvm6WD79c7mXcHNbi1LIu1drQWmU6pIf8H9rEzEksXYdoGX0C1wDa0wqzdi7NmMbaPzccaUcH1-s81khMMTdToS5fiAXCj4\" alt=\"\"\/><\/figure>\n\n\n\n<p>Change the name to &#8220;LoginTest&#8221; as shown below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/7ehEas3H8E5Yv0dNx1hrejgeSLMAAvS_BgTPtToCk6gXYU3K8yBTN4D_vxW_JhP3hxzUl8TffSALfeOhBOSnB58OPYK7kgcvkoJFe3qGPn7tcRsSeh_peE7hmBGl6TMLghYHZ9GKCtA0H-UGTP49\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let us now add groups tag at test level, see below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/YdDYXFJleetylTmTfM6foqR7zNduGiKfFuTlveSCZo3pI05AgZPM4d7i5Xkm4luxiX1wtrve0aur__oNQ88f8OS1h4z30W_SntnCOJB6PUrW_-93hra7IgC51el6Mz0JjXfJ8yZnsgWFu4Z9DBMt\" alt=\"\"\/><\/figure>\n\n\n\n<p>Next, we will open &lt;run&gt; tag within &lt;groups&gt; tag<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/4itDmpOdTa_ipMKo4PEo7NF9hspE26rlTeAJWuxhxr2V3oYz3JrUU87FCVkPa-a1Xnr8pUL5vDAQlXWk7I_USTBCLrDoc6XECXC318cERatpDjMGjmHsyFVOJhp28D65zCboAlPOwgJVtxgxxFfo\" alt=\"\"\/><\/figure>\n\n\n\n<p>Next, we will open <em>&lt;include \/&gt;<\/em><em> <\/em>tag within &lt;run&gt; tag, see below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/njT8NsogO3U-nqWtjxnqjP_41vh-KqyN1aLf7zSQTjw8D8-Xig3Z9amsAUvXQ64GQy5fUhV7pWm0RIkCyyxANw0pG6vlukBIeYi8L41IGDUMW-kWs-Kx9gBSf5uDU0u5LDUwy8ADQTLeH8_PhlzJ\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now, some of the @Tests are <em>smoke<\/em><em> <\/em>tests and some of them are <em>sanity<\/em> tests&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/rwNNB0O7_tiZ0VfLeHzuW9LmaVrtpmFe4hjP6r-CvN85JuFVFsmxLhz5LhQ3NOXul2alF38x-Qr3kaTuEBpyL0t67uBfwcBg6E7qOexQN3QcXAnecEzZt9r5utF9P__dUl8F3pcypRk9uXCjpW0w\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let\u2019s say we want to execute only smoke tests.&nbsp;<\/p>\n\n\n\n<p>To do that, we can simply mention <em>name=\u201dsmoke\u201d<\/em> as shown below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/PAbEWCjapfilmpSOSvYwZsfHgwawQ71qdZLsIcRcAOrbaTlhfQXuW26suFh740mU_hVRb_Hcya9os50zKuo2ydV4qoBu6yCGn9xiQSK-YqW4khIYHOv8HavKvsGFUHyboPrUIH6Ith0_uqE3nb1w\" alt=\"\"\/><\/figure>\n\n\n\n<p>Save the xml.<\/p>\n\n\n\n<p>Right click xml &gt; Run as &gt; TestNG Suite<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/eoaXlgxLbdIIuzqCmQ4J38IYJncSEYtrgpluqFxQiSSI_f08QvijUhjexElVQBRJOPD6D3OH2IxwPOlM-DKRBdKPWTaaihddllWBMQWfDOUFG8CAh4BP6JbOiO4iMoZDVBO3mkA8pv9HuIAiOKNI\" alt=\"\"\/><\/figure>\n\n\n\n<p>Notice below that only 2 @Tests are executed. These 2 tests are \u2018smoke\u2019 tests&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/sz3d11cH-9V4bQsOhfQgFf50MTyWMT_2wUF2AgQPPFNCc_10ZawPwod8TZ9odu1ouJrgIbuEY5vjH9nohc6OC62ne7BRVqeVIlXTpGPe1QRMEqJdzutyW-9w6kyUzvCXGt0lIRDjN1n9w43Vucz8\" alt=\"\"\/><\/figure>\n\n\n\n<p>This is how we use the <em>&lt;include&gt;<\/em> tag inside the xml.<\/p>\n\n\n\n<p><strong>Exclude groups at test tag level<\/strong><\/p>\n\n\n\n<p>We can also exclude the groups. So below will exclude all the @Tests that have <em>groups=\u201dsanity\u201d<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/mNFf3W7T5vzf9UqixFE-TQS8gFmPiYB5-KtY3g9rhUnwk8i5Rc1lDrk-_5DOSuypaetbpHZLmHNI8EzGds6Ph-hTFL_Z69APC9UwvLu5gU29x7iTptdS_3LL0aMuR17_2ipUmmI3bAIw0-Gbt5KL\" alt=\"\"\/><\/figure>\n\n\n\n<p>Save xml and run<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/n7W82hVUPtZ_Xm-w8Ela75-bBEfLKOw8LUXC10bWPXuNHGlpaiEMXGuS_byHSoefVmcGldXJR0nOqB2MslT_Af29BY_2ASVZFYzT5JaiEZuDdpp-zO1GZ0So5lopULjfDI3_iv6FDvjub_GKAZ-P\" alt=\"\"\/><\/figure>\n\n\n\n<p>Notice above, none of the test got executed. The reason being, all the @Tests are sanity tests and hence all of them got excluded<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/D1UTs3ggGTJtiHcsrr2wGpn5pC9O_75h3TLI2elojjSHHmS72-eqtJeYNR-bDhpTMsD6NJLPeXj6alOqoXnjR_bRNG1CQb0X8UC3Q6WwV0qhBpnuLap907nSwYkafvaZMEU0Hpb6y6TxYLPKmKi9\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let us remove \u201csanity\u201d from first @Test<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/9z_7erz6Y3_uChvvZ1lIGnAurokLUb1ciZ2uxDCOmmEFfgmQH4k4IQ-_yhsvYranWXTbtwfZsPdhcXd0VBorgRbJ_eHIFXMXC_Cfq1lP3RHElwbj3RXaBI6zrvXIdsGbABDBR03ulNZl1Odrv5VQ\" alt=\"\"\/><\/figure>\n\n\n\n<p>Save the class.<\/p>\n\n\n\n<p>Now let us run the xml again. Notice below, this time the first @Test gets executed, since this test does not have \u201csanity\u201d group. All the \u201csanity\u201d tests got excluded viz they did not run<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/dCAqpK2Rq8KY6DfkYbsrRQfYy1rdA-44S0s9PnYSToORqHvlM1RGx-pWDcb7Q2rLiD3Z74xPygKygGfEILGR_jFeyvW_fBfnvBPdR6oaClA9q2wXvvG314ZgsjB0Cb1TA7cEelJcM7aXOTczXXTG\" alt=\"\"\/><\/figure>\n\n\n\n<p>This is how we use the <em>&lt;exclude&gt;<\/em> tag inside the xml.<\/p>\n\n\n\n<p>Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Include groups at test tag level Let us now see how to group @Tests using groups attribute in TestNG. Let us create \u2018LoginUser\u2019 class under below package Add 5 @Test methods Add groups={&#8220;smoke&#8221;} for first and third @Test, see below Let all 5 @Test methods fall into sanity group Right click src\/main\/java &gt; TestNG &gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1245,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-2775","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=2775"}],"version-history":[{"count":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2775\/revisions"}],"predecessor-version":[{"id":2780,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2775\/revisions\/2780"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1245"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=2775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}