重新运行TestNG测试会导致测试永久运行

时间:2016-07-28 15:00:02

标签: java testng

我有这样的测试:

public class Test1 extends AbstractTest{
@Test(retryAnalyzer=Retry.class)
public void test(){
    System.out.println(this.getClass().getName() + " running");
    Assert.fail();
}

我有一个像这样的重试类:

public class Retry implements IRetryAnalyzer{
    private int retryCount = 0;
    private int maxRetryCount = 3;

    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;
        }
        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;
    }
}

当我运行测试时,它会无限重复,将其作为控制台输出:

tests.Test1 running
Retrying test test with status FAILURE for the 1 time(s).
tests.Test1 running
Retrying test test with status FAILURE for the 2 time(s).
Retrying test test with status FAILURE for the 3 time(s).
tests.Test1 running
tests.Test1 running
tests.Test1 running
tests.Test1 running
tests.Test1 running
tests.Test1 running
tests.Test1 running
tests.Test1 running

等等......

为什么第二次无法运行?为什么它会无限期地运行?如何使其具有所需的运行行为,然后重试最多3次或多次将MaxRetryCount设置为?

1 个答案:

答案 0 :(得分:1)