TestNG 并行测试配置 [英] TestNG Parallel Test Configuration
问题描述
使用 Java 6 上的 TestNG,这是我想在我的 Maven 项目中执行的操作:
With TestNG on Java 6 here's what I'd like to do in my Maven project:
- 启动一个测试方法 (
testAbc
),它可以使用基于队列的DataProvider
运行多线程 - 在
testAbc
的所有线程完成后,开始一个依赖于testAbc
(testXyz
) 的测试
- Kick off a test method (
testAbc
) that can run multi-threaded using a queue-basedDataProvider
- Kick off a test that relies on
testAbc
(testXyz
) after all the threads fromtestAbc
complete
我以为我已经正确配置了它,但我没有.
I thought I had it configured properly, but I do not.
目前我已将其配置为:
@Test ( singleThreaded = false )
public class AutomatedTest {
@Test (
alwaysRun = true,
dataProviderClass = UseCaseProvider.class,
dataProvider = "getUseCasesNoDependencies",
skipFailedInvocations = false,
threadPoolSize = 25
)
public void testAbc(UseCase useCase) {
executeUseCase(useCase);
}
@Test (
dependsOnMethods = {"testAbc"},
dataProviderClass = UseCaseProvider.class,
dataProvider = "getUseCasesDependencies",
singleThreaded = true
)
public void testXyz(UseCase useCase) {
executeUseCase(useCase);
}
}
@DataProvider
提供的 testAbc
中的所有测试都需要很长时间才能运行,但最多可以同时运行 25 个(有几百个)其中).testXyz
中的所有内容都不能并行运行,因为不仅所有情况都依赖于 testAbc
,而且处理对这样的线程池并不友好.
All of the tests in testAbc
that are provided by the @DataProvider
take a long time to run but can be run up to 25 simultaneously (there's a good few hundred of them). Everything in testXyz
cannot be run in parallel as not only do all cases rely on testAbc
, but the processing is just not friendly to thread pooling like that.
我的 Maven 配置设置如下:
My Maven configuration is setup as follows:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<parallel>methods</parallel>
<threadCount>25</threadCount>
</configuration>
</plugin>
</plugins>
</build>
当我运行 mvn -U test
时,我没有看到我的测试实际上是并行运行的.帮助!
When I run mvn -U test
I don't see my tests actually running in parallel.
Help!
$ mvn -version
Apache Maven 2.2.1 (r801777; 2009-08-06 15:16:01-0400)
Java version: 1.6.0_35
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x" version: "10.7.5" arch: "x86_64" Family: "mac"
推荐答案
添加threadPoolSize
@Test ( singleThreaded = false, threadPoolSize = 5 )
public class AutomatedTest {
...
threadPoolSize
属性允许您指定应为此执行分配多少线程.
The threadPoolSize
attribute allows you to specify how many threads should be allocated for this execution.
编辑
刚刚注意到您错过了invocationCount
.请注意,如果未指定 invocationCount
,则忽略 threadPoolSize
.
Just noticed that you missed invocationCount
. Please note that threadPoolSize
is ignored if invocationCount
is not specified.
所以请尝试保留类@Test 并更新testAbc
So please try leaving class @Test as it is and update testAbc
@Test (
alwaysRun = true,
dataProviderClass = UseCaseProvider.class,
dataProvider = "getUseCasesNoDependencies",
skipFailedInvocations = false,
threadPoolSize = 25,
invocationCount = 25
)
public void testAbc(UseCase useCase) {
executeUseCase(useCase);
}
这篇关于TestNG 并行测试配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!