{"id":3698,"date":"2020-06-11T19:34:46","date_gmt":"2020-06-11T14:04:46","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3698"},"modified":"2024-11-04T16:37:47","modified_gmt":"2024-11-04T11:07:47","slug":"testng-test-priority-in-selenium","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/testng-test-priority-in-selenium\/","title":{"rendered":"TestNG @Test Priority in Selenium"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>What is Test Priority?<\/strong><\/h2>\n\n\n\n<p>In TestNG, \u201cPrioritization\u201d means assigning the Execution Priority on Test Case. For example, we want to execute the test cases in order when there are multiple test cases, i.e., first we need to execute the \u201cRegistration\u201d test case before login. The priority on the test case will define the Execution order of Test Case. It can be defined at the Method Level. To assign the priority to a test case, you need to define the test priority at the method level within the @Test annotation. Learning these techniques is an essential part of any <a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\" data-type=\"link\" data-id=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium Course<\/a>, where you can gain practical knowledge on test case prioritization and execution flow.<\/p>\n\n\n\n<p>In order to set the priority, we need to add annotation as @Test (priority=?). The default value for priority will be zero. If you don\u2019t mention the priority, it will take as \u201cpriority=0\u201d for all the test cases and execute. Lower priorities will be executed first. If we define priority as \u201cpriority=\u201d for multiple test cases, then these test cases will get executed only after all the test cases which don\u2019t have any priority and set priority to 0 as the default priority.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\/\/Valid Syntax<\/strong>\n<strong>@Test<\/strong>(priority = 1)\n<strong>\/\/Invalid Syntax<\/strong>\n<strong>@Test<\/strong>(PRIORITY = 1)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TestNG Test Cases without Priority<\/strong><\/h2>\n\n\n\n<p>Let\u2019s consider below <a href=\"https:\/\/www.h2kinfosys.com\/blog\/parallel-test-execution-in-selenium-using-testng\/\">TestNG program <\/a>where we will not define any priority with the test method.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> Launch the Eclipse<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> testNGPriority;\n\n<strong>import<\/strong> org.testng.annotations.Test;\n\n<strong>public<\/strong> <strong>class<\/strong> NoPriority_Example {\n@Test&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> one()&nbsp;\n{&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"First Test Case\");&nbsp;\n}&nbsp;\n@Test&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> two()\n{&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"Second Test Case\");&nbsp;\n}&nbsp;\n@Test&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> three()\n{&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"Third Test Case\");&nbsp;\n}&nbsp;\n}<\/code><\/pre>\n\n\n\n<p><strong>Step 2:<\/strong> Right click on Java class &gt; <strong>Run As<\/strong>&nbsp;and then click on the&nbsp;<strong>TestNG Test<\/strong>.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p><img fetchpriority=\"high\" decoding=\"async\" width=\"602\" height=\"201\" alt=\"NoPriority.PNG\" src=\"https:\/\/lh6.googleusercontent.com\/Pk5cUSBr7Cghx-on9Lq4rah0OOiOT79oNEeC1tgl4BeKnXnxZziN5xDMQJdosH5mSMC6WjXr-U7KjyDtZ32TRRvmHNe87CUnAZ-p52gBFEGNEPmFwF_orHeOQvNM0AJAQeLxi7wirwUlYHPUzg\" title=\"\"><\/p>\n\n\n\n<p>In the above program, we declared a class with three test methods: <code>one()<\/code>, <code>two()<\/code>, and <code>three()<\/code>, each without any assigned priority. When running the tests, we observed that TestNG executed these methods in alphabetical order, following the method names rather than the sequence in which they were implemented in the code. <\/p>\n\n\n\n<p>This default behavior ensures a predictable, consistent order for execution, even when no specific priority is defined.<\/p>\n\n\n\n<p>However, to control the execution order according to our specific requirements perhaps starting with essential setup methods or prioritizing critical test cases. we can use the <code>@Test<\/code> annotation to assign priorities explicitly. <\/p>\n\n\n\n<p>By setting priority values with the <code>@Test<\/code> annotation, we can customize the order to better suit our testing goals, ensuring that key test methods are executed in the desired sequence. This approach provides greater flexibility and control, especially in complex test scenarios where method dependencies or ordered execution are crucial for comprehensive testing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Set Priority in TestNG for Test Cases?<\/strong><\/h2>\n\n\n\n<p>Let\u2019s consider below TestNG program where we will define priority with the test methods to execute in sequential order.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> Launch the Eclipse<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> testNGPriority;\n\n<strong>import<\/strong> org.testng.annotations.Test;\n\n<strong>public<\/strong> <strong>class<\/strong> testNGPriorityExample {\n@Test(priority = 1)&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> one()&nbsp;\n{&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"First Test Case\");&nbsp;\n}&nbsp;\n@Test(priority = 2)&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> two()\n{&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"Second Test Case\");\n}&nbsp;\n@Test(priority = 3)&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> three()\n{&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"Third Test Case\");&nbsp;\n}&nbsp;\n}<\/code><\/pre>\n\n\n\n<p><strong>Step 2:<\/strong> Right click on Java class &gt; <strong>Run As<\/strong>&nbsp;and then click on the&nbsp;<strong>TestNG Test<\/strong>.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p><img decoding=\"async\" width=\"602\" height=\"222\" alt=\"priorityex.PNG\" src=\"https:\/\/lh6.googleusercontent.com\/PC1-3NGUtlh8dB48pOqHiEqBK2O4Qen1nHTSo4G0oRdNn5vNhUcDusiXhIQp7VeKIiu-YxWkEREjMR_eEM9wHgwwIrQP9fDAODtOj875bmkZqhgk41dg8DEWCfz8o4JdUX-wgVTFZLniIMAZFA\" title=\"\"><\/p>\n\n\n\n<p>After assigning the priority to each test method the output has changed. From the above output you can see the test method having lower priority is executed first i.e. execution is happened in sequential order based on priority assigned to the test method.<\/p>\n\n\n\n<p>Thus, alphabetical order method name has not been considered to the test methods as we provide priorities and all the test cases has got executed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Test Methods with Same Priority<\/strong><\/h3>\n\n\n\n<p>In TestNG, when multiple test methods are assigned the same priority level, TestNG applies a default execution rule to determine their order. <\/p>\n\n\n\n<p>This allows us to explore how TestNG handles test methods with equal priority values and in what sequence they execute. Let\u2019s look at a TestNG program where we define several test methods with the same priority. <\/p>\n\n\n\n<p>By observing this program, we can examine TestNG&#8217;s execution behavior and understand how it organizes methods alphabetically when priority levels are identical.<\/p>\n\n\n\n<p>This feature is particularly useful for testers who want a deeper understanding of TestNG\u2019s default ordering mechanism and how it can help achieve consistent, organized test runs even when methods share priority.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> Launch the Eclipse<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> testNGPriority;\n\n<strong>import<\/strong> org.testng.annotations.Test;\n\n<strong>public<\/strong> <strong>class<\/strong> SamePriorityExample {\n@Test(priority = 1)&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> firstTest()&nbsp;\n{&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"First test method\");&nbsp;\n}&nbsp;\n@Test(priority = 1)&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> secondTest()\n{&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"Second test method\");&nbsp;\n}&nbsp;\n@Test(priority = 0)&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> thirdTest()\n{&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"Third test method\");&nbsp;\n}&nbsp;\n@Test(priority = -1)&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> fourthTest()\n{&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"Fourth test method\");&nbsp;\n}&nbsp;\n@Test(priority = -2)&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> fifthTest()\n{&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"Fifth test method\");&nbsp;\n}&nbsp;\n}<\/code><\/pre>\n\n\n\n<p><strong>Step 2:<\/strong> Right click on Java class &gt; <strong>Run As<\/strong>&nbsp;and then click on the&nbsp;<strong>TestNG Test<\/strong>.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p><img decoding=\"async\" width=\"580\" height=\"327\" alt=\"samepriority.PNG\" src=\"https:\/\/lh4.googleusercontent.com\/BfypiWu-77FeWaY15YPmW37pNe19qE3nEpGlTTSJbLRvy_M3ls-fDnCWzThMiH9YoQzbz-yEbAzTzESMIDNq9h3YRMfaL-T_9dUg_EPAMhOr8d8jnjwcdchoDcPcx-eNE8UqaAr4ZG0rX15grg\" title=\"\"><\/p>\n\n\n\n<p>In the example provided, we have a class containing five test methods, each assigned specific priority values that determine the execution order in TestNG. TestNG follows a priority-based approach, where lower values have higher execution precedence. However, if two or more methods are assigned the same priority, TestNG defaults to executing those methods in alphabetical order by their method names.<\/p>\n\n\n\n<p>For instance, in this case, the methods <code>firstTest()<\/code> and <code>secondTest()<\/code> both have a priority value of <code>1<\/code>. Since these two methods share the same priority, TestNG checks the alphabetical order of their names to decide the execution sequence. <\/p>\n\n\n\n<p>Here, TestNG detects that <code>firstTest()<\/code> should run before <code>secondTest()<\/code> because the letter &#8216;f&#8217; comes before &#8216;s&#8217; alphabetically. This ensures consistency in execution when priority conflicts arise, helping maintain a predictable test order.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Combination of Prioritized&nbsp; and Non-prioritized methods<\/strong><\/h3>\n\n\n\n<p>Let\u2019s consider the following TestNG program where we cover two scenarios within a single TestNG class. First, we have test methods with the same priority value, and second, we include multiple methods without any assigned priority. This setup allows us to observe how TestNG handles tests when there are methods with identical priority levels and methods that lack priority altogether.<\/p>\n\n\n\n<p>In TestNG, when multiple methods share the same priority, they execute in alphabetical order by their method names. However, if there are also non-prioritized methods in the same class, TestNG will execute these methods last, again in alphabetical order. This behavior illustrates how TestNG\u2019s default execution rules prioritize based on priority values first, then organize remaining non-prioritized tests.<\/p>\n\n\n\n<p>By experimenting with these configurations, testers can gain a deeper understanding of how TestNG organizes tests and how to use the <code>@Test<\/code> annotation with priority settings effectively, especially when custom execution orders are needed. <\/p>\n\n\n\n<p>This understanding becomes invaluable for complex test suites, allowing testers to have more control over test execution flow and better manage dependencies between tests.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> Launch the Eclipse<\/p>\n\n\n\n<p><strong>Step 2: <\/strong>Create Class name as \u201cTestNGPriorityTest\u201d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> testNGPriority;\n\n<strong>import<\/strong> org.testng.annotations.Test;\n\n<strong>public<\/strong> <strong>class<\/strong> TestNGPriorityTest {\n@Test&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> m1_c()&nbsp;\n{&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"m1_c\");&nbsp;\n}&nbsp;\n@Test&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> m1_b()\n{&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"m1_b\");&nbsp;\n}&nbsp;\n@Test(priority = 1)&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> m1_a()\n{&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"m1_a\");&nbsp;\n}&nbsp;\n@Test(priority = 1)&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> m1_d()\n{&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"m1_d\");&nbsp;\n}&nbsp;\n@Test(priority = 0)&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> m1_e()\n{&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"m1_e\");&nbsp;\n}&nbsp;\n@Test(priority = 2)&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> m1_f()\n{&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"m1_f\");&nbsp;\n&nbsp; }&nbsp;\n\n}<\/code><\/pre>\n\n\n\n<p><strong>Step 2:<\/strong> Right click on Java class &gt; <strong>Run As<\/strong>&nbsp;and then click on the&nbsp;<strong>TestNG Test<\/strong>.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"389\" alt=\"priority.PNG\" src=\"https:\/\/lh4.googleusercontent.com\/QxUr_S1CehYLtJS06-PZTruHWGettKTvD1Enq_1-ZTb65pYmyBzGUN6rDKCAo03b09tPfaE5t0HgHi82UU0kB4cxKDKlgAgk6FlglfZAhxyY4ku02dPmHgS67deahh3sTZrBU563Edu90ePeiw\" title=\"\"><\/p>\n\n\n\n<p><strong>First preference:<\/strong>&nbsp;In the above class, m1_b() and m1_c() which are <a href=\"https:\/\/en.wikipedia.org\/wiki\/Regression_testing#Test_case_prioritization\" rel=\"nofollow noopener\" target=\"_blank\">non-prioritized test methods <\/a>have been executed based on alphabetical order \u2018b\u2019 and then \u2018c\u2019.<br><br><strong>Second preference: <\/strong>m1_e() which is&nbsp;prioritized methods has been executed first because of having the highest priority (0). As the test methods m1_a() and m1_d() have the same priority. Therefore, TestNG will considered the alphabetical order of their methods names. So, between them, \u201cm1_a\u201d was executed first and then \u201cm1_d\u201d.<\/p>\n\n\n\n<p><strong>Third preference:<\/strong>&nbsp; m1_f() is executed last by TestNG because of having the lowest priority (2).<\/p>\n\n\n\n<p><strong>Let\u2019s consider the scenario where we automate Google home page and print the title of home page by using the priority feature.<\/strong><\/p>\n\n\n\n<p><strong>Step 1:<\/strong> Launch the Eclipse<\/p>\n\n\n\n<p><strong>Step 2:<\/strong> Create the Class name as \u201cGoogleTest\u201d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> sampletestpackage;\n\n<strong>import<\/strong> org.openqa.selenium.WebDriver;\n<strong>import<\/strong> org.openqa.selenium.chrome.ChromeDriver;\n<strong>import<\/strong> org.testng.annotations.Test;\n\n<strong>public<\/strong> <strong>class<\/strong> GoogleTest {\nWebDriver driver;\n@Test(priority = 1)&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> driverSetup()\n{&nbsp;\nSystem.<em>setProperty<\/em>(\"webdriver.chrome.driver\", \"D:\\\\Drivers\\\\geckodriver.exe\");\ndriver=<strong>new<\/strong> ChromeDriver(); }&nbsp;\n@Test(priority = 2)\n<strong>public<\/strong> <strong>void<\/strong> getURL()\n{&nbsp;\n&nbsp; driver.get(\"https:\/\/www.google.com\");&nbsp;\n}&nbsp;\n@Test(priority = 3)&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> getTitle()\n{&nbsp;\n&nbsp; String title = driver.getTitle();&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(title);&nbsp;\n&nbsp; }&nbsp;\n@Test(priority = 4)&nbsp;\n<strong>public<\/strong> <strong>void<\/strong> closeBrowser()\n{&nbsp;\n&nbsp; driver.close();&nbsp;\n&nbsp; System.<strong><em>out<\/em><\/strong>.println(\"Test successfully passed\");&nbsp;\n&nbsp; }&nbsp;\n}<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"531\" height=\"294\" alt=\"google.PNG\" src=\"https:\/\/lh3.googleusercontent.com\/TjJhhmpYMRylNqSDg7S3o2nS5DNU9jd0RXoC66jCpj_LgtSWd52b0TNgUUWHAbaLYF1iCoGT7SYH4tOQusiqpSLriUyPDUuJ_b_2BSblnhJDG1chxkGiqlKgWhz7yhL4318nZTVy_2jVP_gYJw\" title=\"\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion: <\/h2>\n\n\n\n<p>In TestNG, setting a specific execution order for test methods is straightforward with the use of priorities. By assigning priority values, you can control the sequence in which tests run, ensuring that essential steps are completed in a defined order. <\/p>\n\n\n\n<p>This approach is particularly valuable for complex test scenarios where certain tests depend on the outcomes of previous ones. With TestNG\u2019s priority feature, creating an organized and efficient testing flow becomes easy, allowing you to manage test dependencies and improve overall test reliability.<\/p>\n\n\n\n<p>Moreover, when two or more methods share the same priority value, TestNG\u2019s default alphabetical ordering provides an additional layer of control, ensuring a consistent and structured execution sequence. This flexibility in ordering, combined with the use of the Test Annotation in TestNG, not only improves test organization but also minimizes the risk of dependency-related test failures, enhancing the overall stability of your automated test suite.<\/p>\n\n\n\n<p>For testers and developers working on complex projects, TestNG\u2019s priority feature along with the Test Annotation streamlines the testing process, making it easier to manage and optimize test flows. With careful use of priority settings and Test Annotation, teams can maintain more robust, reliable tests that support seamless development and faster debugging.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Call to Action<\/strong><\/h2>\n\n\n\n<p>Ready to take control of your test execution order? At H2K Infosys, our expert-led Selenium courses cover TestNG\u2019s @Test Priority in detail, equipping you with the skills to manage complex test flows, prioritize critical tests, and boost test efficiency. Join H2K Infosys today and unlock the full potential of TestNG<strong> <\/strong>@Test Priority in Selenium to advance your career in automation testing!<br><br><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Test Priority? In TestNG, \u201cPrioritization\u201d means assigning the Execution Priority on Test Case. For example, we want to execute the test cases in order when there are multiple test cases, i.e., first we need to execute the \u201cRegistration\u201d test case before login. The priority on the test case will define the Execution order [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":3708,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[976,977,1871,987,313,1870,975,974,1873,1872],"class_list":["post-3698","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials","tag-cases-without-priority","tag-prioritized-methods","tag-test-annotation","tag-test-case","tag-test-cases","tag-test-priority","tag-test-priority-in-selenium","tag-testng","tag-testng-for-test-cases","tag-testng-program"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3698"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3698\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3708"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}