2017-04-18 7 views
0

私は単一のtestNG /キュウリランナーを持っています& single testng @Testこれはtestngパラメータとして単一のフィーチャファイルの名前で渡すことができます@Parameterを使用して)実行します。私はランタイムソリューションが欲しい。TestNG&Cucumber:単一フィーチャファイル名をパラメータとして渡します

私は既にTestNGフレームワークを使って書かれた非キュウリのテストをたくさん持っています。私はそこにキュウバーのコードも持っていたいと思います。

誰かが賢明な何かを思い付いていますか?

+0

あなたはMavenを使用していますか? – Grasshopper

+0

Mavenは私たちが探している候補ですが、私は特定のビルドシステムに依存しないソリューションを好むでしょう。私はCuccumber Javaソースを見てきましたが、これを行う方法を見つけた可能性があります。 TestNGCucumberRunnerのv1.2.4を使用して、私はCucumberFeatureオブジェクトのリストにアクセスできます。リストをスキャンして、testngパラメータを使用して入力した文字列と同じ "パス"を持つフィーチャを探します。次に、runCucumberメソッドを使用してランナーに一致する機能を使用することができます。少なくともそれは理論です:-)。私はそれを試してみるつもりです。 –

+0

これは、TestNGCucumberRunnerのgetFeatures()の実装を変更するオプションです。簡単な方法があります。ランナークラスを呼び出すときに、cucumberoptionsタグをオーバーライドすることができます。 https://github.com/cucumber/cucumber-java-skeletonは、「オプションの上書き」を参照してください。 Mavenはこのパラメータを渡す簡単な方法を提供します。これをtestng.xmlにも渡す方法があるはずです。 – Grasshopper

答えて

0

別の技術は、ランナー(インスピレーションのためのいくつかの古い記事のおかげで)インスタンス化する前に、リアルタイムでCucumberOptions注釈を修正するためにリフレクションを使用している:

@Parameters({"featurePath"}) 
@BeforeTest(alwaysRun = true) 
public void setUpTest(@Optional("src/main/java/cucumberFeatureFiles/Testcase.feature") String featurePath) throws Exception { 
    Class<?> testClass = this.getClass(); 
    changeCucumberAnnotation(testClass, "features", new String [] {featurePath});  
    testNGCucumberRunner = new WaltersTestngCucumberRunner(testClass);   
} 

private static void changeCucumberAnnotation(Class<?> clazz, String key, Object newValue) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{ 
    Annotation options = clazz.getAnnotation(CucumberOptions.class);     //get the CucumberOptions annotation 
    InvocationHandler proxyHandler = Proxy.getInvocationHandler(options);    //setup handler so we can update Annotation using reflection. Basically creates a proxy for the Cucumber Options class 
    Field f = proxyHandler.getClass().getDeclaredField("memberValues");    //the annotaton key/values are stored in the memberValues field 
    f.setAccessible(true);                //suppress any access issues when looking at f 
    Map<String, Object> memberValues = (Map<String, Object>) f.get(proxyHandler);  //get the key-value map for the proxy 
    memberValues.remove(key);               //renove the key entry...don't worry, we'll add it back 
    memberValues.put(key,newValue);             //add the new key-value pair. The annotation is now updated. 
}//end method 
1

特徴ファイル名を含むカスタムcucumberoptionsをランナークラスに送信する方法を説明します。これにより、キュウリ試験と非キュウリ試験の両方をtestng.xmlから実行することができます。任意のオプションのオーバーライドが@CucumberOptions注釈のために提供されている場合は、テキストの下

は「キュウリJavaのための」ブックの詳細...


キュウリのチェックに基づいています。上から下にチェックされた後、いずれかが検出された後に停止します。

  1. OS環境変数CUCUMBER_OPTIONS
  2. Javaシステムプロパティcucumber.options
  3. キュウリとJavaのリソースバンドルのcucumber.properties。 optionsプロパティ

オーバーライドで見つかった値は、プラグイン引数を除くすべての値を置き換えます。プラグイン引数が追加されます。上書きされない引数は影響を受けません。あなたはまた、次のように日食に...

セットアップとして実行コンフィギュレーションをAbstractTestNgCucumberTestsが、使用組成物を拡張しないTestNGのキュウリクラスを使用して実行することができます


testng.xml 

<suite name="Default suite">  
    <test name="Cucumber Mix"> 
     <classes> 
      <class name="cucumtestng.test.RunAbstractSampleTest"></class> 
      <class name="cucumtestng.test.NormalTest"></class> 
     </classes> 
    </test> 
</suite> 


@CucumberOptions(features="",glue="cucumtestng.test.stepdefs",snippets=SnippetType.CAMELCASE, 
plugin={"pretty", "html:report", "json:reports.json"}) 
public class RunAbstractSampleTest extends AbstractTestNGCucumberTests { 

} 


public class NormalTest { 
    @Test 
    public void f() { 
     System.out.println("NORMAL TESTNG CLASS"); 
    } 
} 

... enter image description here enter image description here

0

この「セットアップ」コードがトリックを行います。それは私に実行に興味があるキュウリの特徴を私に与える。他の提案も見ていきます。

@Parameters({"featurename"}) 
@BeforeTest(alwaysRun = true) 
public void setUpTest(String featureName) throws Exception { 
    testNGCucumberRunner = new TestNGCucumberRunner(this.getClass()); 
    List<CucumberFeature> featureList = testNGCucumberRunner.getFeatures(); 

    for (CucumberFeature feature : featureList){ 
     if (featureName.equalsIgnoreCase(feature.getPath())){ 
      runFeature = feature; 
      break; 
     } 
    } 
} 
関連する問題