简体   繁体   English

TestNG:RetryAnalyzer,如果重试成功则跳过依赖组

[英]TestNG: RetryAnalyzer, dependent groups are skipped if a test succeeds upon retry

I have a RetryAnalyzer and RetryListener .我有一个RetryAnalyzerRetryListener In RetryListener onTestFailure , I check if the test is retryable, if yes I set the result to SUCCESS.RetryListener onTestFailure ,我检查测试是否可重试,如果是,则将结果设置为 SUCCESS。 I also do, testResult.getTestContext().getFailedMethods.removeResult(testResult) in this method.我也这样做, testResult.getTestContext().getFailedMethods.removeResult(testResult)在这个方法中。

I again remove failed results (with valid if conditions) in onFinish method in the listener.我再次在侦听器的onFinish方法中删除失败的结果(具有有效的 if 条件)。

Now the problem I am running into is, I made each test class into groups.现在我遇到的问题是,我将每个测试类分组。 One test class does the WRITES and one test class does the READS.一个测试类执行 WRITES,一个测试类执行 READS。 So READs group depends on WRITES.所以 READs 组依赖于 WRITES。

If a test case fails on 1st attempts and succeeds on retrying, then all the test cases in the dependent group are SKIPPED, despite removing failed result in onTestFailure method.如果测试用例在第一次尝试时失败并成功重试,则依赖组中的所有测试用例都将被跳过,尽管在onTestFailure方法中删除了失败的结果。

Is there a way to run dependent method if a test case succeeds on retrying?如果测试用例重试成功,有没有办法运行依赖方法? I am fine with the behavior if the test case fails in all attempts, so I am not looking to add "alwaysRun=true" on each dependent method.如果测试用例在所有尝试中都失败,我对这种行为很好,所以我不希望在每个依赖方法上添加"alwaysRun=true"

On retry you should be removing the test from the Failed tests.重试时,您应该从失败的测试中删除测试。 And plz be sure to remove ITestResult object.并且请务必删除 ITestResult 对象。 (ie, result but not result.getMethod()) (即,结果但不是 result.getMethod())

@Override
public boolean retry(ITestResult result) {
    if (currentCount < maxRetryCount) {
        result.getTestContext().getFailedTests().removeResult(result);
        currentCount++;
        return true;
    }
    return false;
}

I was using TestNG 6.8.7, upgraded it to 6.9.5.我使用的是 TestNG 6.8.7,将其升级到 6.9.5。

After that, upon retry, TestNG was marking test case as SKIPPED.之后,在重试时,TestNG 将测试用例标记为已跳过。 I just had to create a Listener, which implemented TestListenerAdapter and override onTestSkipped, if there are retries available then remove the method from skippedTests.我只需要创建一个监听器,它实现了 TestListenerAdapter 并覆盖 onTestSkipped,如果有可用的重试,则从 skippedTests 中删除该方法。

result.getTestContext().getSkippedTests().removeResult(result.getMethod());

If not set test to FAILURE.如果没有设置测试为失败。 Now it works as expected.现在它按预期工作。

In retry file, add a mechanism to see if retry is left of the case.在重试文件中,添加一个机制来查看重试是否留在案例中。

In Custom Listener, override onTestSkipped() and check if RetryLeft, remove it from skippedResult and return在自定义侦听器中,覆盖 onTestSkipped() 并检查是否 RetryLeft,将其从 skippedResult 中删除并返回

public class Retry implements IRetryAnalyzer {

   private int count = 0;
   private static final List retriedTests = new CopyOnWriteArrayList();
   private static final ConcurrentHashMap<String, Boolean> retriedTestsMap = new ConcurrentHashMap();

   @Override
   public boolean retry(ITestResult iTestResult) {
      int maxTry = 3;
      if (!iTestResult.isSuccess()) { // Check if test not succeed
         String name = getNameForTestResult(iTestResult);
         if (count < maxTry) { // Check if maxTry count is reached
            count++; // Increase the count count by 1
            retriedTests.add(iTestResult);
            retriedTestsMap.put(name, true);
            RestApiUtil.println("**" + name + " retry count " + count + " **");
            iTestResult.setStatus(ITestResult.FAILURE); // Mark test as failed
            return true; // Tells TestNG to re-run the test
         } else {
            iTestResult.setStatus(ITestResult.FAILURE); // If maxCount reached,test marked as failed
            retriedTestsMap.put(name, true);
         }
      } else {
         iTestResult.setStatus(ITestResult.SUCCESS); // If test passes, TestNG marks it as passed
      }
      return false;
   }

   public static List getRetriedTests() {
      return retriedTests;
   }

   public static boolean isRetryLeft(ITestResult tr) {
      return retriedTestsMap.get(getNameForTestResult(tr));
   }

   private static String getNameForTestResult(ITestResult tr) {
      return tr.getTestClass().getRealClass().getSimpleName() + "::" + tr.getName();
   }

}





public class CustomTestNGListener extends TestListenerAdapter {
   @Override
   public void onTestSkipped(ITestResult tr) {
      if (Retry.isRetryLeft(tr)) {
         tr.getTestContext().getSkippedTests().removeResult(tr);
         return;
      }
      super.onTestSkipped(tr);
   }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM