在 TestNG/Selenium 中自动重启失败的测试用例 [英] Restart failed test case automatically in TestNG/Selenium
问题描述
我在 Java 中使用 Selenium webdriver 和 TestNG 来运行 X 数量的测试用例.
我想要的是,任何测试用例一旦失败就自动重启(无论是从启动还是从故障点).
我知道TestNG框架有以下方法
@Override公共无效 onTestFailure(ITestResult tr) {日志(F");}
但我不知道如何找出它是哪个测试用例,然后我将如何重新启动它.
我想看一个包含实际代码的示例,在这里找到了:立即使用 TestNg 重新开始测试>
观察以下测试如何在失败发生后立即重新运行一次.
import org.testng.Assert;导入 org.testng.IRetryAnalyzer;导入 org.testng.ITestResult;导入 org.testng.annotations.Test;公共类重试实现 IRetryAnalyzer {私有 int retryCount = 0;私有 int maxRetryCount = 1;公共布尔重试(ITestResult 结果){如果 (retryCount < maxRetryCount) {重试计数++;返回真;}返回假;}@Test(retryAnalyzer = Retry.class)公共无效 testGenX() {Assert.assertEquals("james", "JamesFail");//监听器测试失败}@Test(retryAnalyzer = Retry.class)公共无效 testGenY() {Assert.assertEquals("你好", "世界");//监听器测试失败}}
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.
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
}
}
这篇关于在 TestNG/Selenium 中自动重启失败的测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!