在 TestNG/Selenium 中自动重启失败的测试用例

发布于 2024-12-24 03:14:10 字数 273 浏览 2 评论 0原文

我正在使用 Selenium webdriver,在 Java 中与 TestNG 一起运行 X 数量的测试用例。

我希望任何测试用例一旦失败就自动重新启动(无论是从开始还是从故障点)。

我知道 TestNG 框架有以下方法

@Override
  public void onTestFailure(ITestResult tr) {
    log("F");
  }

,但我不知道如何找出它是哪个测试用例,然后如何重新启动它。

I am using Selenium webdriver, in Java with TestNG to run an X amount of test cases.

What I would like, is for any test case to automatically restart (either from starting or from point of failure), as soon as it fails.

I know TestNG framework has the following method

@Override
  public void onTestFailure(ITestResult tr) {
    log("F");
  }

but I do not know how to find out which testcase it was and then how would I restart it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

予囚 2024-12-31 03:14:10

我想查看一个包含实际代码的示例,并在这里找到了它:
使用 TestNg 立即重新启动测试

观察如何一旦发生故障,以下测试将立即重新运行一次。

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

    }
}

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

    }
}
横笛休吹塞上声 2024-12-31 03:14:10

来自 testng.org

每次测试在套件中失败时,TestNG 都会在输出目录中创建一个名为 testng-failed.xml 的文件。此 XML 文件包含仅重新运行这些失败的方法所需的信息,使您能够快速重现失败,而无需运行整个测试。

如果您想在失败后重新运行测试,则需要调用失败的方法。您可以从 ITestResult 对象获取该方法名称。

如果您想一起重新运行所有失败的测试用例,那么您可以在第一次执行后将 testng-failed.xml 作为输入 xml。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文