无法控制黄瓜的并行执行
Unable to control parallel execution for cucumber
我无法控制来自 dataproviderthreadcount 的进程。
例如,如果我们有 4 个场景并且我 运行 我的脚本,它会并行执行所有 4 个场景。我在 pom 中为 maven-surfire 提供的 dataproviderthreadcount 值并不重要。
下面是截图。
POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>testParallerRun</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<cucumbertestng.version>5.6.0</cucumbertestng.version>
<cucumberjava.version>5.6.0</cucumberjava.version>
<mvncompiler.version>3.8.1</mvncompiler.version>
<javarelease.version>11</javarelease.version>
<mvnsurefire.version>3.0.0-M5</mvnsurefire.version>
</properties>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>${cucumbertestng.version}</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumberjava.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${mvncompiler.version}</version>
<configuration>
<release>${javarelease.version}</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${mvnsurefire.version}</version>
<configuration>
<properties>
<property>
<name>dataproviderthreadcount</name>
<value>2</value>
</property>
</properties>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
亚军Class:
package com.test.runner;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
import org.testng.annotations.DataProvider;
@CucumberOptions(
plugin = {
"pretty",
"json:target/reports/json/result.json",
"html:target/reports/html/result.html"},
strict = true,
features = {"src/test/resources/"},
glue = {"com.test.stepdef"},
tags = {""}
)
public class TestRunner extends AbstractTestNGCucumberTests {
@DataProvider(parallel = true)
@Override
public Object[][] scenarios() {
return super.scenarios();
}
}
功能 1:
Feature: test 1
Scenario: 01
Given Print "01"
Scenario: 02
Given Print "02"
Scenario: 03
Given Print "03"
Scenario: 04
Given Print "04"
如果有人知道如何控制线程数而不是让 Testng 决定,请告诉我。
TestNG 本身没有问题。
您在 pom 文件中设置的 dataproviderthreadcount
属性仅在您通过 mvn clean test
进行测试时才适用和相关。
如果您尝试从 IDE(IntelliJ 或 Eclipse)中 运行 此测试 class 它不会生效。
这是我创建的测试 class,由数据提供者提供支持(为了简单起见,我特意将黄瓜排除在方程式之外)
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class AppTest {
@Test(dataProvider = "dp")
public void shouldAnswerWithTrue(int i) {
System.err.println("Running on [" + Thread.currentThread().getId() + "]");
}
@DataProvider(name = "dp", parallel = true)
public Object[][] testData() {
return new Object[][]{
{1},
{2},
{3},
{4},
{5},
{6}
};
}
}
这是我 运行 mvn clean test
时的命令行输出
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ testng_playground ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.rationaleemotions.AppTest
Running on [15]
Running on [14]
Running on [15]
Running on [14]
Running on [14]
Running on [15]
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.62 s - in com.rationaleemotions.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0
我遇到的一模一样。正如 Krishnan 提到的,pom.xml 中的 dataproviderthreadcount 仅在您 运行 mvn test 但没有任何参数时有效。如果要指定 xml 到 运行 的套件,您还需要在命令行中添加 -Ddataproviderthreadcount=2。例如,“mvn -Dsurefire.suiteXmlFiles=suite.xml -Ddataproviderthreadcount=2 test”
我无法控制来自 dataproviderthreadcount 的进程。 例如,如果我们有 4 个场景并且我 运行 我的脚本,它会并行执行所有 4 个场景。我在 pom 中为 maven-surfire 提供的 dataproviderthreadcount 值并不重要。
下面是截图。
POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>testParallerRun</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<cucumbertestng.version>5.6.0</cucumbertestng.version>
<cucumberjava.version>5.6.0</cucumberjava.version>
<mvncompiler.version>3.8.1</mvncompiler.version>
<javarelease.version>11</javarelease.version>
<mvnsurefire.version>3.0.0-M5</mvnsurefire.version>
</properties>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>${cucumbertestng.version}</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumberjava.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${mvncompiler.version}</version>
<configuration>
<release>${javarelease.version}</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${mvnsurefire.version}</version>
<configuration>
<properties>
<property>
<name>dataproviderthreadcount</name>
<value>2</value>
</property>
</properties>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
亚军Class:
package com.test.runner;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
import org.testng.annotations.DataProvider;
@CucumberOptions(
plugin = {
"pretty",
"json:target/reports/json/result.json",
"html:target/reports/html/result.html"},
strict = true,
features = {"src/test/resources/"},
glue = {"com.test.stepdef"},
tags = {""}
)
public class TestRunner extends AbstractTestNGCucumberTests {
@DataProvider(parallel = true)
@Override
public Object[][] scenarios() {
return super.scenarios();
}
}
功能 1:
Feature: test 1
Scenario: 01
Given Print "01"
Scenario: 02
Given Print "02"
Scenario: 03
Given Print "03"
Scenario: 04
Given Print "04"
如果有人知道如何控制线程数而不是让 Testng 决定,请告诉我。
TestNG 本身没有问题。
您在 pom 文件中设置的 dataproviderthreadcount
属性仅在您通过 mvn clean test
进行测试时才适用和相关。
如果您尝试从 IDE(IntelliJ 或 Eclipse)中 运行 此测试 class 它不会生效。
这是我创建的测试 class,由数据提供者提供支持(为了简单起见,我特意将黄瓜排除在方程式之外)
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class AppTest {
@Test(dataProvider = "dp")
public void shouldAnswerWithTrue(int i) {
System.err.println("Running on [" + Thread.currentThread().getId() + "]");
}
@DataProvider(name = "dp", parallel = true)
public Object[][] testData() {
return new Object[][]{
{1},
{2},
{3},
{4},
{5},
{6}
};
}
}
这是我 运行 mvn clean test
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ testng_playground ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.rationaleemotions.AppTest
Running on [15]
Running on [14]
Running on [15]
Running on [14]
Running on [14]
Running on [15]
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.62 s - in com.rationaleemotions.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0
我遇到的一模一样。正如 Krishnan 提到的,pom.xml 中的 dataproviderthreadcount 仅在您 运行 mvn test 但没有任何参数时有效。如果要指定 xml 到 运行 的套件,您还需要在命令行中添加 -Ddataproviderthreadcount=2。例如,“mvn -Dsurefire.suiteXmlFiles=suite.xml -Ddataproviderthreadcount=2 test”