{"id":2734,"date":"2023-11-22T21:20:00","date_gmt":"2023-11-22T21:20:00","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2023\/11\/22\/testng-annotations\/"},"modified":"2023-11-22T21:20:00","modified_gmt":"2023-11-22T21:20:00","slug":"testng-annotations","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/2023\/11\/22\/testng-annotations\/","title":{"rendered":"TestNG Annotations"},"content":{"rendered":"<p><html><body><\/p>\n<section class=\"tcb-post-content tcb-shortcode thrv_wrapper\" data-css=\"tve-u-16ec5d248bb\">\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<div class=\"code-block code-block-2\" style=\"margin: 8px auto; text-align: center; display: block; clear: both;\">\n<p><!-- horizontal responsive 2 --><br \/>\n<ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-3231318496783163\" data-ad-format=\"auto\" data-ad-slot=\"2788468763\" data-full-width-responsive=\"true\" style=\"display:block\"><\/ins>\n<\/div>\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<p>Thus, <em>@BeforeTest<\/em> annotation would be given first priority (no matter in which class it is written).\u00a0<\/p>\n<p>See below the usage of <em>@BeforeTest<\/em> annotation\u00a0<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/Kv5aCf1QNryO-Y5apo-HR0hGPPzaax7_9DwQ7izbTK8e4xfWs2u_2ZbdBYKV9kut8psYmDPTkndQnqsjXgqZQczkC0R2ZWy2JTs6HrOhRI2ufDZXvpPU11zf6_AM47PmuzEZVt591HzZmiGxmd9N\"\/><\/figure>\n<p>Let us run this as \u2018TestNG Test\u2019<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/hvqDjGA0QwIWqG9PCu5pJwD78AwUjsH1LCyPWoUpkNVYJpGfFQh5WSrVY2sPA39AZEanmwDMMR3AY6tIdVmoc-QF-N-zfvDacYR6pv7_NNEvBPQgU4OIFrBU0R-vncsk-LYoKEoc7z98ZMrmSHGk\"\/><\/figure>\n<p>Notice that <em>@BeforeTest<\/em> gets executed first<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/EMGu0FzjsGjSFrlwhtNOmYqotNdaE-c7dB_BFFi3dbd9AdfeXJUdm-LwyJnTu3ny_jgKhBABnG5Zw75HnhUuh3epEn52PLJXIn7SYRykcGn6KOL4rxxgWDTjKo9mWyhrQD8F9NhBjQ8ben1F0Rgg\"\/><\/figure>\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 decoding=\"async\" height=\"370\" loading=\"lazy\" src=\"https:\/\/lh7-us.googleusercontent.com\/6uquwnKQaiMKLwgBjRAU7TyOJ656-eHSm6-nQjFCrI2jKXLh958T3LNn1M1Wm8D8doQEajtZ0h6X11Mmb2LVMVRJpeSnCon6FiZY5clbUwFvS7EtlFGTCw2tjLv2Yn71w3nGlwwWJRjRiOTfr8H1\" width=\"640\"\/><\/p>\n<p>Run, notice that the sop message for <em>@AfterTest<\/em> gets printed after all the tests are executed<br \/><img decoding=\"async\" height=\"242\" loading=\"lazy\" src=\"https:\/\/lh7-us.googleusercontent.com\/lurInCJQlFo4oH8cgZhChfIHli2BhTXJDGno_iIAxnJwr-myoQz9hVw_sZMu7dGMR7SWkJVEfo-Trv2P58mFZXdN_-EDAEgcihNzbBpaDtTTRe_KHsYHJEXsMu0yagzJPQ8Cov09hzg4ghzpRlYu\" width=\"403\"\/><\/p>\n<p>Thus, <em>@AfterTest<\/em> can be used to delete cookies, close browsers, generate reports, close db connections etc.<\/p>\n<p><strong>TestNg annotation: @BeforeSuite<\/strong><\/p>\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<p>Let us add this annotation to the last test of below class<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/M-FyzE_-3eoMP0i0kW-avIvjtFRTMTEVcI8a9b_DoAAA2fFvISnil7xmDD3vQ-_R7EYUGA6ie73r4WCly67vvJt8vpHWErefgRJNIj4XT6AVMD0SQ_bSGW6l5IQPQ7ajX70OVTCvRWNvkXKsiPAr\"\/><\/figure>\n<p>Run as suite<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/jOW-jOYIp5v3gKOyMkJPC4i7Je8mRM42hOmoJNsPh-EUPCguaNeGh7TgGqmGsc02upxQnvD6fMKzRwJBjYnAStrnVD-SYZ7gAno5qcaoQo8U5_2bDOTHCaXxhGGOUWTiYR5SROil0ZU7LmJ5ljas\"\/><\/figure>\n<p>See below. \u201cBefore suite\u2026\u201d sop is printed at the top<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/-MSkiwtsvHckY6FPFsMprHIin0M9Fy6FUNAefMIoncY_i99KeMSwuYD97FxRJ7EcSS1CNMdy27vzCan_4peDCwsOwdP8nRjZEkSjI1P5A6WWBKXg_U6QjIj1TFoE0HHbsS-7xcWxztTFPYuOvqLE\"\/><\/figure>\n<p><strong>TestNg annotation: @AfterSuite<\/strong><\/p>\n<p>Let us add <em>@AfterSuite<\/em> annotation before the first test of another class viz TestNGDemo.java, see below<br \/><img decoding=\"async\" height=\"253\" loading=\"lazy\" src=\"https:\/\/lh7-us.googleusercontent.com\/by9AiW0Guq8c7rX-MA8NDj7u0iZc2VYzsmXzF-ty6YbCCHxEdfKWl4ZeK_nVX_br0WUQtbuwnm0lIQmSkfWm9Ic4W6Q_wfZxH8yATLalqMrF9o9pzdHIMNfZXH8lwH_TbjAwVKEuFlzM5eWkIs2t\" width=\"646\"\/><\/p>\n<p>Run the testng.xml, see below<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/jOW-jOYIp5v3gKOyMkJPC4i7Je8mRM42hOmoJNsPh-EUPCguaNeGh7TgGqmGsc02upxQnvD6fMKzRwJBjYnAStrnVD-SYZ7gAno5qcaoQo8U5_2bDOTHCaXxhGGOUWTiYR5SROil0ZU7LmJ5ljas\"\/><\/figure>\n<p>Notice \u2018hello\u2019 getting printed at the last, since <em>@AfterSuite<\/em> gets executed at the end<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/ZCCVoFEuMmt7EQTs0PzsLGWqJDMtgP53ft2yJSKgl-hK1687DrC-ga1t_47m_igaXlPAphUmh4AC5WcuIorJJENOittJVsKBIal0yaLqNKwhk-7DKc83gXHmIdHoZhXTbIROUYmmL7QqtWqDF2z6\"\/><\/figure>\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.\u00a0<\/p>\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<p>We see that there are 3 tests in the below class.\u00a0<\/p>\n<p>Let us add <em>@BeforeMethod<\/em>\u00a0<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/u0jvrFCGdSSjEc2Nv07DD5hN_FHy6b8h0N27lN6my924QWT9FmfQHZ5TVRbmJK_0VTyw3WmZntLt5OpUKSz_3MauLmGhD4UPht7FQ3SIx6sqkxvOg9YLgTI9axwB804N_CdKs3K3x9AOTVNqPtCm\"\/><\/figure>\n<p>Run as \u2018TestNG Test\u2019<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/CkgyEDfhZvnD3Vg2ShtWko7EUWet3PYHO_Y2kOpZ35RGG0a2GS8f05IDBFVyNShVZEB7EQMOr-Nn7XOKC8R5xemEFIQLc9r40rPwrrPAQhDt3F8scPUr1rD8voQhG8VOujs17JfX01WJj-BSQGrZ\"\/><\/figure>\n<p>Notice below that <em>@BeforeMethod<\/em> gets executed 3 times viz before each of the 3 tests<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/HF3DPechySOYePjI2wBa7dgE5fqm-f4CfSkv1WNI6oceHD72UW19Stke7S2Ww2GNpzH3R3wLZzjsEl1GU5KNm3IVFSVgBZDENhr0F6Gji89_M_-PozsUx_uQuCBg1FpOADkYRkkGkKgO7viV6jTZ\"\/><\/figure>\n<p><strong>TestNg annotation: @AfterMethod<\/strong>\u00a0<\/p>\n<p>This is just reverse.\u00a0<\/p>\n<p>Let us change @BeforeMethod to <em>@AfterMethod<\/em>, change print statement (line 28)\u00a0\u00a0<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/Urgr7w-H7D4eqLYH4UeJ7j16R6eWFpLEz6LSEhzOhc6QUpfHLdTbi4u-n3JdGCyjZrwU_dC-5j-JSDyfEw6o5_nSBP91qrfzcinahLMGHIdDmujsoMidbkTuqT9cuXFaePbjQK6BY-q6GuEmWZ_x\"\/><\/figure>\n<p>Run test, see that <em>@AfterMethod<\/em> gets executed 3 times, but this time, after each @Test<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/ufedHVSfmv2hPy1qn0E8MZbhyq0TPBiKbPWM4TzTPPxAKLB8iNC42UsON7aD4UWRhGGAe4Lqnd9Tv5T4AKFnSlOeUyTxaYOfKWrLpTOf-11EZH8iujVayDpAcY3aQ7KdWSE10MLlp71PZ2pB_H6K\"\/><\/figure>\n<\/section>\n<p><\/body><\/html><\/p>\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":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2734","post","type-post","status-publish","format-standard","hentry","category-testng","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=2734"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2734\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=2734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=2734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=2734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}