如何获得黄瓜功能的结果

时间:2016-11-12 01:36:41

标签: java cucumber kotlin cucumber-jvm junit5

我正在尝试在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都没有注意到错误:我必须检查文本输出。

我想我必须弄清楚,Executable作为第二个参数传递给dynamicTest,该功能的结果是什么,并在适当的时候提出一个断言。如何确定此时featurefeature.gherkinFeature的结果?

有没有办法获得该功能中每个场景的结果?或者更好的是,有没有办法运行特定的场景,这样我就可以为每个场景创建一个DynamicTest,在JUnit中为我提供更好的报表粒度?

1 个答案:

答案 0 :(得分:1)

为了将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;
  }
});
相关问题