環境: JDK 1.6、surefire プラグイン 2.9、jUnit 4.8.1、Maven 3.0、3.0.3、2.2.1。
このテストクラスを作成しました:
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
@Ignore
public class IgnoreTest {
@BeforeClass
public static void beforeClass() {
System.out.println("BEFORE CLASS");
}
@AfterClass
public static void afterClass() {
System.out.println("AFTER CLASS");
}
@Test
public void test1() throws Exception {
System.out.println("test1");
}
@Test
public void test2() throws Exception {
System.out.println("test2");
}
@Test
public void test3() throws Exception {
System.out.println("test3");
}
}
次にmvn clean test
、これを印刷します。
Running hu.palacsint.stackoverflow.q7535177.IgnoreTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.015 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 1
あなたが期待したように動作します。を削除して再度@Ignore
実行すると、次のように出力されます。mvn clean test
Running hu.palacsint.stackoverflow.q7535177.IgnoreTest
BEFORE CLASS
test2
test1
test3
AFTER CLASS
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.045 sec
Results :
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
したがって、3 つの異なる Maven バージョンで動作します。いいえ@BeforeClass
/はd クラス@AfterClass
で実行されました。@Ignore
@BeforeClass
/@AfterClass
メソッドが@Ignore
d テスト クラスで実行できる状況が 1 つ (おそらくそれ以上) あります。無視されたクラスに無視されていないサブクラスがある場合です。
import org.junit.Test;
public class IgnoreSubTest extends IgnoreTest {
@Test
public void test4() throws Exception {
System.out.println("test4 subclass");
}
}
の結果mvn clean test
:
Running hu.palacsint.stackoverflow.q7535177.IgnoreTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.047 sec
Running hu.palacsint.stackoverflow.q7535177.IgnoreSubTest
BEFORE CLASS
test4 subclass
test1
test2
test3
AFTER CLASS
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.057 sec
Results :
Tests run: 5, Failures: 0, Errors: 0, Skipped: 1
この場合、@BeforeClass
および@AfterClass
メソッドはIgnoreSubTest
テスト クラスのメソッドであるため実行されます。