有没有一种方法可以根据通过/失败设置TestNG depenency



给定以下testNG测试设置:

package testCases;
import org.testng.annotations.Test;
import framework.utilities.TestBase;
public class Scratch extends TestBase {
@Test()
public void test1() {
// creating a web page
}

@Test(dependsOnMethods = {"test1"})
public void test2() {
// populating the web page with information
}


@Test(dependsOnMethods = {"test2"})
public void test3() {
// updating the web pages information with new information
}


@Test(dependsOnMethods = {"test3"})
public void test4() {
// deleting the web page
}
}

上述设置是一个简单的CRUD测试流程,通过selenium和testNG在我们的网页中自动化功能,每个测试都有一个特定的目的,在每个测试方法的注释中都有明确的说明。

我想解决以下情况,假设test1通过,而test2失败。我想重新尝试运行test2,但是如果test1已经通过,那么只运行test2。如果test1失败,则在test2之前重新运行test1

我的想法是,如果test2失败,并且我们确实有成功创建的网页,那么与其浪费时间重新运行test1,不如重新运行test2。然而,在当前的实现中,testNG将重新运行test1,而不考虑重试。

我考虑过的两种解决方案是做以下一种:

  • 在重新尝试之前,清除任何测试生成的所有数据。因此,它将重新运行test1 是有意义的

  • 每次创建数据时都要使生成的数据唯一,这样在test1重新运行时就不会遇到问题。

然而,添加逻辑以理解这两种解决方案需要一些时间,我想知道我是否有作弊的方法不必这么做?

您可以创建一个RetryAnalyzer类,如下所示:

public class RetryAnalyzer implements IRetryAnalyzer {
private String name = ""; // last failed executed test.
private int count = 0; // number of failures
private static final int MAX_LIMIT = 5;
@Override
public boolean retry(ITestResult result) {
String testName = result.getName();
if(!testName.equals(name)) {
// new test
count = 0;
name = testName;    
}
if(count < MAX_LIMIT) {
count++;
return true;
}
// max limit reached
return false;
}
}

当前testNG不支持在类级别指定retryAnalyzer。(可能有最新/未来版本(。因此,目前我正在将retryAnalyzer属性添加到每个类中。

public class MyTest {
@Test(retryAnalyzer = RetryAnalyzer.class)
public void test1() {
// creating a web page
}

@Test(dependsOnMethods = {"test1"}, retryAnalyzer = RetryAnalyzer.class)
public void test2() {
// populating the web page with information
}

@Test(dependsOnMethods = {"test2"}, retryAnalyzer = RetryAnalyzer.class)
public void test3() {
// updating the web pages information with new information
}   

@Test(dependsOnMethods = {"test3"}, retryAnalyzer = RetryAnalyzer.class)
public void test4() {
// deleting the web page
}
}

这里,如果test1通过并且test2失败,则将NOT重新运行。只有test2会被重试。如果test2在达到指定的最大限制之前通过,则test3将被执行,依此类推

通过的测试将只运行一次。如果任何测试在达到限制之前没有通过,那么相关测试将被标记为跳过。

最新更新