2017-03-15 5 views
0

私はdataproviderメソッドとテストメソッド(テストメソッドでITestContextパラメータを使用)を使用しています。次のように簡略化した例である。テストメソッドでITestContextを使用してTestNGを使用してテストケースを再試行する

@DataProvider(name="Dataprovider") 
public Object[][] dataprovider(){ 

    return new Object[][]{{1},{2,},{3}}; 
} 

@Test(dataProvider="Dataprovider") 
public void test(int data, ITestContext itx){ 

    System.out.println(data); 
    org.testng.Assert.assertEquals(data, 3); 
} 

マイリトライクラスとRetryListenerクラスは以下の通りである:テストが失敗した場合、リトライはTestNGのによって呼び出され、その後:期待

public class RetryListener implements IAnnotationTransformer { 

@Override 
public void transform(ITestAnnotation testannotation, Class testClass, 
     Constructor testConstructor, Method testMethod) { 

    IRetryAnalyzer retry = testannotation.getRetryAnalyzer(); 

    if (retry == null) { 
     testannotation.setRetryAnalyzer(Retry.class); 
    } 
} 
} 

public class Retry implements IRetryAnalyzer { 

private static int retryCount = 0; 
private int maxRetryCount = 1; 

// Below method returns 'true' if the test method has to be retried else 'false' 
//and it takes the 'Result' as parameter of the test method that just ran 
    public boolean retry(ITestResult result) { 
     if (retryCount < maxRetryCount) { 
      System.out.println("Retrying test " + result.getName() + " with status " 
        + getResultStatusName(result.getStatus()) + " for the " + (retryCount+1) + " time(s)."); 
      retryCount++; 
      return true; 
     } 
     retryCount = 0; 
     return false; 
    } 

    public String getResultStatusName(int status) { 
     String resultName = null; 
     if(status==1) 
      resultName = "SUCCESS"; 
     if(status==2) 
      resultName = "FAILURE"; 
     if(status==3) 
      resultName = "SKIP"; 
     return resultName; 
    } 

}

dataproviderは、同じ値をテストメソッドに返す必要があります。

観測:DataProviderに同じ値を返しますが、テストメソッドが実行されず、再試行が終了し、次のテストを開始するには

(新しい値は現在のdataProviderによって返されます)しかし、私の再試行は、試験方法に入りません(テストメソッドの(int data、ITestContext itx)は期待していません。 ITestContextを削除すると、再試行が行われます。

ITestContextは、テストケースのコンテキストを維持するために必要です。 ITestContextをテストメソッドに保持して再試行する方法。

+0

だ(あなたが気付いた場合、私は今、明示的にも、データプロバイダを経由してITestContextに渡しています)あなたのために働くだろうかどうかを確認してください私の再試行はテスト方法には入っていません " - >それはどういう意味ですか? – juherr

+0

再試行が呼び出されると、データプロバイダは同じ値で再度呼び出され、テストメソッドに戻ります。今回はテストメソッドが実行されず、テストは単に再試行され、次のテストに進みます。 –

+0

現在追加のパラメータ 'ITestContext itx'のためにコンパイルされません。 –

答えて

0

データプロバイダが提供する@Testメソッドの中で、ITestContextを明示的なパラメータとして入力するのは難しいと思っています。TestNGは、それを注入する方法を知らないため、TestNGによって注入されると考えていますポジション)。

以下「

import org.testng.IRetryAnalyzer; 
import org.testng.ITestContext; 
import org.testng.ITestResult; 
import org.testng.annotations.DataProvider; 
import org.testng.annotations.Test; 

public class RetryWithDataProvider { 
    @DataProvider(name = "Dataprovider") 
    public Object[][] dataprovider(ITestContext ctx) { 

     return new Object[][]{{1, ctx}, {2, ctx}, {3, ctx}}; 
    } 

    @Test(dataProvider = "Dataprovider", retryAnalyzer = Retry.class) 
    public void test(int data, ITestContext itx) { 
     System.out.println(data); 
     System.out.println("Current Test name is : " + itx.getName()); 
     org.testng.Assert.assertEquals(data, 3); 
    } 

