我不知道如何使用 @cucumberOption 并行化我在黄瓜中的测试

I don't know how to parallelize my test in cucumber with @cucumberOption

我有这样的配置:

@RunWith(Cucumber.class)
@CucumberOptions(
         features = "src/test/resources/features",
         glue = "com.cucumberTest.stepDefinitions",
         monochrome=true,
         plugin = {
                 "html:target/cucumber-html-report",
                 "json:target/cucumber.json",
                 "pretty:target/cucumber-pretty.txt",
                 "usage:target/cucumber-usage.json",
                 "junit:target/cucumber-results.xml"
                    }

)

我尝试转换为:

Main.main(new String[]{"--threads", "4",
                "-p","timeline:target/cucumber-parallel-report",
               "-p","json:target/prueba/cucumber.json",
                "-p","junit:target/cucumber-results.xml",
                "-p","pretty:target/cucumber-pretty.txt",
               "-p","html:target/cucumber-html-report",
                "-g", "com.cucumberTest.stepDefinitions", "src/test/resources/features/"});

但带有@cucumberOption 标签。我也在尝试为此使用下一个插件,我想我明白了,但我想进入@cucumberOptions

插件:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                    <parallel>both</parallel>
                    <threadCount>15</threadCount>
                </configuration>
 </plugin>

我能拿到吗?

您使用的 cucumber-java 是哪个版本?如果是cucumber 4,那么你可以使用threads = "2"(多于1个线程)来并行执行场景。

要点: 在我们开始之前,想分享一个注意事项以便将来更好地理解,我们不会混合直接依赖和传递依赖,特别是他们的版本!这样做会导致不可预知的结果。好吧,让我们一步步来了解一下。

首先 - 使用正确的 io.cucumber 依赖集更新 POM.xml。我们正在考虑为此实现使用 v4.2.6

 <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 有 direct/transitive testng的依赖。由于 testNG 导致 Surefire 忽略 JUnit 包装器 class。如果您有 testng 依赖项,请删除 TestNG 依赖项,或者您可以调用 2 define 2 execution - For TestNG & JUnit 并根据需要禁用一个。

其次 - 根据您的框架需求自定义 Runner class

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  
    }
   }