{"id":2734,"date":"2023-11-22T21:20:00","date_gmt":"2023-11-22T21:20:00","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=2734"},"modified":"2023-11-19T16:27:40","modified_gmt":"2023-11-19T16:27:40","slug":"testng-annotations","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2023\/11\/22\/testng-annotations\/","title":{"rendered":"TestNG Annotations\u00a0"},"content":{"rendered":"\n<p>1. TestNg annotation: @BeforeTest<br>2. TestNg annotation: @AfterTest<br>3. TestNg annotation: @BeforeSuite<br>4. TestNg annotation: @AfterSuite<br>5. TestNg annotation: @BeforeMethod<br>6. TestNg annotation: @AfterMethod<br><strong><br><\/strong><strong>TestNg annotation: @BeforeTest<\/strong><\/p>\n\n\n\n<p>Let\u2019s say, before you run any test case, there is a pre-requisite to clean the data that you have created in the database. This can be achieved using <strong>@BeforeTest<\/strong>.<\/p>\n\n\n\n<p>Thus, <em>@BeforeTest<\/em> annotation would be given first priority (no matter in which class it is written).&nbsp;<\/p>\n\n\n\n<p>See below the usage of <em>@BeforeTest<\/em> annotation&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/Kv5aCf1QNryO-Y5apo-HR0hGPPzaax7_9DwQ7izbTK8e4xfWs2u_2ZbdBYKV9kut8psYmDPTkndQnqsjXgqZQczkC0R2ZWy2JTs6HrOhRI2ufDZXvpPU11zf6_AM47PmuzEZVt591HzZmiGxmd9N\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let us run this as \u2018TestNG Test\u2019<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/hvqDjGA0QwIWqG9PCu5pJwD78AwUjsH1LCyPWoUpkNVYJpGfFQh5WSrVY2sPA39AZEanmwDMMR3AY6tIdVmoc-QF-N-zfvDacYR6pv7_NNEvBPQgU4OIFrBU0R-vncsk-LYoKEoc7z98ZMrmSHGk\" alt=\"\"\/><\/figure>\n\n\n\n<p>Notice that <em>@BeforeTest<\/em> gets executed first<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/EMGu0FzjsGjSFrlwhtNOmYqotNdaE-c7dB_BFFi3dbd9AdfeXJUdm-LwyJnTu3ny_jgKhBABnG5Zw75HnhUuh3epEn52PLJXIn7SYRykcGn6KOL4rxxgWDTjKo9mWyhrQD8F9NhBjQ8ben1F0Rgg\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>TestNg annotation: @AfterTest<\/strong><br>Now let us change @BeforeTest to <em>@AfterTest<\/em>, see below. Let us keep the print message same<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/6uquwnKQaiMKLwgBjRAU7TyOJ656-eHSm6-nQjFCrI2jKXLh958T3LNn1M1Wm8D8doQEajtZ0h6X11Mmb2LVMVRJpeSnCon6FiZY5clbUwFvS7EtlFGTCw2tjLv2Yn71w3nGlwwWJRjRiOTfr8H1\" width=\"640\" height=\"370\"><br><br>Run, notice that the sop message for <em>@AfterTest<\/em> gets printed after all the tests are executed<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/lurInCJQlFo4oH8cgZhChfIHli2BhTXJDGno_iIAxnJwr-myoQz9hVw_sZMu7dGMR7SWkJVEfo-Trv2P58mFZXdN_-EDAEgcihNzbBpaDtTTRe_KHsYHJEXsMu0yagzJPQ8Cov09hzg4ghzpRlYu\" width=\"403\" height=\"242\"><br><\/p>\n\n\n\n<p>Thus, <em>@AfterTest<\/em> can be used to delete cookies, close browsers, generate reports, close db connections etc.<\/p>\n\n\n\n<p><strong>TestNg annotation: @BeforeSuite<\/strong><\/p>\n\n\n\n<p><em>@BeforeSuite<\/em> can be used if you want to set some global environment variables to your framework. For example, for every release, we will have some new url (development, test or staging etc). You can set these urls as part of <em>@BeforeSuite<\/em>, so that all the tests get executed with that url.<\/p>\n\n\n\n<p>Let us add this annotation to the last test of below class<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/M-FyzE_-3eoMP0i0kW-avIvjtFRTMTEVcI8a9b_DoAAA2fFvISnil7xmDD3vQ-_R7EYUGA6ie73r4WCly67vvJt8vpHWErefgRJNIj4XT6AVMD0SQ_bSGW6l5IQPQ7ajX70OVTCvRWNvkXKsiPAr\" alt=\"\"\/><\/figure>\n\n\n\n<p>Run as suite<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/jOW-jOYIp5v3gKOyMkJPC4i7Je8mRM42hOmoJNsPh-EUPCguaNeGh7TgGqmGsc02upxQnvD6fMKzRwJBjYnAStrnVD-SYZ7gAno5qcaoQo8U5_2bDOTHCaXxhGGOUWTiYR5SROil0ZU7LmJ5ljas\" alt=\"\"\/><\/figure>\n\n\n\n<p>See below. \u201cBefore suite\u2026\u201d sop is printed at the top<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/-MSkiwtsvHckY6FPFsMprHIin0M9Fy6FUNAefMIoncY_i99KeMSwuYD97FxRJ7EcSS1CNMdy27vzCan_4peDCwsOwdP8nRjZEkSjI1P5A6WWBKXg_U6QjIj1TFoE0HHbsS-7xcWxztTFPYuOvqLE\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>TestNg annotation: @AfterSuite<\/strong><\/p>\n\n\n\n<p>Let us add <em>@AfterSuite<\/em> annotation before the first test of another class viz TestNGDemo.java, see below<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/by9AiW0Guq8c7rX-MA8NDj7u0iZc2VYzsmXzF-ty6YbCCHxEdfKWl4ZeK_nVX_br0WUQtbuwnm0lIQmSkfWm9Ic4W6Q_wfZxH8yATLalqMrF9o9pzdHIMNfZXH8lwH_TbjAwVKEuFlzM5eWkIs2t\" width=\"646\" height=\"253\"><br><\/p>\n\n\n\n<p>Run the testng.xml, see below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/jOW-jOYIp5v3gKOyMkJPC4i7Je8mRM42hOmoJNsPh-EUPCguaNeGh7TgGqmGsc02upxQnvD6fMKzRwJBjYnAStrnVD-SYZ7gAno5qcaoQo8U5_2bDOTHCaXxhGGOUWTiYR5SROil0ZU7LmJ5ljas\" alt=\"\"\/><\/figure>\n\n\n\n<p>Notice \u2018hello\u2019 getting printed at the last, since <em>@AfterSuite<\/em> gets executed at the end<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/ZCCVoFEuMmt7EQTs0PzsLGWqJDMtgP53ft2yJSKgl-hK1687DrC-ga1t_47m_igaXlPAphUmh4AC5WcuIorJJENOittJVsKBIal0yaLqNKwhk-7DKc83gXHmIdHoZhXTbIROUYmmL7QqtWqDF2z6\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>TestNg annotation: @BeforeMethod<\/strong><strong><br><\/strong>You can use <em>@BeforeMethod<\/em> to clear cache, cookies etc before executing a method.&nbsp;<\/p>\n\n\n\n<p>Now we know each @Test is a method. So if there are 4 @Test (methods) in a class, than @BeforeMethod will be executed 4 times before each @Test.<\/p>\n\n\n\n<p>We see that there are 3 tests in the below class.&nbsp;<\/p>\n\n\n\n<p>Let us add <em>@BeforeMethod<\/em>&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/u0jvrFCGdSSjEc2Nv07DD5hN_FHy6b8h0N27lN6my924QWT9FmfQHZ5TVRbmJK_0VTyw3WmZntLt5OpUKSz_3MauLmGhD4UPht7FQ3SIx6sqkxvOg9YLgTI9axwB804N_CdKs3K3x9AOTVNqPtCm\" alt=\"\"\/><\/figure>\n\n\n\n<p>Run as \u2018TestNG Test\u2019<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/CkgyEDfhZvnD3Vg2ShtWko7EUWet3PYHO_Y2kOpZ35RGG0a2GS8f05IDBFVyNShVZEB7EQMOr-Nn7XOKC8R5xemEFIQLc9r40rPwrrPAQhDt3F8scPUr1rD8voQhG8VOujs17JfX01WJj-BSQGrZ\" alt=\"\"\/><\/figure>\n\n\n\n<p>Notice below that <em>@BeforeMethod<\/em> gets executed 3 times viz before each of the 3 tests<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/HF3DPechySOYePjI2wBa7dgE5fqm-f4CfSkv1WNI6oceHD72UW19Stke7S2Ww2GNpzH3R3wLZzjsEl1GU5KNm3IVFSVgBZDENhr0F6Gji89_M_-PozsUx_uQuCBg1FpOADkYRkkGkKgO7viV6jTZ\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>TestNg annotation: @AfterMethod<\/strong>&nbsp;<\/p>\n\n\n\n<p>This is just reverse.&nbsp;<\/p>\n\n\n\n<p>Let us change @BeforeMethod to <em>@AfterMethod<\/em>, change print statement (line 28)&nbsp;&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/Urgr7w-H7D4eqLYH4UeJ7j16R6eWFpLEz6LSEhzOhc6QUpfHLdTbi4u-n3JdGCyjZrwU_dC-5j-JSDyfEw6o5_nSBP91qrfzcinahLMGHIdDmujsoMidbkTuqT9cuXFaePbjQK6BY-q6GuEmWZ_x\" alt=\"\"\/><\/figure>\n\n\n\n<p>Run test, see that <em>@AfterMethod<\/em> gets executed 3 times, but this time, after each @Test<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/ufedHVSfmv2hPy1qn0E8MZbhyq0TPBiKbPWM4TzTPPxAKLB8iNC42UsON7aD4UWRhGGAe4Lqnd9Tv5T4AKFnSlOeUyTxaYOfKWrLpTOf-11EZH8iujVayDpAcY3aQ7KdWSE10MLlp71PZ2pB_H6K\" alt=\"\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>1. TestNg annotation: @BeforeTest2. TestNg annotation: @AfterTest3. TestNg annotation: @BeforeSuite4. TestNg annotation: @AfterSuite5. TestNg annotation: @BeforeMethod6. TestNg annotation: @AfterMethodTestNg annotation: @BeforeTest Let\u2019s say, before you run any test case, there is a pre-requisite to clean the data that you have created in the database. This can be achieved using @BeforeTest. Thus, @BeforeTest annotation would be [&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-2734","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\/2734","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=2734"}],"version-history":[{"count":9,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2734\/revisions"}],"predecessor-version":[{"id":2795,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2734\/revisions\/2795"}],"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=2734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}