    public static class Retry implements IRetryAnalyzer { 

     private static int retryCount = 0; 
     private int maxRetryCount = 1; 

     public boolean retry(ITestResult result) { 
      if (retryCount < maxRetryCount) { 
       System.out.println("Retrying test " + result.getName() + " with status " 
         + getResultStatusName(result.getStatus()) + " for the " + (retryCount + 1) + " time(s)."); 
       retryCount++; 
       return true; 
      } 
      retryCount = 0; 
      return false; 
     } 

     String getResultStatusName(int status) { 
      String resultName = null; 
      if (status == 1) 
       resultName = "SUCCESS"; 
      if (status == 2) 
       resultName = "FAILURE"; 
      if (status == 3) 
       resultName = "SKIP"; 
      return resultName; 
     } 
    } 
} 

はここに私のコンソール出力

[TestNG] Running: 
    /Users/krmahadevan/Library/Caches/IntelliJIdea2016.3/temp-testng-customsuite.xml 
1 
Retrying test test with status FAILURE for the 1 time(s). 

Test ignored. 
1 

java.lang.AssertionError: expected [3] but found [1] 
Expected :3 
Actual :1 
<Click to see difference> 


    at org.testng.Assert.fail(Assert.java:94) 
    at org.testng.Assert.failNotEquals(Assert.java:513) 
    at org.testng.Assert.assertEqualsImpl(Assert.java:135) 
    at org.testng.Assert.assertEquals(Assert.java:116) 
    at org.testng.Assert.assertEquals(Assert.java:389) 
    at org.testng.Assert.assertEquals(Assert.java:399) 
    at com.rationaleemotions.stackoverflow.RetryWithDataProvider.test(RetryWithDataProvider.java:20) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104) 
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:645) 
    at org.testng.internal.Invoker.retryFailed(Invoker.java:998) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1200) 
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) 
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) 
    at org.testng.TestRunner.privateRun(TestRunner.java:756) 
    at org.testng.TestRunner.run(TestRunner.java:610) 
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:387) 
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382) 
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) 
    at org.testng.SuiteRunner.run(SuiteRunner.java:289) 
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293) 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1218) 
    at org.testng.TestNG.runSuites(TestNG.java:1133) 
    at org.testng.TestNG.run(TestNG.java:1104) 
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72) 
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 

2 
Retrying test test with status FAILURE for the 1 time(s). 

Test ignored. 
2 

java.lang.AssertionError: expected [3] but found [2] 
Expected :3 
Actual :2 
<Click to see difference> 


    at org.testng.Assert.fail(Assert.java:94) 
    at org.testng.Assert.failNotEquals(Assert.java:513) 
    at org.testng.Assert.assertEqualsImpl(Assert.java:135) 
    at org.testng.Assert.assertEquals(Assert.java:116) 
    at org.testng.Assert.assertEquals(Assert.java:389) 
    at org.testng.Assert.assertEquals(Assert.java:399) 
    at com.rationaleemotions.stackoverflow.RetryWithDataProvider.test(RetryWithDataProvider.java:20) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104) 
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:645) 
    at org.testng.internal.Invoker.retryFailed(Invoker.java:998) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1200) 
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) 
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) 
    at org.testng.TestRunner.privateRun(TestRunner.java:756) 
    at org.testng.TestRunner.run(TestRunner.java:610) 
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:387) 
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382) 
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) 
    at org.testng.SuiteRunner.run(SuiteRunner.java:289) 
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293) 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1218) 
    at org.testng.TestNG.runSuites(TestNG.java:1133) 
    at org.testng.TestNG.run(TestNG.java:1104) 
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72) 
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 

3 

=============================================== 
Default Suite 
Total tests run: 5, Failures: 2, Skips: 2 
=============================================== 
+0

応答いただきありがとうございます。うまくいくように見える。 –

関連する問題