Ignore Test (@Ignore) in JUnit

In this blogpost, we are going to learn how to ignore a particular test case from a test suite.

This can be useful when we do not want to run a particular test method or a group of test methods contained in a class.

JUnit provides @Ignore annotation, that can be used at two levels.

  1. If you want to ignore a particular test method, you can use @Ignore at the method level.
  2. If you want to ignore all the test cases of a class, you can use @Ignore at the class level.

Using @Ignore at the method level:

Let us understand how to ignore a particular test method using @Ignore annotation.

Create a class containing all the @Test methods. Two test methods are annotated with @Ignore and @Ignore(“message”) annotation. @Ignore(“message”) works in the same way, but additionally allows us to mention any message for the test method

import org.junit.Ignore;
import org.junit.Test;

public class IgnoreTestExample {

@Test
public void testMethod1() {
System.out.println(“Inside test method 1”);
}

@Test
public void testMethod2() {
System.out.println(“Inside test method 2”);
}

@Ignore
@Test
public void ignoreTestMethod1() {
System.out.println(“Inside ignore test method 1”);
}

@Ignore(“The feature is not ready yet.”)
@Test
public void ignoreTestMethod2() {
System.out.println(“Inside test method 2”);
}

}

Create a test runner class to run the test case class.

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class IgnoreTestRunner {

public static void main(String[] args) {
Result result = JUnitCore.runClasses(IgnoreTestExample.class);

for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println();
System.out.println(“Test Suite execution was successful: ” + result.wasSuccessful());
}
}

Since ignoreTestMethod1() and ignoreTestMethod2() are marked with @Ignore annotation, the code enclosed within it will not be executed.

Using @Ignore at the class level

Now, we will see how to ignore all the test methods contained in a particular class.

Create a class annotated with @Ignore annotation, containing two test methods, one annotated with @Ignore annotation.

Here, since we have annotated a class with @Ignore annotation, none of the test methods of the class will be executed by JUnit. So, this is the way we can ignore multiple test methods at once, instead of annotating each test method with @Ignore annotation.

import static org.junit.Assert.*;

import org.junit.Ignore;
import org.junit.Test;

@Ignore
public class IgnoreTestClass {

@Test
public void testMethod1() {
System.out.println(“Inside test method 1”);
}

@Ignore
@Test
public void ignoreMethod1() {
System.out.println(“Inside ignore method 1”);
}

}

Create  a test runner class to run the test class’ methods.

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class IgnoreClassTestRunner {

public static void main(String[] args) {
Result result = JUnitCore.runClasses(IgnoreTestClass.class);

for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}

System.out.println(“Test RunCount : ” + result.getRunCount());

}
}

If you run the IgnoreClassTestRunner class, you will get the output as below.

ignore-test-class-in-junit.PNG

Hope it helps you!!

Cheers 🙂

Advertisements

#ignore-annotation, #ignore-test, #ignore-test-class-in-junit, #ignore-test-method-in-junit