{"id":2768,"date":"2023-11-24T14:31:00","date_gmt":"2023-11-24T14:31:00","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=2768"},"modified":"2023-11-19T16:31:40","modified_gmt":"2023-11-19T16:31:40","slug":"priority-attribute-in-testng","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2023\/11\/24\/priority-attribute-in-testng\/","title":{"rendered":"Priority attribute in TestNG\u00a0"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li>Default execution order of @Test methods<\/li>\n\n\n\n<li>Priority attribute usage<\/li>\n\n\n\n<li>Priority = 0<\/li>\n\n\n\n<li>Priority = -1 (negative priority)<\/li>\n\n\n\n<li>Same priority to all test methods<\/li>\n<\/ol>\n\n\n\n<p><strong>Default execution order of @Test methods&nbsp;<\/strong><\/p>\n\n\n\n<p>Ensure that you have TestNG installed.&nbsp;<\/p>\n\n\n\n<p>If TestNG is not installed, than perform following steps: Help &gt; Eclipse Marketplace\u2026&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/WSd75Bik1RI3_HQvVvoijBlykX5vYcpldHDiM2cAwkY7quEjVnzJ-NuGVMhGXs86nd5Q9KC89VzMz_sKjQOfjgQxqgY1njRY97LPEG3LVf7ubWdSklEZ7WY1UtA81AVPZjDqtxSB2qEXs4MEAoeM\" alt=\"\"\/><\/figure>\n\n\n\n<p>Enter Testng in search field as shown below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/r5tmV2ftjl1slA3qIJiMXBSu3wI9gqJuc6H0rI2aeMQEYP8iI36DAX5C72RarfpRERGvPMuwBM2fB4K9Qxr0umCwrCUaSgN8STe7lNd7DjzwlT_fWZtbe0SRJ7IQKG_66YfX7bjAyW5a7j1Vu-YQ\" alt=\"\"\/><\/figure>\n\n\n\n<p>Complete remaining steps to complete TestNG installation.<\/p>\n\n\n\n<p>Ensure testng dependency in pom.xml file<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/q_P-l8Wu90UDA4u4Svx0-QzKYC4PHNAEg96gbUu_YPTSVzoIrPzRotYe6ADgyrloc8G_ijg1NREtt4MoWj6GIgCbtcQyDwJDyJiu-KGOiq_TWkM_OFDS4UZ51IaYq50E7OToOgIDGyr5cyMlMorS\" alt=\"\"\/><\/figure>\n\n\n\n<p>Create a new class and add 5 @Test methods as shown below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/BJbW9LPfrEyWHgxpfbB3fYQXOG16JzY82XAKRUMv9sx2pHBxSCzhS_u89zt5ISxVvMSM4Ss4hT01EWNj26H1wYI3d7aHh2eVATgH5l3m49dPtLkHLbDoULN1LgPCymWqVoPLUspCfp1ubeTP8VDD\" alt=\"\"\/><\/figure>\n\n\n\n<p>Execute the Test<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/z2AN0OoWHNmYgdOi363Vwh-Re6CeqHnLR3y0vPoB1okG7a7dZamqvhUmEYY7qjfDcG7nGTcd29UfEyX3bfQGxZRajBw5BSCCR6P1sZ082xaARWIkT6_SCxe55gQxTOcoyLDQjN-D9EetxmGrnUhj\" alt=\"\"\/><\/figure>\n\n\n\n<p>Notice the below output<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/sJ9oMgOCJgVXRE-dS1TNE9V0JQP2omLat043bAxa-TM588_5mKhYl1is9LrERDQb3UL38iWOPk5vD3Mr4bcGnlDfmBNIzn5A2ROF_dYpMiZHNq2ElwDTSHVrin9dT2oWTbzvEZx3SjBLgwCcR-gO\" alt=\"\"\/><\/figure>\n\n\n\n<p>The @Test methods get executed in alphabetical order (random manner).&nbsp;<\/p>\n\n\n\n<p>So, by default, all the @Tests methods inside a class will be executed in alphabetical order.<\/p>\n\n\n\n<p><strong>Priority attribute usage<\/strong><\/p>\n\n\n\n<p>We can specify our own order of execution as well. This can be done using &#8216;priority&#8217; attribute in TestNG.<\/p>\n\n\n\n<p>So let us assign priority to all the @Test methods as shown below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/3oBtYOBTeYpPSD_tzEgZykKYScn7ILvW2LRESYa5NIOsflVGkbwkfFscRgu6A3z7uKz-06-Qmoom9CGzFfJ4HUqxw9nmqEZdHjHlCF2d-R24IO6vDLjD8kYvHrHQQSunO-96wxfM6ya_hVirIKzI\" alt=\"\"\/><\/figure>\n\n\n\n<p>Execute.&nbsp;<\/p>\n\n\n\n<p>The @Test methods execution happens in order<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/fG82e_MyeS67airRDVx-ohQ2E39fiKyfs-Qns3PGzH_CGLUAJglbdHV20lLYmJ1iCD14cSiXl39hCHwpGP8pz1hhOPhW2S99aUxqoPHt9zLKk2drPi6UZ24_nfjOXjXycHky9aGAKl9x4vbFrbZH\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Priority = 0<\/strong><\/p>\n\n\n\n<p>If we set priority=0 for a @Test method, the method will execute before the method having priority=1 (since 0 comes before 1).<\/p>\n\n\n\n<p>Let us set <em>priority=0<\/em> for testTwo() as shown below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/homAvVv3ya70fFrw4ibOYxwinOjBMwH3bffVozCAhS449Rg0gKKC8dXLSRKk2QBPMaQC1q6WK9OLAcxbU9zm9UesrtBodoP39_kEIopUEC38oZhI1sBFWV9UuehO1IH5iDeq052FPRuFf_ydlxpZ\" alt=\"\"\/><\/figure>\n\n\n\n<p>Execute the test.&nbsp;<\/p>\n\n\n\n<p>Notice below that \u201ctestTwo method\u201d is printed before \u201ctestOne method\u201d since former has priority=0<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/n13c6tw4dYo-G9fzeaHpDo0IOc3aK6bH53n2__b28AwtOpChYezXgQtYC6eC7wrTT8sRawmkWPdKsPwLM4fI07wgkeHyfausDuLwPjzAT4nYkiewJyI1BjCTz5ckwinxRi9cPV3Empx_Yz2Zm6cJ\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Priority = -1 (negative priority)<\/strong><\/p>\n\n\n\n<p>When we set priority=-1 for any @Test method, that method will be executed before the method that has priority=0 (since -1 comes before 0).<\/p>\n\n\n\n<p>Let us set <em>priority=-1<\/em> for testThree() as shown below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/VoNceqBRsqTh_4CgUaUG10ZRnWpvhVXOGlJDn2W9e58xw3n2GDLn0QTElrr62kPV2Y2TXLuek0v7UH8BZa0lwZYg-Og8ogkxkm6M5ovMcs2pqmX3ubCW845_XcKrVwvjWHlvu-NB2dSzRbmTmeQK\" alt=\"\"\/><\/figure>\n\n\n\n<p>Execute.<\/p>\n\n\n\n<p>Notice below that \u201ctestThree method\u201d is printed before \u201ctestTwo method\u201d since former has priority=-1<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/k4fOebJnQnR1I1MZEy-QTG6a-efvaiJmk060m_GxQCPqHWwvBgFASGtfRQC836D60dkZ9D8mWD8IdEYhGzhyRH-JN2ugk2TzSG29eq27jOPvIcMF-Ahq-HuhtDWIlRFhwpX7ovbbsL-k8t6Q6J4x\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Same priority to all test methods<\/strong><\/p>\n\n\n\n<p>Let us give same priority to all the @Test methods<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/p-dGFflpBromFGswgCkHPvvD_yFHvVeIUujB_MT0yVhxgOw2QaSm48L2e3IHQAhb2WUMruP9iVYN2X2JHerJHDlqAQxsBaEsfWXCT-UibuFa5sNbwQWxS0O8tSVrQQUr1zq7XdlNFfw8xJoOqg0g\" alt=\"\"\/><\/figure>\n\n\n\n<p>Setting same priority to all the @Test methods is same as not setting any priority to and @Test.<\/p>\n\n\n\n<p>So when we execute, the result will be same like we saw for default execution order of @Test methods, see below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/CF5kxORo7Csg2mraeHNr1iaretplaiSQydachR02G4uVn4oET2Im5-vuvfHH7RsKcJVPFmijlhs2osEZBsV617vULiLIQfxwImFQgCBohzwzxBJbCV79nnB_8USEqMp6iqTe9XsEMr2gNPKuJtTO\" alt=\"\"\/><\/figure>\n\n\n\n<p>So this is how priority attribute can be used in TestNG.<\/p>\n\n\n\n<p>The source code snapshot:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/vM9mWqGufoWw2BWsoWBoDkrcMu8nbWILcUQuDkqVtc0eQZxN2epkH-VLj0i84dMlPbaoH_5pHykcZ-9IgOZH9ER6rUBQqCsfY2QxPe4HXkpcttwQlSJDLApvm735zxKeWXLfWMYyCCrSiVMTL1sV\" alt=\"\"\/><\/figure>\n\n\n\n<p>Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Default execution order of @Test methods&nbsp; Ensure that you have TestNG installed.&nbsp; If TestNG is not installed, than perform following steps: Help &gt; Eclipse Marketplace\u2026&nbsp; Enter Testng in search field as shown below Complete remaining steps to complete TestNG installation. Ensure testng dependency in pom.xml file Create a new class and add 5 @Test methods [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1238,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-2768","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\/2768","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=2768"}],"version-history":[{"count":7,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2768\/revisions"}],"predecessor-version":[{"id":2797,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2768\/revisions\/2797"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1238"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=2768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}