Site icon TestingDocs.com

How to ignore a Test in TestNG?

Overview

In this tutorial, we will learn how 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 use of the annotation is just similar to the JUnit framework. For more details:

https://www.testingdocs.com/ignore-annotation-in-junit/

We can use this annotation at various levels like

@Test(enabled=false)

The alternative of the @Ignore annotation is the Test annotation switch attribute enabled=false. We can disable a test by using the attribute switch enabled = false in the @Test  annotation. Let’s understand this using an example test class.

 

package com.testingdocs.testng.ignore;

//www.TestingDocs.com - TestNG Tutorials
import org.testng.Assert;
import org.testng.annotations.Test;

public class IgnoreTest {

	@Test // By default the test is enabled=true
	public void defaultTest() {
		Assert.assertEquals(true, true);
	}

	@Test(enabled = false)
	public void ignoreThisTest() {
		Assert.assertEquals(true, true);
	}

}

 

 

 

By default the flag is true. i.e enabled = true 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.

 

PASSED: defaultTest

===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================

 

Dependent Methods

It’s important to check if any methods dependent on this method before ignoring it.

 

package com.testingdocs.testng.ignore;

//www.TestingDocs.com - TestNG Tutorials
import org.testng.Assert;
import org.testng.annotations.Test;

public class IgnoreTest {

	@Test(enabled = false)
	public void startServer() {
		Assert.assertEquals(true, true);
	}

	@Test(dependsOnMethods="startServer")
	public void ignoreThisTest() {
		Assert.assertEquals(true, true);
	}

}

 

The dependent method would encounter an Exception.

 

org.testng.TestNGException: 
com.testingdocs.testng.ignore.IgnoreTest.ignoreThisTest() is depending on method
 public void com.testingdocs.testng.ignore.IgnoreTest.startServer(),
 which is not annotated with @Test or not included.

 

TestNG Tutorials on this website can be found at:

https://www.testingdocs.com/testng-framework-tutorial/

For more details on the TestNG Framework, visit the official website of TestNG at:

https://testng.org

Exit mobile version