简体   繁体   中英

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.

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

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM