第一次测试失败时中断测试类 [英] Interrupt test class at the first test failure

查看:85
本文介绍了第一次测试失败时中断测试类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JUnit 5测试,由按顺序执行的多个步骤组成.这些步骤是在单独的方法中定义的.

I have a JUnit 5 test consisting of multiple steps executing in order. The steps are defined in separate methods.

我希望测试在夹具/类中的第一次失败时停止执行.

I want the tests to stop executing at first failure in the fixture/class.

这是可以通过使用 @Stepwise 批注在Spock中实现的行为.我看不到如何在JUnit 5中完成此操作.

This is behaviour which can be achieved in Spock by using @Stepwise annotation. I don't see how this can be done in JUnit 5.

添加了样本测试

@TestMethodOrder(Alphanumeric.class)
class MainTest {

    @Test void test1() {
        assertTrue(true);
        System.out.printf("%d test 1 - ok%n", System.currentTimeMillis());
    }

    @Test void test2() {
        assertTrue(false);
        System.out.printf("%d test 2 -nok%n", System.currentTimeMillis());
    }

    @Test void test3() {
        assertTrue(true);
        System.out.printf("%d test 3 - ok%n", System.currentTimeMillis());
    }

    @Test void test4() {
        assertTrue(true);
        System.out.printf("%d test 4 - ok%n", System.currentTimeMillis());
    }
}

给出以下结果:

1596054675044 test 1 - ok
1596054675075 test 2

org.opentest4j.AssertionFailedError: 
Expected :true
Actual   :false

1596054675111 test 3 - ok
1596054675115 test 4 - ok

推荐答案

通过添加JUnit5批注似乎无法实现.

It seems impossible to achieve this by adding JUnit5 annotation yet.

但是,所有基础结构都可以实现您自己的扩展,类似于

However, all infrastructure is there to implement your own extension, similar to one in issue comment

这完全符合预期.

这篇关于第一次测试失败时中断测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