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

时间:2019-05-16 11:41:19

标签: cucumber-jvm cucumber-java

我有此配置:

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

我可以得到吗?

2 个答案:

答案 0 :(得分:0)

您正在使用哪个版本的cucumber-java?如果是黄瓜4,则可以使用thread =“ 2”(多于1个线程)并行执行方案。

答案 1 :(得分:0)

要点:在开始之前,想分享一下笔记以便将来更好地理解,我们将不会混用直接和传递依赖项,尤其是它们的版本!这样做可能导致不可预测的结果。好的,让我们逐步了解事物。

首先-用正确的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具有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  
    }
   }