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

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

9 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
AC
13 years ago

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

Smithc87
8 years ago

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

CoderMan
12 years ago

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 ?

mkyong
10 years ago
Reply to  CoderMan

@Ignore is added since v4.0, please upgrade your JUnit.

13 years ago

Forgive me, but it seems weird to have this? Why make a test that doesn’t test?

mkyong
10 years ago

Thanks @Betlista. Also refer to the FAQs above.

Betlista
12 years ago

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…

Ramakrishna Chandragiri
14 years ago

Wow very useful..

Satya
15 years ago

Thanks For You Article, Very Interesting I would often visit here.