{"id":3693,"date":"2020-06-11T18:30:26","date_gmt":"2020-06-11T13:00:26","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3693"},"modified":"2024-10-04T17:54:01","modified_gmt":"2024-10-04T12:24:01","slug":"parallel-test-execution-in-selenium-using-testng","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/parallel-test-execution-in-selenium-using-testng\/","title":{"rendered":"Parallel Test Execution in Selenium using TestNG"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>What is Parallel Testing in TestNG?<\/strong><\/h2>\n\n\n\n<p>Parallel Testing is a technique where you want to run multiple tests simultaneously in different threads to reduce the execution time. It allows us to<a href=\"https:\/\/www.h2kinfosys.com\/blog\/how-to-run-multiple-test-suites-in-selenium-testng\/\"> run multiple tests <\/a>at the same time across multiple environments instead of running tests one by one or in sequential order. Hence, it is called a parallel test execution in selenium.<\/p>\n\n\n\n<p>Parallel testing helps us to run classes, test methods, tests in parallel. We can reduce the execution time as tests will get executed simultaneously by using parallel test execution.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How to execute Classes, Parallel Tests, &amp; Test Methods in Selenium using TestNG XML File?<\/strong><\/h3>\n\n\n\n<p>In Selenium, by using the \u201cparallel\u201d attribute we can execute our classes, test methods, and tests in parallel for Test Suite in the testng.xml file. The parallel attribute for on the &lt;suite&gt; tag can accept one of the following values:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>&lt;suite name = &#8220;Parallel Test Suite&#8221; parallel = &#8220;methods&#8221; thread-count = &#8220;4&#8221;><\/li>\n\n\n\n<li>&lt;suite name = &#8220;Parallel Test Suite&#8221; parallel = &#8220;classes&#8221; thread-count = &#8220;4&#8221;><\/li>\n\n\n\n<li>&lt;suite name = &#8220;Parallel Test Suite&#8221; parallel = &#8220;tests&#8221; thread-count = &#8220;4&#8221;><\/li>\n\n\n\n<li>&lt;suite name = &#8220;Parallel Test Suite&#8221; parallel = &#8220;instances&#8221; thread-count = &#8220;4&#8221;><\/li>\n<\/ol>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>parallel = \u201cmethods\u201d:\u00a0<\/strong>TestNG will run all the methods in parallel annotated with @Test annotation in separate threads. Dependent methods will also run in separate threads in the specified order.<\/li>\n\n\n\n<li><strong>parallel = \u201cclasses\u201c:<\/strong>\u00a0TestNG will run all the test methods in the same class in the same thread but in a separate thread each Java class will get run.<\/li>\n\n\n\n<li><strong>parallel = \u201cTests\u201c:<\/strong>\u00a0TestNG will run all the test cases or test methods in the same &lt;test> tag in the same thread but in different threads, each &lt;test> tag will get run.<\/li>\n\n\n\n<li><strong>parallel = \u201cinstances\u201c:<\/strong>\u00a0TestNG will run all the test cases in the same instance in the same thread, but in different threads two methods on two different instances will get run.<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"TestNG Annotations | Selenium And Junit Integration To Jenkins | Devops Tutorial For Beginners H2K\" width=\"800\" height=\"450\" src=\"https:\/\/www.youtube.com\/embed\/Ggn22qs5ExA?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>The attribute \u201cthread-count\u201d specifies no of threads should be allocated for this execution.<\/p>\n\n\n\n<p>Let\u2019s consider below example for Parallel execution of Test Methods using \u201cparallel\u201d attribute for &lt;suite&gt; tag in testng.xml file.<\/p>\n\n\n\n<p><strong>Parallel Execution of Test Methods in TestNG<\/strong><\/p>\n\n\n\n<p>In below example, we will create a class with two methods and execute them in different threads.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> <a href=\"https:\/\/www.h2kinfosys.com\/blog\/how-to-install-testng-in-eclipse\/\">Launch the Eclipse<\/a><\/p>\n\n\n\n<p><strong>Step 2:<\/strong> Create a New Package (parallelTesting)<\/p>\n\n\n\n<p><strong>Step 3:<\/strong> Create a new class TestMethodparallelExecution<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ClassOne {\n@Test \npublic void firstTest() \n{ \n  System.out.println(\" Test method One in ClassOne\"+\" \" +\"Thread Id: \" +Thread.currentThread().getId()); \n} \n@Test \npublic void secondTest() \n{ \n  System.out.println(\" Test method two in ClassOne\"+ \" \" +\"Thread Id: \" +Thread.currentThread().getId()); \n} \n\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package parallelTesting;\n\nimport org.testng.annotations.Test;\n\npublic class ClassTwo {\n@Test \npublic void firstTest() \n{ \n  System.out.println(\" Test method one in ClassTwo\"+ \" \" +\"Thread Id: \" +Thread.currentThread().getId()); \n} \n@Test \npublic void secondTest() \n{ \n  System.out.println(\" Test method two in ClassTwo\"+ \" \" +\"Thread Id: \" +Thread.currentThread().getId()); \n  } \n\n}<\/code><\/pre>\n\n\n\n<p>Now, let\u2019s create a testng.xml file and configure with two attributes \u201cparallel\u201d and \u201cthread-count\u201d for the execution of test methods in parallel at suite level.<\/p>\n\n\n\n<p>Here, the attribute \u201cthread-count\u201d is used to pass the number of maximum threads to be created.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;suite name = <em>\"Parallel Test Suite\"<\/em> parallel = <em>\"methods\"<\/em> thread-count = <em>\"3\"<\/em>>\u00a0\n&lt;test name = <em>\"Test Methods\"<\/em>>\u00a0\n&lt;classes>\u00a0\n\u00a0\u00a0\u00a0\u00a0&lt;class name = <em>\"parallelTesting.TestMethodparallelExecution\"<\/em>\/>\u00a0\n&lt;\/classes>\u00a0\n&lt;\/test>\u00a0\n&lt;\/suite><\/code><\/pre>\n\n\n\n<p>Run the above configure testng.xml file as TestNG Suite and it will display the output as<\/p>\n\n\n\n<p><img fetchpriority=\"high\" decoding=\"async\" width=\"530\" height=\"329\" alt=\"Result.PNG\" src=\"https:\/\/lh6.googleusercontent.com\/0ic5z0HKemhpt2dykLYOc7mMloWUvLA7fD98fpMW214h2QQmHENt7scCfljAAswGdov6XuPlplmc6va0qJvgOGdHp13UFh3dOLs07XWsnasJlmwpN8nRmHm_fV9iBoaOIweZeBKRDIW9keFS5Q\" title=\"\"><\/p>\n\n\n\n<p>From the above result you observe that two test methods have been executed in parallel in separate threads. Both test methods have taken time 0.013s for complete execution.<\/p>\n\n\n\n<p><strong>Parallel Execution of Test Classes in TestNG<\/strong><\/p>\n\n\n\n<p>In this scenario, we will run test cases in parallel. During execution, each <a href=\"https:\/\/www.h2kinfosys.com\/blog\/nested-classes-in-java\/\">java class <\/a>will start and execute simultaneously in separate threads.<\/p>\n\n\n\n<p>In below example, we will create two test classes with two methods and execute them in different threads.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> Launch the Eclipse<\/p>\n\n\n\n<p><strong>Step 2:<\/strong> Create a New Package (parallelTesting)<\/p>\n\n\n\n<p><strong>Step 3:<\/strong> Create a new class \u201cClassOne\u201d with two test methods.<\/p>\n\n\n\n<p>Now create a new testng.xml file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;suite thread-count = <em>\"3\"<\/em> name = <em>\"Parallel Test Suite\"<\/em> parallel = <em>\"classes\"<\/em>>\u00a0\n&lt;test name = <em>\"Test Classes\"<\/em>>\u00a0\n&lt;classes>\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0&lt;class name = <em>\"parallelTesting.ClassOne\"<\/em>\/>\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0&lt;class name = <em>\"parallelTesting.ClassTwo\"<\/em>\/>\u00a0\n&lt;\/classes>\u00a0\n&lt;\/test>\u00a0\n&lt;\/suite><\/code><\/pre>\n\n\n\n<p>Run the above configure testng.xml file as TestNG Suite and it will display the output in console as<\/p>\n\n\n\n<p><img decoding=\"async\" width=\"519\" height=\"323\" alt=\"method1.PNG\" src=\"https:\/\/lh3.googleusercontent.com\/KMFoz5Iu9P1u49hiXzBWOVTKcuT9qfQsRnIE47pnEksSFJfV6jMcT7kXTVTJDcsughxVl_7pDAUH4iCmgnks3KGv8kLIldOR5gFn2I8Pcwsbjl_zjMF01IFpqEp_rP8R5cJKKrmOAEDdZXvLwg\" title=\"\"><\/p>\n\n\n\n<p>From the above output, TestNG has run all the test cases (<a href=\"https:\/\/stackoverflow.com\/questions\/19211726\/testng-parallel-execution\" rel=\"nofollow noopener\" target=\"_blank\">test methods<\/a>) in the same class in the same thread but each Java class has run in different threads.<\/p>\n\n\n\n<p><strong>Results of running suite<\/strong><\/p>\n\n\n\n<p><img decoding=\"async\" width=\"526\" height=\"373\" alt=\"method2.PNG\" src=\"https:\/\/lh4.googleusercontent.com\/pSNCjrXnnmqkKSst_Eqnlo8fP-k-sqXIkrA03CsyagALX5OMIsl6LlRCCJSIpd3k-cmNOgqZxvRuSEHA2nvxJg5-tpDoZZesXdOrQdPtr80-2aEJlYkJ39Z94feH4a46dyM8E_YQYag5NyL9Iw\" title=\"\"><\/p>\n\n\n\n<p>From the above result we observe that ClassOne, and ClassTwo have executed at the same time in parallel in which firstTest() methods of both classes and secondTest() methods of both classes have the same execution time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Parallel Execution of Tests in TestNG<\/strong><\/h2>\n\n\n\n<p>In below example we will consider a program for parallel execution of tests and configure testng.xml file to the following code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;suite thread-count = <em>\"3\"<\/em> name = <em>\"Parallel Test Suite\"<\/em> parallel = <em>\"tests\"<\/em>>\u00a0\n&lt;test name = <em>\"ClassOne\"<\/em>>\u00a0\n&lt;classes>\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0&lt;class name = <em>\"parallelTesting.ClassOne\"<\/em>\/>\u00a0\n&lt;\/classes>\u00a0\n&lt;\/test>\u00a0\n&lt;test name = <em>\"ClassTwo\"<\/em>>\u00a0\n&lt;classes>\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;class name = <em>\"parallelTesting.ClassTwo\"<\/em>\/>\u00a0\n&lt;\/classes>\u00a0\n&lt;\/test>\u00a0\n&lt;\/suite><\/code><\/pre>\n\n\n\n<p>Run the above configure testng.xml file as TestNG Suite and it will display the output in console as<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"526\" height=\"299\" alt=\"test1.PNG\" src=\"https:\/\/lh5.googleusercontent.com\/mTsK6uZY0b_P09IrkDx9Gn1BnAA7U4BtGaoLVnVM6Z1kQ4vnA0UHO4cPSeN39XCeiqeAjXtZo7BCd0IOATo7iWcmkH_JPH03inu85kE86b-BrKzUEAnd7U6aM4hKRZruHrdOgI9bgsxInAvhTw\" title=\"\"><\/p>\n\n\n\n<p>From the above output, TestNG has run all the test methods in the same &lt;test&gt; tag in the same thread but each &lt;test&gt; tag has run in different threads.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"528\" height=\"357\" alt=\"test2.PNG\" src=\"https:\/\/lh5.googleusercontent.com\/OreQ7vkXLUKxAtiZiObBgAYUrR8y3785mZEBKKkakOiGdo55uvif0S4pjk97dmRd6A8nl4IZRpFlJrlBbjjt8F_GAI6JpH6zieU-iyOwMd9fYEyngoQSYbt3-8Ifj1fJTe2S2vLyUlE9HcrL2g\" title=\"\"><\/p>\n\n\n\n<p>From the above output, both ClassOne and ClassTwo tests have run in parallel at the same time.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-webdriver-junit-training-course\"><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"90\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/11\/Selenium-Advanced-Training-2.jpeg\" alt=\"selenium advanced training\" class=\"wp-image-6669\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/11\/Selenium-Advanced-Training-2.jpeg 728w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/11\/Selenium-Advanced-Training-2-300x37.jpeg 300w\" sizes=\"(max-width: 728px) 100vw, 728px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>What is Parallel Testing in TestNG? Parallel Testing is a technique where you want to run multiple tests simultaneously in different threads to reduce the execution time. It allows us to run multiple tests at the same time across multiple environments instead of running tests one by one or in sequential order. Hence, it is [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":3696,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[968,969,1875,1877,978,1876],"class_list":["post-3693","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials","tag-parallel-test-execution","tag-parallel-testing-in-testng","tag-parallel-tests","tag-selenium-using-testng","tag-test-execution-in-selenium","tag-test-methods-in-selenium"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3693","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=3693"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3693\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3696"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}