我如何得到一个黄瓜功能的结果

我正在尝试在JUnit 5 Jupiter中运行Cucumber功能。 我已经从Cucumber-jvm源代码中提取了一些代码,并将其用于JUnit 5的TestFactory 。 它正在工作:当我运行所有JUnit测试(这是Kotlin代码,但同样适用于Java)时,我看到我的功能正在运行:

 @CucumberOptions( plugin = arrayOf("pretty"), features = arrayOf("classpath:features") ) class Behaviours { @TestFactory fun loadCucumberTests() : Collection<DynamicTest> { val options = RuntimeOptionsFactory(Behaviours::class.java).create() val classLoader = Behaviours::class.java.classLoader val resourceLoader = MultiLoader(classLoader) val classFinder = ResourceLoaderClassFinder(resourceLoader, classLoader) val runtime = Runtime(resourceLoader, classFinder, classLoader, options) val cucumberFeatures = options.cucumberFeatures(resourceLoader) return cucumberFeatures.map<CucumberFeature, DynamicTest> { feature -> dynamicTest(feature.gherkinFeature.name) { var reporter = options.reporter(classLoader) feature.run(options.formatter(classLoader), reporter, runtime) } } } } 

但是,JUnit报告说每个功能都是成功的,不管它是否真的是成功的。 当功能失败时,结果将正确打印,但生成的DynamicTest会通过。 gradle test和Intellij都没有注意到这个错误:我必须检查文本输出。

我想我必须弄清楚,在作为第二个参数传递给dynamicTestExecutable ,该特性的结果是什么,并在适当的时候引发一个断言。 如何确定featurefeature.gherkinFeature在这一点的结果?

有没有一种方法来获得功能中的每个场景的结果? 或者更好,是否有办法运行一个特定的场景,以便我可以为每个场景创建一个DynamicTest,从而在JUnit中提供更好的报告粒度?

为了将一个Cucumber场景的结果记录为一个JUnit5,我发现实现一个JunitLambdaReporter是最简单的,它实际上是现有JunitReporter的一个简单版本。 一旦你有一个记者记得当前的场景是什么,那么你可以创建一个使用这个逻辑的@TestFactory

 return dynamicTest(currentScenario.getName(), () -> { featureElement.run(formatter, reporter, runtime); Result result = reporter.getResult(currentScenario); // If the scenario is skipped, then the test is aborted (neither passes nor fails). Assumptions.assumeFalse(Result.SKIPPED == result); Throwable error = result.getError(); if (error != null) { throw error; } });