2016-11-12 47 views
0

我正在尝试在JUnit 5木星中运行黄瓜功能。我已经从Cucumber-jvm源代码中提取了一些代码,并将其改编为JUnit 5的TestFactory。它的工作:我看到我的功能运行时,我运行所有的JUnit测试(这是科特林代码,但同样适用于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的报告说,每一个功能是成功的,它是否实际上是。功能失败时,结果将正确打印,但会生成DynamicTestgradle test和Intellij都没有注意到这个错误:我必须检查文本输出。

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

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

+0

您可以使用挂接之后,脚本对象传递给它。场景类具有getStatus(),它返回传递,失败,未定义,跳过,挂起或返回布尔值的isFailed()。 – Grasshopper

+0

也许[这个问题](https://stackoverflow.com/questions/35550386/cucumber-jvm-hooks-when-scenario-is-passed-or-failed/35553304#35553304)可以帮助你。 – troig

+1

你的例子中记者是什么类型的?我还没有深入研究过Junit5,但是对于junit4集成,这应该是一个'JunitReporter',它将信息转发给junit'RunNotifier'。 –

回答

1

为了将黄瓜场景的结果记录为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; 
    } 
});