2016-11-12 16 views
0

私はJUnit 5 Jupiterでキュウリの機能を実行しようとしています。私はCucumber-jvmソースからいくつかのコードを取り上げ、JUnit 5のTestFactoryに適合させました。それは働いている:私は(これはKotlinコードですが、同じでは、Javaに適用されます)私は、すべてのJUnitテストを実行したときに、私の機能が実行されている参照してください。キュウリの機能の結果を取得するには

@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もエラーに気づいていません。テキスト出力を検査する必要があります。

私は、dynamicTestに2番目のパラメータとして渡されたExecutableの中で、その結果が何であるかを把握し、適切な場合にアサーションを発生させる必要があると思います。その時点でfeatureまたはfeature.gherkinFeatureの結果を確認するにはどうすればよいですか?

機能の各シナリオの結果を得る方法はありますか?それ以上の場合、特定のシナリオを実行する方法があるので、各シナリオのDynamicTestを作成して、JUnitでレポートの精度を向上させることができますか?

+0

あなたがフックした後使用して、それにシナリオオブジェクトを渡すことができます。シナリオクラスには、渡された、失敗した、未定義の、スキップされた、保留中の、またはブール値を返すisFailed()を返すgetStatus()があります。 – Grasshopper

+0

多分[この質問](https://stackoverflow.com/questions/35550386/cucumber-jvm-hooks-when-scenario-is-passed-or-failed/35553304#35553304)がお手伝いします。 – troig

+1

あなたの例では 'reporter'はどんなタイプですか?私はJunit5をよく見ていませんが、junit4の統合では、これはJunitReporterでなければなりません.Junit4は、情報をjunitのRunNotifierに転送します。 –

答えて

1

キュウリのシナリオの結果をJUnit5として記録するには、JunitLambdaReporterを実装するのが最も簡単であることがわかりました.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; 
    } 
}); 
関連する問題