gpt4 book ai didi

java - Cucable 插件未并行运行场景

转载 作者:行者123 更新时间:2023-12-02 09:50:10 25 4
gpt4 key购买 nike

我正在尝试使用 Trivago 的 Cucable 插件 并行运行场景。为了在实现我的项目之前对其进行测试,我下载了这个项目。

https://github.com/trivago/cucable-plugin/tree/master/example-project

我尝试了 mvn clean verify 并在目标文件夹中创建了 *IT.java 文件,但我注意到这些文件不是并行运行的。

我怎么知道?我在每个语句中添加了 sleep。最大 sleep 时间为 15 秒,因此总构建时间应约为 16 秒,但它显示 30 秒(所有场景 2+5+15+8 的所有 sleep 时间之和)。

cucable.template

import cucumber.api.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
glue = "steps",
features = {"target/parallel/features/[CUCABLE:FEATURE].feature"},
plugin = {"json:target/cucumber-report/[CUCABLE:RUNNER].json"}
)
public class [CUCABLE:RUNNER] {
// [CUCABLE:CUSTOM:comment]
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--suppress UnresolvedMavenProperty -->
<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>com.trivago.rta</groupId>
<artifactId>cucable-test-project</artifactId>
<version>1.5.1</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.failsafe.plugin.version>3.0.0-M3</maven.failsafe.plugin.version>
<maven.build.helper.plugin.version>3.0.0</maven.build.helper.plugin.version>
<cucumber.version>4.2.6</cucumber.version>
<maven.compiler.plugin.version>3.7.0</maven.compiler.plugin.version>

<generated.runner.directory>${project.build.directory}/parallel/runners</generated.runner.directory>
<generated.feature.directory>${project.build.directory}/parallel/features</generated.feature.directory>
</properties>

<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.trivago.rta</groupId>
<artifactId>cucable-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>generate-test-resources</id>
<phase>generate-test-resources</phase>
<goals>
<goal>parallel</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceRunnerTemplateFile>src/test/java/some/template/CucableJavaTemplate.java
</sourceRunnerTemplateFile>
<sourceFeatures>src/test/resources/features</sourceFeatures>

<generatedFeatureDirectory>${generated.feature.directory}</generatedFeatureDirectory>
<generatedRunnerDirectory>${generated.runner.directory}</generatedRunnerDirectory>

</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${maven.build.helper.plugin.version}</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${generated.runner.directory}</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven.failsafe.plugin.version}</version>
<executions>
<execution>
<id>Run parallel tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
<configuration>
<forkCount>5</forkCount>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

最佳答案

要点:

  • 我们不会混合直接依赖和传递依赖,特别是它们的版本!这样做可能会导致不可预测的结果。
  • 我们需要专门使用 Cucumber-JVM v4.x.x 来实现并行执行,而不使用 cucumber-jvm-parallel 或 cucable 插件
  • 我们将考虑使用 v4.2.6 进行实现

通过 JUnit 进行 Cucumber 并行执行

首先 - 使用正确的 io.cucumber 依赖项集更新 POM.xml。

 <dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.6</version>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>4.2.6</version>
</dependency>

注意事项 - 可能会出现这样的问题:一切正常,但测试仍然无法并行执行,如果您的 pom.xml 具有 testng 的直接/传递依赖性,则可能会出现这种情况。由于 testNG 导致 Surefire 忽略 JUnit 包装类。如果您有 testng 依赖项,请删除 TestNG 依赖项,或者您可以调用 2 定义 2 执行 - 对于 TestNG 和 JUnit,并根据您的需要禁用一个。

第二 - 根据您的框架需求自定义 Runner 类

package com.jacksparrow.automation.suite.runner;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features = "classpath:features/functional/",
glue = {"com.jacksparrow.automation.steps_definitions.functional" },
plugin = { "pretty","json:target/cucumber-json/cucumber.json",
"junit:target/cucumber-reports/Cucumber.xml", "html:target/cucumber-reports"},
tags = { "@BAMS_Submitted_State_Guest_User" },
junit ={ "--step-notifications"},
strict = false,
dryRun = false,
monochrome = true)

public class RunCukeTest {
}

第三 - 实现 Maven Surefire 插件,该插件实际上会并行运行测试

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<parallel>methods</parallel>
<threadCount>2</threadCount>
<reuserForks>false</reuserForks>
<testFailureIgnore>true</testFailureIgnore>
<includes>
<include>**/*RunCukeTest.java</include>
</includes>
</configuration>
</plugin>

第四 - 实现 Hooks.java

import cucumber.api.Scenario;
import cucumber.api.java.Before;
import cucumber.api.java.After;

public class Hooks {

@Before
public void setUpScenario(String browser){
//BaseSteps.getInstance().getBrowserInstantiation(browser); your browser setup method
}
@After
public void afterScenario(Scenario scenario){
// more code goes here
}
}

通过 TestNG 进行 Cucumber 并行执行

注意:在下面的实现中,我们将从 TestNG.xml 文件中读取浏览器参数

首先 - 使用正确的 io.cucumber 依赖项集更新 POM.xml。

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>4.2.6</version>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>4.2.6</version>
</dependency>

第二 - 根据您的框架需求自定义 TestNGRunner 类

package com.jacksparrow.automation.suite.runner;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import com.jacksparrow.automation.steps_definitions.functional.BaseSteps;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;

@CucumberOptions(features = "classpath:features/functional/",
glue = {"com.jacksparrow.automation.steps_definitions.functional" },
plugin = { "pretty","json:target/cucumber-json/cucumber.json",
"junit:target/cucumber-reports/Cucumber.xml", "html:target/cucumber-reports"},
tags = { "@BAMS_Submitted_State_Guest_User" },
junit ={ "--step-notifications"},
strict = false,
dryRun = false,
monochrome = true)

public class RunCukeTest extends Hooks {

}

第三 - 实现 Hooks.java

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import cucumber.api.testng.AbstractTestNGCucumberTests;

public class Hooks extends AbstractTestNGCucumberTests {

@Parameters({ "browser" })
@BeforeTest
public void setUpScenario(String browser){
//BaseSteps.getInstance().getBrowserInstantiation(browser); your browser setup method
}
}

第四 - 根据您的 TestNGRunner 类和框架需要更新/src/test/resources/下的 TestNG.xml。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Testng Cucumber Suite" parallel="tests" thread-count="2">
<test name="SmokeTest">
<parameter name="browser" value="chrome" />
<classes>
<class name="com.cvs.runner.TestSuiteRunner" />
</classes>
</test>
</suite>

第五 - 您应准备好通过以下任何方式使用 TestNG 运行自动化套件

 -    Run TestNG.xml directly from IDE 
- From CMD - mvn test -Dsurefire.suiteXmlFiles=src/test/resources/testng.xml
- From POM.xml - Using Surefire Plugin

<profiles>
<profile>
<id>selenium-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

关于java - Cucable 插件未并行运行场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56372368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com