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
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.
Hey buddy, just came across your website. Appreciate your efforts. I will come back again to learn things here. Keep up the good work.
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
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 ?
@Ignore is added since v4.0, please upgrade your JUnit.
Forgive me, but it seems weird to have this? Why make a test that doesn’t test?
Thanks @Betlista. Also refer to the FAQs above.
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…
Wow very useful..
Thanks For You Article, Very Interesting I would often visit here.