gpt4 book ai didi

java - 将 TestNG dataProvider 与 dependentOnGroups 结合使用来设置执行顺序

转载 作者:行者123 更新时间:2023-12-02 09:29:30 25 4
gpt4 key购买 nike

我有几个相互依赖的测试用例。执行顺序可以在 TestNG 中使用 groups 定义。和 dependsOnGroups@Test注释中:

public class MyTest {
@Test(groups = { "group1" })
public void testCase1() {
}

@Test(groups = {"group2"}, dependsOnGroups = { "group1" })
public void testCase2() {
}

@Test(groups = {"group2"}, dependsOnGroups = { "group1" })
public void testCase() {
}

@Test(groups = { "group3" }, dependsOnGroups = { "group2" })
public void testCase4() {
}
}

但我希望仅使用一个 @Test 方法和一个 @DataProvider 动态地为我提供组和依赖项。

public class MyTest {

@Test(dataProvider = "test-cases", groups = {testCase[1]}, dependsOnGroups = {testCase[2]})
public void executeTest(TestCase testCase) throws Exception {
}

@DataProvider(name = "test-cases", parallel = true)
public Object[][] getTestCases() {
Object[][] testdata = new Object[...][...];
// Fields: test name, group, depends on, test data
testdata[0] = {"TC#1", "group1", "", "foo data"};
testdata[1] = {"TC#2", "group2", "group1", "bar data"};
testdata[2] = {"TC#3", "group2", "group1", "bzz data"};
testdata[3] = {"TC#4", "group3", "group2", "frr data"};
return testdata;
}

}

不幸的是,这种方法不起作用,因为无法从 @Test(..., groups = {testCase[1]}, ...) 注释访问 testCase[1]。

是否有另一种方法可以使数据提供者驱动的测试按固定顺序进行?

最佳答案

根据您的评论和编辑,我认为您希望将数据获取到所有方法中,并按照它们在类中出现的顺序运行它们。

您可以使用以下方法和 testng.xml 以及 preserve-order="true" 来运行数据驱动测试。请注意,您可能必须删除 groupsdependsOnGroups 才能运行此程序,如下所示。

public class MyTest {

@Test(dataProvider = "test-cases")
public void testCase1(Object[][] obj) {
//access obj to get data
}

@Test(dataProvider = "test-cases")
public void testCase2(Object[][] obj1) {
//access obj1 to get data
}

@Test(dataProvider = "test-cases")
public void testCase(Object[][] obj2) {
//access obj2 to get data

}

@Test(dataProvider = "test-cases")
public void testCase4(Object[][] obj3) {
//access obj3 to get data
}
}

这是您的 testng.xml,应按照它们出现的顺序运行它

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="yourSuiteName" >

<test name="FirstScenario_1" preserve-order="true">

<classes>
<class name="yourPackage.MyTest" >
<methods>
<include name="testCase1" />
<include name="testCase2" />
<include name="testCase" />
<include name="testCase4" />
</methods>
</classes>
</test>
</suite>

关于java - 将 TestNG dataProvider 与 dependentOnGroups 结合使用来设置执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58098456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com