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: