How to Rerun Failed Tests in JUnit?

该帖转自其他出处

Sometimes due to some temporarily problems such as connection problems, server problems, browser issues, mobile application crashes and freezes and so on our tests fail. In these kinds of situations, we may want to rerun our tests automatically. But how? We can handle this with test frameworks such as TestNG and JUnit. In this post, I want to show you how to solve this problem with JUnit. Also, you can do the same operation with TestNG. It is a great test framework and actually more QA friendly. In another post, I will also explain how to do the same operation with TestNG using several ways. Especially, you can handle many situations with TestNG Listeners and this is another post topic.

In my Junit Rules post, I described how to write Custom Rules and I showed a sample custom ScreenShot Rule implementation.  In this post, we will create a similar custom Rule class which implements TestRule class.  We need to override evaluate() method and write retry logic in it.

Let’s do an example and see how it works?

We need two classes, one of them is our Rule Class ->> RetryRule and the other is our Test Class ->>RetryRuleTest.

In RetryRuleTest class, I will open www.swtestacademy.com and get its title and check it with WRONG expected title. Thus, our test will fail and I will expect that our test rerun according to given retry count argument. I set retry count as 3 in our example.

RetryRule Class:

package junitexamples.junitrules;
 
/**
 * Created by ONUR BASKIRT on 27.03.2016.
 */
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
 
public class RetryRule implements TestRule {
    private int retryCount;
 
    public RetryRule (int retryCount) {
        this.retryCount = retryCount;
    }
 
    public Statement apply(Statement base, Description description) {
        return statement(base, description);
    }
 
    private Statement statement(final Statement base, final Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                Throwable caughtThrowable = null;
 
                // implement retry logic here
                for (int i = 0; i < retryCount; i++) {
                    try {
                        base.evaluate();
                        return;
                    } catch (Throwable t) {
                        caughtThrowable = t;
                        //  System.out.println(": run " + (i+1) + " failed");
                        System.err.println(description.getDisplayName() + ": run " + (i + 1) + " failed.");
                    }
                }
                System.err.println(description.getDisplayName() + ": giving up after " + retryCount + " failures.");
                throw caughtThrowable;
            }
        };
    }
}

RetryRuleTest Class:

package junitexamples.junitrules;
 
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
 
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
 
/**
 * Created by ONUR BASKIRT on 27.03.2016.
 */
public class RetryRuleTest {
 
    static WebDriver driver;
    final private String URL = "http://www.swtestacademy.com";
 
    @BeforeClass
    public static void setupTest(){
        driver = new FirefoxDriver();
    }
 
    //Set retry count argument
    @Rule
    public RetryRule retryRule = new RetryRule(3);
 
    @Test
    public void getURLExample() {
        //Go to www.swtestacademy.com
        driver.get(URL);
 
        //Check title is correct
        assertThat(driver.getTitle(), is("WRONG TITLE"));
    }
}

Result:

转载于:https://www.cnblogs.com/woniu123/p/6839984.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值