运行 使用 TestNg 中的数据提供程序依赖于不同参数的并行测试方法
Run parallel test methods which are dependent with different parameter using Data Provider in TestNg
我想 运行 测试依赖并使用 ITestContext 的方法,运行 使用 TestNg 中的数据提供程序与不同的参数并行。我正在尝试实用地调用测试 class。
代码如下所示:
package com.ExploringTestNg;
import java.util.Random;
import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class ParallelMethodTest {
@Test(dataProvider = "dataprovider")
public void testclass1(ITestContext context,String deviceId) {
long id = Thread.currentThread().getId();
System.out.println("TestClass1 is being called and context is being setup");
System.out.print("The thread id is "+id);
System.out.println("device id is "+deviceId);
context.setAttribute("deviceId",deviceId);
}
@Test(dependsOnMethods = {"testclass1"})
public void testclass2(ITestContext context) {
long id = Thread.currentThread().getId();
System.out.println("TestClass2 is being called and context is being retrieved "+(String)context.getAttribute("deviceId"));
System.out.println("The thread id is "+id+"\n");
}
@Test(dependsOnMethods = {"testclass2"})
public void testclass3(ITestContext context) {
long id = Thread.currentThread().getId();
System.out.println("TestClass3 is being called and context is being retrieved "+(String)context.getAttribute("deviceId"));
System.out.println("The thread id is "+id);
}
@DataProvider(name = "dataprovider",parallel = true)
public Object[][] getDataFromDataprovider(){
return new Object[][]
{
{"1001"},
{"1002"},
{"1003"}
};
}
}
package com.ExploringTestNg;
import java.util.ArrayList;
import java.util.List;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
public class DynamicXMLSetuo {
public static void main(String[] args) {
XmlSuite suite = new XmlSuite();
suite.setName("TempSuite1");
XmlTest test = new XmlTest(suite);
test.setName("Functional usecase 1");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass("com.ExploringTestNg.ParallelMethodTest"));
test.setXmlClasses(classes);
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.setThreadCount(5);
tng.setParallel(XmlSuite.ParallelMode.METHODS);
tng.run();
}
}
我的功能是使用不同的参数多次调用测试 class 并且被调用的 classes 应该 运行 并行
这个问题是 testclass1
会被调用三次,但是 testclass2
和 testclass3
只会被调用一次,因为这些方法的执行不会取决于执行的次数 testclass1
。只看testclass1
通过与否。要解决此问题,您需要使用 @Factory
public class ParallelMethodTest {
private final String deviceId;
ParallelMethodTest(String id) {
this.deviceId = id;
}
@DataProvider(parallel = true)
public static Object[][] dataProvider() {
return new Object[][] { { "1001" }, { "1002" }, { "1003" } };
}
@Factory(dataProvider = "dataProvider")
public Object[] createInstances(String id) {
// The factory method uses the dataProvider to initialize
// multiple instances of the test class.
return new Object[] { new ParallelMethodTest(id) };
}
@Test
public void testclass1() {
long id = Thread.currentThread().getId();
System.out.println("TestClass1 is being called and context is being setup");
System.out.print("The thread id is " + id);
System.out.println("device id is " + deviceId);
}
@Test(dependsOnMethods = { "testclass1" })
public void testclass2() {
long id = Thread.currentThread().getId();
System.out.println("TestClass2 is being called and context is being retrieved " + deviceId);
System.out.println("The thread id is " + id + "\n");
}
@Test(dependsOnMethods = { "testclass2" })
public void testclass3() {
long id = Thread.currentThread().getId();
System.out.println("TestClass3 is being called and context is being retrieved " + deviceId);
System.out.println("The thread id is " + id);
}
}
有了这个,就不需要在测试上下文中存储 deviceId
,因为相应的 deviceId
将在 class 本身中可用。
UPDATE : 要并行运行,你需要在DynamicXMLSetuo
.
里面多做一件事
suite.setParallel(XmlSuite.ParallelMode.METHODS);
通过添加 ParallelMode.METHODS
,TestNG 将 运行 您在单独线程中的所有测试方法。从属方法也将 运行 在单独的线程中,但它们将遵守您指定的顺序。
我想 运行 测试依赖并使用 ITestContext 的方法,运行 使用 TestNg 中的数据提供程序与不同的参数并行。我正在尝试实用地调用测试 class。
代码如下所示:
package com.ExploringTestNg;
import java.util.Random;
import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class ParallelMethodTest {
@Test(dataProvider = "dataprovider")
public void testclass1(ITestContext context,String deviceId) {
long id = Thread.currentThread().getId();
System.out.println("TestClass1 is being called and context is being setup");
System.out.print("The thread id is "+id);
System.out.println("device id is "+deviceId);
context.setAttribute("deviceId",deviceId);
}
@Test(dependsOnMethods = {"testclass1"})
public void testclass2(ITestContext context) {
long id = Thread.currentThread().getId();
System.out.println("TestClass2 is being called and context is being retrieved "+(String)context.getAttribute("deviceId"));
System.out.println("The thread id is "+id+"\n");
}
@Test(dependsOnMethods = {"testclass2"})
public void testclass3(ITestContext context) {
long id = Thread.currentThread().getId();
System.out.println("TestClass3 is being called and context is being retrieved "+(String)context.getAttribute("deviceId"));
System.out.println("The thread id is "+id);
}
@DataProvider(name = "dataprovider",parallel = true)
public Object[][] getDataFromDataprovider(){
return new Object[][]
{
{"1001"},
{"1002"},
{"1003"}
};
}
}
package com.ExploringTestNg;
import java.util.ArrayList;
import java.util.List;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
public class DynamicXMLSetuo {
public static void main(String[] args) {
XmlSuite suite = new XmlSuite();
suite.setName("TempSuite1");
XmlTest test = new XmlTest(suite);
test.setName("Functional usecase 1");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass("com.ExploringTestNg.ParallelMethodTest"));
test.setXmlClasses(classes);
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.setThreadCount(5);
tng.setParallel(XmlSuite.ParallelMode.METHODS);
tng.run();
}
}
我的功能是使用不同的参数多次调用测试 class 并且被调用的 classes 应该 运行 并行
这个问题是 testclass1
会被调用三次,但是 testclass2
和 testclass3
只会被调用一次,因为这些方法的执行不会取决于执行的次数 testclass1
。只看testclass1
通过与否。要解决此问题,您需要使用 @Factory
public class ParallelMethodTest {
private final String deviceId;
ParallelMethodTest(String id) {
this.deviceId = id;
}
@DataProvider(parallel = true)
public static Object[][] dataProvider() {
return new Object[][] { { "1001" }, { "1002" }, { "1003" } };
}
@Factory(dataProvider = "dataProvider")
public Object[] createInstances(String id) {
// The factory method uses the dataProvider to initialize
// multiple instances of the test class.
return new Object[] { new ParallelMethodTest(id) };
}
@Test
public void testclass1() {
long id = Thread.currentThread().getId();
System.out.println("TestClass1 is being called and context is being setup");
System.out.print("The thread id is " + id);
System.out.println("device id is " + deviceId);
}
@Test(dependsOnMethods = { "testclass1" })
public void testclass2() {
long id = Thread.currentThread().getId();
System.out.println("TestClass2 is being called and context is being retrieved " + deviceId);
System.out.println("The thread id is " + id + "\n");
}
@Test(dependsOnMethods = { "testclass2" })
public void testclass3() {
long id = Thread.currentThread().getId();
System.out.println("TestClass3 is being called and context is being retrieved " + deviceId);
System.out.println("The thread id is " + id);
}
}
有了这个,就不需要在测试上下文中存储 deviceId
,因为相应的 deviceId
将在 class 本身中可用。
UPDATE : 要并行运行,你需要在DynamicXMLSetuo
.
suite.setParallel(XmlSuite.ParallelMode.METHODS);
通过添加 ParallelMode.METHODS
,TestNG 将 运行 您在单独线程中的所有测试方法。从属方法也将 运行 在单独的线程中,但它们将遵守您指定的顺序。