Java TestNG 与 DataProvider 并行执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31521466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TestNG parallel Execution with DataProvider
提问by sujith
I have a single test which receives data from data provider. I would like this test to run in parallel with different values from data provider .
我有一个从数据提供者接收数据的测试。我希望此测试与来自 data provider 的不同值并行运行。
I tried an approach like :
我尝试了一种方法:
public class IndependentTest
{
@Test(dataProvider = "dp1" ,threadPoolSize=3,invocationCount=1)
public void testMethod(int number)
{
Long id = Thread.currentThread().getId();
System.out.println("HELLO : " + id);
}
@DataProvider(name = "dp1",parallel=true)
public Object[][] dp1() {
return new Object[][] {
new Object[] { 1 },
new Object[] { 2 },
new Object[] { 3 },
new Object[] { 4 },
new Object[] { 5 },
new Object[] { 6 },
new Object[] { 7 },
new Object[] { 8 }
};
}
}
}
The output i received is :
我收到的输出是:
HELLO : 10
你好:10
HELLO : 12
你好:12
HELLO : 17
你好:17
HELLO : 11
你好:11
HELLO : 16
你好:16
HELLO : 14
你好:14
HELLO : 13
你好:13
HELLO : 15
你好:15
Spawned 10 threads while i specified 5 in the thread pool size . Could you please tell what has to be added to the above snippet to control the data provider thread pool size .
生成了 10 个线程,而我在线程池大小中指定了 5 个。你能告诉我必须在上面的代码片段中添加什么来控制数据提供者线程池的大小吗?
采纳答案by niharika_neo
回答by TheCodingFrog
Currently only one thread is getting used as you have define invocationCount
as 1, if you change it to 3 then three workers thread will get used.
当前只有一个线程被使用,因为您定义invocationCount
为 1,如果您将其更改为 3,则将使用三个工作线程。
invocationCount
:- The number of times this method should be invoked.
invocationCount
:- 此方法应被调用的次数。
threadPoolSize
:- The size of the thread pool for this method. The method will be invoked from multiple threads as specified by invocationCount.
Note: this attribute is ignored if invocationCount is not specified.
threadPoolSize
:- 此方法的线程池大小。该方法将从 invocationCount 指定的多个线程中调用。注意:如果未指定 invocationCount,则忽略此属性。
Also,
还,
You can also specify that a @Test
method should be invoked from different threads. You can use the attribute threadPoolSize
to achieve this result:
您还可以指定@Test
应从不同线程调用方法。您可以使用该属性threadPoolSize
来实现此结果:
@Test(threadPoolSize = 3, invocationCount = 10, timeOut = 10000)
public void testServer() {
In this example, the function testServer will be invoked ten times from three different threads. Additionally, a time-out of ten seconds guarantees that none of the threads will block on this thread forever.
在这个例子中,函数 testServer 将从三个不同的线程调用十次。此外,十秒的超时保证没有任何线程永远阻塞在该线程上。
More info can be found here
更多信息可以在这里找到
回答by Igor Gladun
In testng.xml you can set thread count for the dataprovider via data-provider-thread-count="3"
在 testng.xml 中,您可以通过以下方式为数据提供程序设置线程数 data-provider-thread-count="3"
<suite name="Manage" data-provider-thread-count="3" >
<test name="Manage data tests">
<classes>
<class name="uk.example.ExampleTest"></class>
</classes>
</test>
</suite>
回答by sanitar4eg
Try to set the thread pool in following way:
尝试按以下方式设置线程池:
@BeforeClass
public void setupClassName(ITestContext context) {
context.getCurrentXmlTest().getSuite().setDataProviderThreadCount(5);
context.getCurrentXmlTest().getSuite().setPreserveOrder(false);
}
回答by yu yang
I think there is a way to set it on annotation level, it should be add on the DataProvider's :
我认为有一种方法可以在注释级别设置它,它应该添加到 DataProvider 的:
@DataProvider(name="quick-screen-list", parallel = true)
public Object[][] quickScreenDataProvider() {
.....
回答by ChaM
You can achieve this by adding extra configuration 'parallel=true', along with the name of the DataProvider, in its definition. An example is as follows:
您可以通过在其定义中添加额外的配置 'parallel=true' 以及 DataProvider 的名称来实现这一点。一个例子如下:
@DataProvider(name="InvalidLoginDataProvider", parallel = true)
public Object[][] myDataProviderMethod(){
...
...
}
As per TestNG documentation, the @Test thread pool (created using invocationCount and threadPoolSize parameters in @Test) and Data provider thread pool are different and managed differently.
So, to specify how many threads in Data provider thread pool, one has to add the following configuration in testng.xml file.
根据 TestNG 文档,@Test 线程池(使用 @Test 中的 invocationCount 和 threadPoolSize 参数创建)和 Data provider 线程池是不同的,并且管理方式不同。
因此,要指定 Data provider 线程池中的线程数,必须在 testng.xml 文件中添加以下配置。
<suite name="Suite1" data-provider-thread-count="20" >
...
...
</suite>
HTH!
哼!
回答by user12896703
hi I am getting below error when i am trying to handle with data provider thread count .How to resolve the issue
嗨,当我尝试处理数据提供程序线程计数时出现以下错误。如何解决该问题
java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated. Make sure to release the connection before allocating another one.
java.lang.IllegalStateException:BasicClientConnManager 的使用无效:连接仍然分配。确保在分配另一个连接之前释放连接。