JUnit - Ignorer un test

JUnit - Ignorer un test

Dans JUnit, pour ignorer un test, ajoutez simplement une annotation@Ignore avant ou après la méthode@Test.

P.S Tested with JUnit 4.12

IgnoreTest.java

package com.example;

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() {
        //...
    }

}

Dans l'exemple ci-dessus,testMath2() ettestMath3() seront ignorés.

FAQS

1. Pour ignorer un test, pourquoi ne pas simplement commenter les méthodes de test ou l'annotation@Test?
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. Pourquoi faire un test qui ne le teste pas?
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.

R: Ou vous voulez que quelqu'un vous aide à créer le test, comme@Ignore ("help for this method!"), le paramètre facultatif (String) sera affiché dans le testeur.