Over a million developers have joined DZone.

How To Disable Test Method using TestNG?

· Agile Zone

Learn more about how DevOps teams must adopt a more agile development process, working in parallel instead of waiting on other teams to finish their components or for resources to become available, brought to you in partnership with CA Technologies.

Test annotation in TestNG has several attributes. We can use “enabled” attribute to ignore / disable certain test methods while executing the Test classes.

The default value for “enabled” is true

Code

The following example shows how to disable test methods using TestNG

Service Class

package com.skilledmonster.example/*** Calculator Service Interface to demonstrate TestNG examples** @author Jagadeesh Motamarri* @version 1.0*/public interface CalculatorService {  int sum(int a, int b);  int multiply(int a, int b);  int div(int a, int b);  int sub(int a, int b);}

Implementation Class

package com.skilledmonster.example;
/**
* Calculator Service implementation class
*
* @author Jagadeesh Motamarri
* @version 1.0
*/
public class SimpleCalculator implements CalculatorService {
  public int sum(int a, int b) {
return a + b;
}
  public int multiply(int a, int b) {
return a * b;
}
  public int div(int a, int b) {
return a / b;
}
  public int sub(int a, int b) {
return a - b;
}
}

Test Class

package com.skilledmonster.example;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* Calculate Service Test Class to show usage of enabled attribute in @Test annotation
*
* @author Jagadeesh Motamarri
* @version 1.0
*/
public class DisableTestMethodAnnotation {
  public CalculatorService service;
  @BeforeClass
public void init() {
System.out.println("@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.");
System.out.println("init service");
service = new SimpleCalculator();
}
  @Test(enabled = false)
public void testSum() {
System.out.println("@Test : testSum()");
int result = service.sum(10, 5);
Assert.assertEquals(result, 15);
}
  @Test
public void testDiv() {
System.out.println("@Test : testDiv()");
int result = service.div(10, 5);
Assert.assertEquals(result, 2);
}
  @Test(enabled = false)
public void testMultiply() {
System.out.println("@Test : testMultiple()");
int result = service.multiply(10, 5);
Assert.assertEquals(result, 50);
}
  @Test
public void testSub() {
System.out.println("@Test : testSub()");
int result = service.sub(10, 5);
Assert.assertEquals(result, 5);
}
}

Output

testng_disable_test_method

As shown in the above test results, only testDiv() and testSub() methods are executed. The methods testSum() and testMultiply() are not executed because “enabled” attribute is set to false.

Discover the warning signs of DevOps Dysfunction and learn how to get back on the right track, brought to you in partnership with CA Technologies.

Topics:
java ,testing ,testng

Published at DZone with permission of Jagadeesh Motamarri, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}