Abort/ignore JUnit 5 中的参数化测试

Abort/ignore parameterized test in JUnit 5

我有一些参数化测试

@ParameterizedTest
@CsvFileSource(resources = "testData.csv", numLinesToSkip = 1)
public void testExample(String parameter, String anotherParameter) {

    // testing here
}

如果一次执行失败,我想忽略所有后续执行。

据我所知,没有内置机制可以做到这一点。以下确实有效,但有点老套:

@TestInstance(Lifecycle.PER_CLASS)
class Test {

    boolean skipRemaining = false;
    
    @ParameterizedTest
    @CsvFileSource(resources = "testData.csv", numLinesToSkip = 1)
    void test(String parameter, String anotherParameter) {
        Assumptions.assumeFalse(skipRemaining);
        try {
            // testing here
        } catch (AssertionError e) {
            skipRemaining = true;
            throw e;
        }
    }

}

与将测试标记为失败的失败断言相反,假设会导致测试中止。此外,lifecycle 从每个方法切换到每个 class:

When using this mode, a new test instance will be created once per test class. Thus, if your test methods rely on state stored in instance variables, you may need to reset that state in @BeforeEach or @AfterEach methods.

根据您需要该功能的频率,我宁愿使用 custom extension