我正在嘗試在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的報告說,每一個功能是成功的,它是否實際上是。功能失敗時,結果將正確打印,但會生成DynamicTest
。 gradle test
和Intellij都沒有注意到這個錯誤:我必須檢查文本輸出。
我想我必須弄清楚,在Executable
作爲第二個參數傳遞給dynamicTest
時,該功能的結果是什麼,並在適當的時候提出斷言。我該如何確定feature
或feature.gherkinFeature
的結果?
是否有一種方法可以獲取功能中每個場景的結果?或者更好的是,有沒有辦法運行特定的場景,以便我可以爲每個場景創建一個DynamicTest,爲JUnit提供更好的報告粒度?
您可以使用掛接之後,腳本對象傳遞給它。場景類具有getStatus(),它返回傳遞,失敗,未定義,跳過,掛起或返回布爾值的isFailed()。 – Grasshopper
也許[這個問題](https://stackoverflow.com/questions/35550386/cucumber-jvm-hooks-when-scenario-is-passed-or-failed/35553304#35553304)可以幫助你。 – troig
你的例子中記者是什麼類型的?我還沒有深入研究過Junit5,但是對於junit4集成,這應該是一個'JunitReporter',它將信息轉發給junit'RunNotifier'。 –