如果我有多个必须在链中的@Test方法,并且我想使用DataProvider为整个测试场景传递更多数据,那么正确的方法是什么。
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestClass {
private int type = -1;
@DataProvider
public Object[][] mock() {
return new Object[][] { { 1, "Hello" }, { 2, "World" } };
}
@Test(groups = { "A" }, priority = 0, dataProvider = "mock")
public void testOne(int i, String name) {
System.out.println("TestClass.testOne()");
switch (i) {
case 1:
type = 1;
Assert.assertEquals(name, "Hello");
break;
case 2:
type = 2;
Assert.assertEquals(name, "World");
break;
default:
break;
}
}
@Test(groups = { "A" }, priority = 1)
public void testTwo() {
Assert.assertEquals(type != -1, true);
System.out.println("TestClass.testTwo()");
}
}
每次使用模拟数据调用testOne之后,还必须调用testTwo: Resutlt - > TestClass.testOne() TestClass.testOne() TestClass.testTwo()
预期结果 - > TestClass.testOne() TestClass.testTwo() TestClass.testOne() TestClass.testTwo()
这是我的套件xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" group-by-instances="true">
<test name="Test" verbose="3" group-by-instances="true" >
<classes>
<class name="demo.test.src.TestClass">
</class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
答案 0 :(得分:1)
你基本上需要一个工厂和一个数据提供者来完成这项工作。
以下是它的外观示例:
public class TestClass {
private int type = - 1;
private String name;
@Factory (dataProvider = "mock")
public TestClass(int type, String name) {
this.type = type;
this.name = name;
}
@DataProvider
public static Object[][] mock() {
return new Object[][] {{1, "Hello"}, {2, "World"}};
}
@Test (groups = {"A"})
public void testOne() {
System.out.println("TestClass.testOne()");
switch (type) {
case 1:
type = 1;
Assert.assertEquals(name, "Hello");
break;
case 2:
type = 2;
Assert.assertEquals(name, "World");
break;
default:
break;
}
}
@Test (groups = {"A"}, dependsOnMethods = "testOne")
public void testTwo() {
Assert.assertEquals(type != - 1, true);
System.out.println("TestClass.testTwo()");
}
}
然后,您可以使用利用group-by-instances
属性的TestNG套件xml文件(示例如下所示)
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="factory-suite" verbose="2" group-by-instances="true" >
<test name="factory-test">
<classes>
<class name="org.rationale.emotions.TestClass"/>
</classes>
</test>
</suite>