简体   繁体   English

java testng重试逻辑

[英]java testng retry logic

I have written a code for retrying a failed test case in selenium web driver and java. 我已经编写了一个代码,用于在Selenium Web驱动程序和Java中重试失败的测试用例。 How should we enhance the script to hold only one record in the test case output even the same test case as been executed multiple times. 我们应该如何增强脚本,使其在测试用例输出中只保留一个记录,即使是多次执行相同的测试用例也是如此。 I don't want the test output report to contain any redundant result entries. 我不希望测试输出报告包含任何多余的结果条目。

Code: Retry Logic : 代码:重试逻辑:

package tests;

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

public class Retry implements IRetryAnalyzer {
    private int retryCount         = 0;
    private int maxRetryCount     = 2;   // retry a failed test 2 additional times

    @Override
    public boolean retry(ITestResult result) {
        if (retryCount <maxRetryCount) {
            retryCount++;
            return true;
        }
        return false;
    }
}

Implementation in the class 在课堂上实施

@Test(retryAnalyzer=Retry.class)
    public void example() throws CustomException
    {
        throw new CustomException("Example");
    }

Please let me know what changes are needed. 请让我知道需要哪些更改。

Thanks and Regards Sushanth.G 感谢和问候Sushanth.G

I had the same problem before. 我之前也遇到过同样的问题。 We used jenkins to run CI, and need to make sure all test case results were SUCCESS (even after some times of retry) then we can deploy build. 我们使用jenkins来运行CI,并且需要确保所有测试用例的结果都为SUCCESS(即使在重试了几次之后),然后才能部署build。

The solution is adding TestListenerAdapter to re-write test result status to SKIP if this test will be run again. 如果此测试将再次运行,则解决方案是添加TestListenerAdapter以将测试结果状态重写为SKIP。

public class MyTestListenerAdapter extends TestListenerAdapter {
    @Override
    public void onTestFailure(ITestResult result) {
        if (result.getMethod().getRetryAnalyzer() != null) {
            MyRetryAnalyzer retryAnalyzer = (MyRetryAnalyzer)result.getMethod().getRetryAnalyzer();

            if(retryAnalyzer.isRetryAvailable()) {
                result.setStatus(ITestResult.SKIP);
            } else {
                result.setStatus(ITestResult.FAILURE);
            }
            Reporter.setCurrentTestResult(result);
        }
    }
}

RetryAnalyzer needs to provide another method (isRetryAvailable() in this example) for TestListenerAdapter. RetryAnalyzer需要为TestListenerAdapter提供另一个方法(在此示例中为isRetryAvailable())。

public class MyRetryAnalyzer implements IRetryAnalyzer {
    private static int MAX_RETRY_COUNT = 3;

    AtomicInteger count = new AtomicInteger(MAX_RETRY_COUNT);

    public boolean isRetryAvailable() {
        return (count.intValue() > 0);
    }

    @Override
    public boolean retry(ITestResult result) {
        boolean retry = false;
        if (isRetryAvailable()) {
            System.out.println("Going to retry test case: " + result.getMethod() + ", " + (MAX_RETRY_COUNT - count.intValue() + 1) + " out of " + MAX_RETRY_COUNT);
            retry = true;
            count.decrementAndGet();
        }
        return retry;
    }
}

Now we can add this TestListenerAdapter to test class. 现在,我们可以将此TestListenerAdapter添加到测试类。

@Listeners({MyTestListenerAdapter.class})
public class AppTest {
    @Test(retryAnalyzer=MyRetryAnalyzer.class)
    public void testSum(){
        MyClass c = new MyClass();
        Assert.assertEquals(c.sum(2, 3), 5);
    }
}

Please see full example here: https://github.com/haojiwu/testng-retry-example 请在此处查看完整示例: https//github.com/haojiwu/testng-retry-example

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

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