Restart failed test case automatically in TestNG/Selenium

冷暖自知 提交于 2019-11-30 14:01:59

I wanted to see an example with actual code in it and found it here: Restarting Test immediately with TestNg

Observe how the below tests will each be re-run once as soon as the failure happens.

import org.testng.Assert;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
import org.testng.annotations.Test;

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

    public boolean retry(ITestResult result) {

        if (retryCount < maxRetryCount) {
            retryCount++;
            return true;
        }
        return false;
    }

    @Test(retryAnalyzer = Retry.class)
    public void testGenX() {
        Assert.assertEquals("james", "JamesFail"); // ListenerTest fails
    }

    @Test(retryAnalyzer = Retry.class)
    public void testGenY() {
        Assert.assertEquals("hello", "World"); // ListenerTest fails

    }
}

From testng.org

Every time tests fail in a suite, TestNG creates a file called testng-failed.xml in the output directory. This XML file contains the necessary information to rerun only these methods that failed, allowing you to quickly reproduce the failures without having to run the entirety of your tests.

If you want to rerun the test exactly after the failure you need to call the method that failed. You can get that method name from ITestResult object.

If you want to rerun all the failed test cases together, then you can give the testng-failed.xml as input xml after the first execution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!