在测试场景中使用 DataProvider
Use DataProvider in test scenario
如果我有多个必须在链中的@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:
结果 ->
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 -->
您基本上需要一个工厂和一个数据提供者来完成这项工作。
这是一个示例:
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>
如果我有多个必须在链中的@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: 结果 -> 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 -->
您基本上需要一个工厂和一个数据提供者来完成这项工作。
这是一个示例:
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>