如果特定测试失败,则停止JUnit套件
我有一个JUnit测试套件的forms:
@RunWith(Suite.class) @Suite.SuiteClasses( { xx.class, yy.cass }) public class AllTests { public static Test suite() { TestSuite suite = new TestSuite(AllTests.class.getName()); //$JUnit-BEGIN$ //$JUnit-END$ return suite; } }
然后调用这样的vanilla测试:
public class xxx { @Test public void test () throws { ...
我有一种情况,如果在第一次测试中出现错误或失败,我想停止运行测试套件的其余部分。 但是其他错误/失败是可以的,套件应尽可能多地完成其他测试。 基本上,第一次测试失败将表明运行其余测试是不安全的。
这可能吗?
调用System.exit()
什么问题?
首先你需要junit RunListener:
import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunListener; import org.junit.runner.notification.RunNotifier; public class FailureListener extends RunListener { private RunNotifier runNotifier; public FailureListener(RunNotifier runNotifier) { super(); this.runNotifier=runNotifier; } @Override public void testFailure(Failure failure) throws Exception { super.testFailure(failure); this.runNotifier.pleaseStop(); } }
然后准备套房:
public class StopOnFailureSuite extends Suite { public StopOnFailureSuite(Class> klass, Class>[] suiteClasses) throws InitializationError { super(klass, suiteClasses); } public StopOnFailureSuite(Class> klass) throws InitializationError { super(klass, klass.getAnnotation(SuiteClasses.class).value()); } @Override public void run(RunNotifier runNotifier) { runNotifier.addListener(new FailureListener(runNotifier)); super.run(runNotifier); } }
并运行你的套房:
@RunWith(StopOnFailureSuite.class) @Suite.SuiteClasses({ FirstTestClass.class, SecondTestClass.class, ... })
如果是第一次测试,则考虑将其validation移至@BeforeClass并在失败时抛出exception。 然后只有@AfterClass方法才会出现此exception。
当然,这样您就缺少在测试设置方法中创建的所有夹具工件。
基于Hiro2k的答案(谢谢!)我使用了以下解决方案。 这有点像黑客但它的确有效。
可以阻止其他测试运行的测试位于@ Suite.SuiteClasses列表的顶部。 那个测试有以下几点:
private static boolean shouldStopRestOfSuite = false; @Test public void test () throws Throwable { try { ... run some test code... } catch (Throwable e) { shouldStopRestOfSuite = true; throw e; } }
注意上面的确需要捕获Throwable(不是exception),因此它会捕获断言错误。 它还重新抛出错误,以便由JUnit记录以进行分析。
然后还有另一种测试方法:
@Test public void testKillIfNeeded () throws Exception { if (!shouldStopRestOfSuite) { return; } System.out.println ("Test suite killed due to dangerous error / failure"); System.exit(1); }
以上是第二次运行并将终止JUnit进程。
使用此方法,如果出现问题,JUnit测试将不会以失败/错误结束,但会记录失败/错误以供JUnit分析,并且不会运行其他测试。
不太漂亮,但它做的工作:)
就像你的答案,但在集成测试中使用@Before
,我做了类似这样的事情:
public class FooTest { private static boolean bar; @BeforeClass public static void setUpBeforeClass() throws Exception { bar = false; } @Before public void setUp() throws Exception { assertTrue(bar); } @Test public void test() { System.out.println("something"); assertTrue(true); } @Test public void test1() { System.out.println("Something2"); assertTrue(true); } }
问候!
首先,您应该在运行第二次测试之前捕获错误并检查相同的错误。
@Rule public ErrorCollector collector = new ErrorCollector();
1.添加错误。
collector.addError(new Throwable("first thing went wrong"));
2.在依赖运行之前检查 。
collector.checkThat(getResult(), not(containsString("ERROR!")));
参考 – ErrorCollector
你在用ant运行测试吗?
您可以编写自定义测试侦听器。 您可以在ant http://ant.apache.org/manual/Tasks/junit.html(enableTestListenerEvents )中设置此项。