JUnit - Einen Test ignorieren

JUnit - Ignoriere einen Test

Um in JUnit einen Test zu ignorieren, fügen Sie einfach eine@Ignore-Annotation vor oder nach der@Test-Methode hinzu.

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

}

Im obigen Beispiel werden sowohltestMath2() als auchtestMath3() ignoriert.

FAQS

1. Um einen Test zu ignorieren, kommentieren Sie einfach die Testmethoden oder die Annotation von@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. Warum einen Test durchführen, der nicht getestet wird?
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: Oder Sie möchten, dass jemand beim Erstellen des Tests hilft, z. B.@Ignore ("help for this method!"). Der optionale Parameter (String) wird im Testläufer angezeigt.