{"id":19270,"date":"2017-03-15T12:12:44","date_gmt":"2017-03-15T12:12:44","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=19270"},"modified":"2024-09-04T15:47:13","modified_gmt":"2024-09-04T15:47:13","slug":"how-to-test-method-timeouts-in-testng","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-test-method-timeouts-in-testng\/","title":{"rendered":"How to Test method Timeouts in TestNG"},"content":{"rendered":"<h2>How to Test method Timeouts in TestNG<\/h2>\n<p>In this tutorial, we will learn how to Test method Timeouts in TestNG framework. We can use this feature to automatically mark the @Test methods that take longer execution time as failures. We can specify the timeout in milliseconds, using the <strong>timeOut<\/strong> attribute of @Test annotation.<\/p>\n<p>A long executing test would slog the other tests. In such cases we can add the timeOut attribute to the test methods.<\/p>\n<h3>timeOut attribute<\/h3>\n<p>Sample usage of the timeOut attribute. For example to set the timeout of 5 secs to a test method:<\/p>\n<p>@Test(timeOut = 5000) \/\/ SLA &#8211; 5secs<\/p>\n<p>public void testMethodWithFiveSecSLA()<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-19272\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Timeout-Test-methods-in-TestNG.png\" alt=\"Timeout Test methods in TestNG\" width=\"1613\" height=\"913\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Timeout-Test-methods-in-TestNG.png 1613w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Timeout-Test-methods-in-TestNG-300x170.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Timeout-Test-methods-in-TestNG-1024x580.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Timeout-Test-methods-in-TestNG-768x435.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Timeout-Test-methods-in-TestNG-1536x869.png 1536w\" sizes=\"auto, (max-width: 1613px) 100vw, 1613px\" \/><\/p>\n<h3>Sample Code<\/h3>\n<p>&nbsp;<\/p>\n<pre class=\"dm-pre-admin-side\">package com.testingdocs.tests.timeouts; \r\n\r\nimport org.testng.annotations.Test; \r\n\r\n\/\/################################### \r\n\/\/ Testing timeout in Test methods \r\n\/\/ www.TestingDocs.com\r\n\/\/################################### \r\n\r\npublic class TimeoutsTestNG { \r\n\r\n@Test(timeOut = 5000) \/\/ SLA - 5secs Time in milliseconds \r\npublic void testMethodShouldPass() throws InterruptedException { \r\n         Thread.sleep(4000); \r\n} \r\n\r\n@Test(timeOut = 5000) \r\npublic void testMethodShouldFail() { \r\n         while (true);\/\/ indefinite loop - this test will fail\r\n } \r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3>Timeout in XML file<\/h3>\n<p>Notice that for every Test method we are specifying the timeOut attribute. To set the timeout for multiple tests we can set this in the TestNG XML file.<\/p>\n<p>We can specify the timeout at various level in the XML file.<\/p>\n<ul>\n<li><strong>Suite<\/strong><\/li>\n<li><strong>Test<\/strong><\/li>\n<li><strong>Groups<\/strong><\/li>\n<li><strong>Classes<\/strong><\/li>\n<\/ul>\n<p>Let&#8217;s see how we can specify the timeout in the XML file.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"dm-pre-admin-side\">&lt;!DOCTYPE suite SYSTEM \"https:\/\/testng.org\/testng-1.0.dtd\" &gt;\r\n  \r\n&lt;!-- TestNG Tutorials --&gt;\r\n&lt;!-- www.TestingDocs.com --&gt; \r\n&lt;suite name=\"Timeout Tests\" time-out=\"5000\"&gt; \r\n  &lt;test name=\"SampleTest1\"&gt; \r\n\t&lt;classes&gt;\r\n\t  &lt;class name=\"com.testingdocs.tests.timeouts.TimeoutsSuiteXMLDemo\" \/&gt;\r\n\t&lt;\/classes&gt; \r\n  &lt;\/test&gt; \r\n&lt;\/suite&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>We can remove the timeout attributes from the @Test annotation in the test methods. We can place all the test classes that test with the timeout = 5 secs under this suite.<\/p>\n<p>Test methods without <strong>timeOut<\/strong> annotation attribute. Timeout for both the methods would be set to 5 secs as specified in the XML file.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"dm-pre-admin-side\">package com.testingdocs.tests.timeouts;\r\n\r\nimport org.testng.annotations.Test;\r\n\r\n\/\/###################################\r\n\/\/# Testing timeout in Test methods\r\n\/\/# www.TestingDocs.com\r\n\/\/###################################\r\n\r\npublic class TimeoutsSuiteXMLDemo {\r\n\t\/\/ 5 secs timeout specified in the testng.xml file\r\n\t@Test \r\n\tpublic void testMethodShouldPass() throws InterruptedException {\r\n\t\tThread.sleep(4000);\r\n\t}\r\n\r\n\t@Test\r\n\tpublic void testMethodShouldFail() {\r\n\t\twhile (true);\/\/ indefinite loop - this test will fail\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-19414 size-full\" title=\"Timeout Five Secs Suite XML\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Timeout-Five-Secs-Suite-XML.png\" alt=\"Timeout Five Secs Suite XML\" width=\"1728\" height=\"919\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Timeout-Five-Secs-Suite-XML.png 1728w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Timeout-Five-Secs-Suite-XML-300x160.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Timeout-Five-Secs-Suite-XML-1024x545.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Timeout-Five-Secs-Suite-XML-768x408.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Timeout-Five-Secs-Suite-XML-1536x817.png 1536w\" sizes=\"auto, (max-width: 1728px) 100vw, 1728px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Use of Timeouts<\/strong><\/p>\n<p>For example, assume we are testing an API call with SLA( Service level agreement) that the API should respond within 5 secs of time from the API request is sent. It is considered a failure if the API responds after 5 sec as per the SLA. We can use the timeout for the service tests to check the slow API calls.<\/p>\n<h3>Points to Ponder<\/h3>\n<p>1.What happens if we specify time-out as 5000 ms in &lt;suite&gt; XML file and @Test(timeOut=7000ms) in the test method. The test method takes 6000 ms to complete execution.<\/p>\n<p>2.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"dm-pre-admin-side\">&lt;!DOCTYPE suite SYSTEM \"https:\/\/testng.org\/testng-1.0.dtd\" &gt;\r\n&lt;!-- TestNG Tutorials --&gt;\r\n&lt;!-- www.TestingDocs.com --&gt; \r\n&lt;suite name=\"Timeout Tests\" time-out=\"5000\"&gt; \r\n  &lt;test name=\"SampleTest1\" time-out=\"3000\"&gt; \r\n\t&lt;classes&gt;\r\n\t  &lt;class name=\"com.testingdocs.tests.timeouts.TimeoutsSuiteXMLDemo\" \/&gt;\r\n\t&lt;\/classes&gt; \r\n  &lt;\/test&gt; \r\n&lt;\/suite&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>Given the Test method:<\/p>\n<pre>@Test \r\npublic void testMethod() throws InterruptedException {\r\nThread.sleep(4000);\r\n}<\/pre>\n<p>What is the test result Pass\/Fail?<\/p>\n<p>&#8212;<\/p>\n<p>TestNG Tutorials on this website can be found at:<\/p>\n<p><strong><a href=\"https:\/\/www.testingdocs.com\/testng-framework-tutorial\/\">https:\/\/www.testingdocs.com\/testng-framework-tutorial\/<\/a><\/strong><\/p>\n<p>For more details on the TestNG Framework, visit the official website of TestNG at:<\/p>\n<p><strong><a href=\"https:\/\/testng.org\" rel=\"noopener\">https:\/\/testng.org<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Test method Timeouts in TestNG In this tutorial, we will learn how to Test method Timeouts in TestNG framework. We can use this feature to automatically mark the @Test methods that take longer execution time as failures. We can specify the timeout in milliseconds, using the timeOut attribute of @Test annotation. A long [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[213],"tags":[215],"class_list":["post-19270","post","type-post","status-publish","format-standard","hentry","category-testng","tag-testng-questions","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/19270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=19270"}],"version-history":[{"count":33,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/19270\/revisions"}],"predecessor-version":[{"id":24208,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/19270\/revisions\/24208"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=19270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=19270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=19270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}