Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How To Disable Test Method using TestNG?

DZone's Guide to

How To Disable Test Method using TestNG?

· Agile Zone
Free Resource

See how three solutions work together to help your teams have the tools they need to deliver quality software quickly. 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 how TDM Is Essential To Achieving Quality At Speed For Agile, DevOps, And Continuous Delivery. 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.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}