【问题标题】:How do I add retry logic to leanft with Junit tests?如何使用 Junit 测试向leaft 添加重试逻辑?
【发布时间】:2017-10-06 14:00:44
【问题描述】:

虽然 LeanFT 使用 JUnit 作为其测试运行程序,但它似乎没有实现“TestRule”。这不包括其他地方描述的“标准”方法。

How to Re-run failed JUnit tests immediately?

谁有办法解决这个问题?

【问题讨论】:

  • 嗯,你的 pov 应该如何支持它?您的意思是在自动生成的报告中创建单独的测试节点?每次重试尝试一次?
  • 至少,最后的测试结果应该是可用的,因为要么所有重试都失败了(可能是由于相同的原因),要么最终成功了。 TestNG 可通过电子邮件发送的报告格式显示所有测试的输出,并将失败的测试标记为“已跳过”,并将最终测试标记为通过或失败。

标签: java junit automated-tests hp-uft leanft


【解决方案1】:

您似乎是指报告没有测试结果的事实。 确实,使用TestRule 时似乎不再自动报告

但是,您可以手动报告您想要报告的任何内容。

这是一个 Junit 测试示例,它报告我们希望它报告的内容。

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

这就是它的样子:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多