在第一个测试失败时中断测试类。

huangapple go评论71阅读模式
英文:

Interrupt test class at the first test failure

问题

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

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

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

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

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.

这是在Spock中可以通过使用@Stepwise注解实现的行为。我不知道在JUnit 5中如何实现这一点。

Edit: added sample test

编辑:添加了示例测试

@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());
    }
}

Gives the following result:

产生以下结果:

1596054675044 test 1 - ok
1596054675075 test 2

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

1596054675111 test 3 - ok
1596054675115 test 4 - ok

(Note: The code you provided contains HTML encoded characters, which I've left as is in the translation.)

(注意:您提供的代码包含HTML编码字符,我在翻译中保留了原样。)

英文:

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.

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.

Edit: added sample test

@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());
    }
}

Gives the following result:

1596054675044 test 1 - ok
1596054675075 test 2

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

1596054675111 test 3 - ok
1596054675115 test 4 - ok

答案1

得分: 1

然而,所有的基础设施都已经存在,可以实现自己的扩展,类似于问题评论中的一个。

这正如预期的那样运行。

英文:

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

That works exactly as intended.

答案2

得分: 0

你可以在每个步骤中使用断言来实现这一点,因为当断言失败时,JUnit会停止执行过程。

英文:

You can achieve that using an assert in each step , because JUnit stops it's execution process when an assert failed.

答案3

得分: -1

如果您希望JUnit引擎在其中一个@Test方法失败后立即停止运行其他方法,则不可能

JUnit引擎会取您的测试夹具(测试类)并为每个@Test方法实例化一个对象,并注意(!),这些@Test方法的执行对您来说是不可预测的。因此,即使一个@Test失败,JUnit仍然需要测试其他@Test方法,它会继续执行。

>从这个角度来看:如果JUnit在第一个失败后立即停止,那么如何可能测试您软件中的其他单元失败了呢?假设您有1000个@Test方法,第二个失败了,您不会有兴趣测试其他998个单元吗?

英文:

If you want JUnit engine to stop running other @Test methods as soon as one of those fails, then it's not possible.

JUnit Engine takes your fixture (Test class), and instantiates its new object per each @Test method, and note(!), that execution of those @Test methods are unpredictable to you. So, even if one @Test fails, JUnit needs to test other @Test methods and it will do so.

>Think about it from this perspective: If JUnit were to stop right after first failure, then how would it be possible to test what other units of your software are failing? say you have 1000 @Test methods, and 2nd fails, aren't you interested to test other 998 units?

huangapple
  • 本文由 发表于 2020年7月29日 23:11:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63156759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定