gpt4 book ai didi

java - 如何获得 Cucumber 特征的结果

转载 作者:行者123 更新时间:2023-12-01 09:15:43 25 4
gpt4 key购买 nike

我正在尝试在 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;
}
});

关于java - 如何获得 Cucumber 特征的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40558672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com