环境: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
所以,它适用于我的三个不同的 Maven 版本。没有@BeforeClass
/@AfterClass
在@Ignore
d 类中运行。
@BeforeClass
当/@AfterClass
方法可以在@Ignore
d 测试类中运行时,有一种(可能更多)情况。当你的被忽略的类有一个未被忽略的子类时:
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
and@AfterClass
方法运行,因为它们是IgnoreSubTest
测试类的方法。