{"id":19468,"date":"2017-03-19T08:57:26","date_gmt":"2017-03-19T08:57:26","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=19468"},"modified":"2024-08-05T03:13:11","modified_gmt":"2024-08-05T03:13:11","slug":"how-to-ignore-a-test-in-testng","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-ignore-a-test-in-testng\/","title":{"rendered":"How to ignore a Test in TestNG?"},"content":{"rendered":"<h2>How to ignore a Test in TestNG?<\/h2>\n<p>This tutorial will teach us to ignore a Test method in the TestNG framework. This is useful when we want to ignore or disable a test during the test run.<\/p>\n<h2>Ignore a Test<\/h2>\n<h3>@Ignore annotation<\/h3>\n<p>We can use the @Ignore annotation to ignore a test in TestNG. The annotation is similar to the JUnit framework. For more details:<\/p>\n<p><a href=\"https:\/\/www.testingdocs.com\/ignore-annotation-in-junit\/\"><strong>https:\/\/www.testingdocs.com\/ignore-annotation-in-junit\/<\/strong><\/a><\/p>\n<p>We can use this annotation at various levels like<\/p>\n<ul>\n<li>Test method level-&gt;Ignores the test method<\/li>\n<li>Class level &#8211; &gt; Ignore when applied on the class level will disable all the test methods in the class\/subclasses.<\/li>\n<li>package level-&gt; Ignore tests in package\/sub packages.<\/li>\n<\/ul>\n<h3>@Test(enabled=false)<\/h3>\n<p>The alternative of the @Ignore annotation is the Test annotation switch attribute enabled=false. We can disable a test by using the attribute switch <strong>enabled = false<\/strong> in the <strong>@Test\u00a0<\/strong> annotation. Let&#8217;s understand this using an example test class.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"dm-pre-admin-side\">package com.testingdocs.testng.ignore;\r\n\r\n\/\/www.TestingDocs.com - TestNG Tutorials\r\nimport org.testng.Assert;\r\nimport org.testng.annotations.Test;\r\n\r\npublic class IgnoreTest {\r\n\r\n\t@Test \/\/ By default the test is enabled=true\r\n\tpublic void defaultTest() {\r\n\t\tAssert.assertEquals(true, true);\r\n\t}\r\n\r\n\t@Test(enabled = false)\r\n\tpublic void ignoreThisTest() {\r\n\t\tAssert.assertEquals(true, true);\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 size-full wp-image-19471\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Ignore-a-Test-in-TestNG.png\" alt=\"Ignore a Test in TestNG\" width=\"1461\" height=\"919\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Ignore-a-Test-in-TestNG.png 1461w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Ignore-a-Test-in-TestNG-300x189.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Ignore-a-Test-in-TestNG-1024x644.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Ignore-a-Test-in-TestNG-768x483.png 768w\" sizes=\"auto, (max-width: 1461px) 100vw, 1461px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>By default the flag is true. i.e <strong>enabled = true<\/strong> if we do not specify in the @Test annotation. We can see that only one test is run. One test is ignored during the run.<\/p>\n<p>&nbsp;<\/p>\n<pre>PASSED: defaultTest\r\n\r\n===============================================\r\nDefault test\r\nTests run: 1, Failures: 0, Skips: 0\r\n===============================================<\/pre>\n<p>&nbsp;<\/p>\n<h3>Dependent Methods<\/h3>\n<p>It&#8217;s important to check if any methods dependent on this method before ignoring it.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"dm-pre-admin-side\">package com.testingdocs.testng.ignore;\r\n\r\n\/\/www.TestingDocs.com - TestNG Tutorials\r\nimport org.testng.Assert;\r\nimport org.testng.annotations.Test;\r\n\r\npublic class IgnoreTest {\r\n\r\n\t@Test(enabled = false)\r\n\tpublic void startServer() {\r\n\t\tAssert.assertEquals(true, true);\r\n\t}\r\n\r\n\t@Test(dependsOnMethods=\"startServer\")\r\n\tpublic void ignoreThisTest() {\r\n\t\tAssert.assertEquals(true, true);\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The dependent method would encounter an Exception.<\/p>\n<p>&nbsp;<\/p>\n<pre>org.testng.TestNGException: \r\ncom.testingdocs.testng.ignore.IgnoreTest.ignoreThisTest() is depending on method\r\n public void com.testingdocs.testng.ignore.IgnoreTest.startServer(),\r\n which is not annotated with @Test or not included.\r\n<\/pre>\n<p>&nbsp;<\/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 ignore a Test in TestNG? This tutorial will teach us to ignore a Test method in the TestNG framework. This is useful when we want to ignore or disable a test during the test run. Ignore a Test @Ignore annotation We can use the @Ignore annotation to ignore a test in TestNG. The [&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-19468","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\/19468","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=19468"}],"version-history":[{"count":20,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/19468\/revisions"}],"predecessor-version":[{"id":23516,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/19468\/revisions\/23516"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=19468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=19468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=19468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}