0

While LeanFT uses JUnit for its test runner, it doesn't appear to implement 'TestRule'. This excludes a 'standard' method describe elsewhere.

How to Re-run failed JUnit tests immediately?

Anyone have a solution to this?

not-bob
  • 815
  • 1
  • 8
  • 23
  • Hmm how should it be supported from your pov? You mean in the automatically generated report to have separate Test nodes created? One for each retry attempt? – Adelin Oct 23 '17 at 07:44
  • At a minimum, the last test results should be available because either all retries have failed (presumably for the same cause) or it ultimately succeeded. The TestNG emailable report format shows the output from all tests and marks the failed tests as 'skipped' and the final test as passed or failed. – not-bob Oct 24 '17 at 13:02

1 Answers1

1

It seems that you refer to the fact that the report doesn't have the test result. Indeed it seems it's no longer automatically reported when using TestRule.

However, you can manually report whatever you want to report.

Here's an example of Junit test that reports what we want it to report.

import com.hp.lft.report.CaptureLevel;
import com.hp.lft.report.ReportLevel;
import com.hp.lft.report.Reporter;
import com.hp.lft.sdk.web.Browser;
import com.hp.lft.sdk.web.BrowserFactory;
import com.hp.lft.sdk.web.BrowserType;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.*;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import unittesting.UnitTestClassBase;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;

public class RetryTest extends UnitTestClassBase {
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        instance = new LeanFtTest();
        globalSetup(LeanFtTest.class);
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        globalTearDown();
    }


    public class Retry implements TestRule {
        private int retryCount;

        public Retry(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.err.println(description.getDisplayName() + ": run " + (i+1) + " failed");
                        }
                    }
                    System.err.println(description.getDisplayName() + ": giving up after " + retryCount + " failures");
                    throw caughtThrowable;
                }
            };
        }
    }

    @Rule
    public Retry retry = new Retry(3);

    @Test
    public void test2() throws  Exception{
        Reporter.startReportingContext("Reporting for test2");
        Reporter.reportEvent("Reporting", "Reporting stuff", Status.Passed);
        Reporter.reportEvent("Reporting", "Reporting some more stuff", Status.Failed);
        Reporter.endReportingContext();
        Object o = null;
        o.equals("foo");
    }
}

And this is how it looks like: enter image description here

Adelin
  • 7,809
  • 5
  • 37
  • 65