在没有 Suit 的情况下以编程方式对参数化测试进行单元测试

Unit testing the parametrized test programmatically with out Suite

我确实有一些参数化测试,我想 运行 以编程方式 测试 而不是 annotation processors 例如 @Suite @RunWith(Suite.class) ,有没有一种方法可以让我从其他测试 类 访问和调用测试 类?

@SpringBootTest
class Test {

    @ParameterizedTest
    @ValueSource(strings = {"first", "second"})
    public void example(String values) {
    ...
    }
    
   ...
}

是的,您可以 运行 从这样的代码中测试 类。

JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
junit.run(Test.class);

您可以在此处阅读更多相关信息:https://www.baeldung.com/junit-tests-run-programmatically-from-java

您可以使用 EngineTestKit 为给定的 TestEngine 执行测试计划提供支持,然后通过流畅的 API 访问结果以验证预期结果。

EngineTestKit
            .engine("junit-jupiter")
            .selectors(selectClass(Test.class))
            .execute().testEvents().assertStatistics(o -> o.failed(1));