JUnit – Ignore a Test

In JUnit, to ignore a test, just add a @Ignore annotation before or after the @Test method.

P.S Tested with JUnit 4.12

IgnoreTest.java

package com.mkyong;

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

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class IgnoreTest {

    @Test
    public void testMath1() {
        assertThat(1 + 1, is(2));
    }

    @Ignore
    @Test
    public void testMath2() {
        assertThat(1 + 2, is(5));
    }

    @Ignore("some one please create a test for Math3!")
    @Test
    public void testMath3() {
        //...
    }

}

In the above example, both testMath2() and testMath3() will be ignored.

FAQS

1. To ignore a test, why not just comment the test methods or @Test annotation?
A : The test runner will not report the test. In IDE, the test runner will display the ignored tests with different icon or color, and highlight it, so that you know what tests are ignored.

2. Why make a test that doesn’t test?
A : For large project, many developers are handling different modules, the failed test may caused by other teams, you can add @Ignore on the test method to avoid the test to break the entire build process.

A : Or you want someone to help to create the test, like @Ignore ("help for this method!"), the optional parameter(String) will be displayed in the test runner.

References

  1. JUnit – Ignore JavaDoc

9 comments on “JUnit – Ignore a Test

  1. I’ve been absent for a while, but now I remember why I used to love this website. Thanks, I’ll try and check back more frequently. How frequently you update your site? befeacdebgkgbabb

  2. Sorry, but when I run java file on Junit test. It test the function even if I add @ignore annotation. so, what do u think problem is ?

  3. Hey buddy, just came across your website. Appreciate your efforts. I will come back again to learn things here. Keep up the good work.

    1. There can be several reason, for example when project members are all around the world and you need some help completing the test, you want to commit it to version system, but do not want to break build process for others…

Leave a Comment

Your email address will not be published. Required fields are marked *