当前位置: 首页 > 知识库问答 >
问题:

如何运行并行cucumberSpring启动集成测试与Maven(肯定或故障安全)?

楚俊逸
2023-03-14

大家好,
我从今年1月开始开发cucumber测试,现在在Spring Boot环境中与maven(surefire和failsafe)并行运行cucumber集成测试时遇到了一个问题。

简而言之:
我想在运行Spring Boot应用程序的同时运行cucumber集成测试。我使用maven时,Surefire或Failsafe插件处于活动状态。当我注释掉代码中的每个Spring Boot组件时,一切都正常。然后我有和功能文件一样多的线程,它们并行运行。但是,当我添加所需的Spring Boot组件时,测试会以串行方式运行。

我在pom.xml的依赖:

<dependencies>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>${cucumber.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java8</artifactId>
        <version>${cucumber.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>${cucumber.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-spring</artifactId>
        <version>${cucumber.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.10</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring.boot.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>${spring.boot.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>${spring.boot.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.30</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.200</version>
        <scope>test</scope>
    </dependency>
</dependencies>

我的插件在pom.xml:

<plugins>
    <!-- Maven Compiler -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
            <source>8</source>
            <target>8</target>
        </configuration>
    </plugin>
    <!-- Option 1 to run cucumber tests in parallel: Surefire -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.0</version>
        <configuration>
            <parallel>methods</parallel>
            <useUnlimitedThreads>true</useUnlimitedThreads>
        </configuration>
    </plugin>
    <!-- Option 2 to run cucumber tests in parallel: Failsafe -->
    <!--<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>3.0.0-M4</version>
        <executions>
            <execution>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
                <configuration>
                    <parallel>methods</parallel>
                    <threadCount>5</threadCount>
                    <useUnlimitedThreads>false</useUnlimitedThreads>
                    &lt;!&ndash; important for the right execution &ndash;&gt;
                    <perCoreThreadCount>true</perCoreThreadCount>
                </configuration>
            </execution>
        </executions>
    </plugin>-->
</plugins>

我的Java类:

  • RunCucumberit. java--

Java代码:

java和RunCucumberTest.java

package parallel;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
/**
 * Class to run the cucumber tests with Surefire.
 */
@RunWith(Cucumber.class)
@CucumberOptions(plugin = { }, strict = true, snippets = CucumberOptions.SnippetType.CAMELCASE,
        features = "src/test/java/parallel", stepNotifications = true)
public class RunCucumberTest {
}

StepDefinitions.java

package parallel;
import io.cucumber.java8.En;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class StepDefinitions implements En {
    public StepDefinitions() {
        Given("Step from {string} in {string} feature file", this::printThreads);
    }
    private void printThreads(final String scenario, final String file) throws InterruptedException {
        log.info(" Thread ID - {} - {} from {} feature file.", Thread.currentThread().getId(), scenario, file);
        Thread.sleep(5000L);
    }
}

TestApplication.java

package parallel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestApplication {
    public static void main(final String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

cucumber。JAVA

package parallel;
import io.cucumber.java.Before;
import io.cucumber.spring.CucumberContextConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.test.context.SpringBootContextLoader;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
/**
 * Class to use spring application context while running cucumber.
 */
@Slf4j
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = TestApplication.class, loader = SpringBootContextLoader.class)
@CucumberContextConfiguration
public class CucumberSpringContextConfiguration {
    /**
     * Need this method so the cucumber will recognize this class as glue and load spring context configuration.
     */
    @Before
    public void setUp() {
        log.info("Spring Context initialized for executing cucumber tests.");
    }
}

我的小cucumber代码:

scenario-outline.feature

Feature: Scenario Outlines feature file
  Scenario Outline: <scen_out_row_num>
    Given Step from '<scen_out_row_num>' in 'scenario-outlines' feature file
    Examples:
      | scen_out_row_num       |
      | Scenario Outline Row 1 |
      | Scenario Outline Row 2 |

情景-0。特色

Feature: Scenarios feature file
  Scenario: Scenario Number One -
    Given Step from 'Scenario 1' in 'scenarios-0' feature file
  Scenario: Scenario Number Two -
    Given Step from 'Scenario 2' in 'scenarios-0' feature file

scenarios-1.feature

Feature: Scenarios feature file
  Scenario: Scenario Number One a
    Given Step from 'Scenario 1' in 'scenarios-1' feature file
  Scenario: Scenario Number Two a
    Given Step from 'Scenario 2' in 'scenarios-1' feature file

scenarios-2.feature

Feature: Scenarios feature file
  Scenario: Scenario Number One b
    Given Step from 'Scenario 1' in 'scenarios-2' feature file
  Scenario: Scenario Number Two b
    Given Step from 'Scenario 2' in 'scenarios-2' feature file

情景3。特色

Feature: Scenarios feature file
  Scenario: Scenario Number One c
    Given Step from 'Scenario 1' in 'scenarios-3' feature file
  Scenario: Scenario Number Two c
    Given Step from 'Scenario 2' in 'scenarios-3' feature file

预期行为:
每个功能文件一个线程,并行执行集成测试。有些输出是这样的(这是我注释掉所有Spring Boot内容时的输出):

[INFO] --- maven-failsafe-plugin:3.0.0-M4:integration-test (default) @ cucumber-test ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running Scenario Number One b
[pool-1-thread-4] INFO parallel.StepDefinitions -  Thread ID - 17 - Scenario 1 from scenarios-2 feature file.
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.338 s - in Scenario Number One b
[INFO] Running Scenario Number One a
[pool-1-thread-3] INFO parallel.StepDefinitions -  Thread ID - 16 - Scenario 1 from scenarios-1 feature file.
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.353 s - in Scenario Number One a
[INFO] Running Scenario Outline Row 1
[pool-1-thread-1] INFO parallel.StepDefinitions -  Thread ID - 14 - Scenario Outline Row 1 from scenario-outlines feature file.
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.363 s - in Scenario Outline Row 1
[INFO] Running Scenario Number One c
[pool-1-thread-5] INFO parallel.StepDefinitions -  Thread ID - 18 - Scenario 1 from scenarios-3 feature file.
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.371 s - in Scenario Number One c
[INFO] Running Scenario Number One -
[pool-1-thread-2] INFO parallel.StepDefinitions -  Thread ID - 15 - Scenario 1 from scenarios-0 feature file.
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.378 s - in Scenario Number One -
[INFO] Running Scenario Outline Row 2
[pool-1-thread-1] INFO parallel.StepDefinitions -  Thread ID - 14 - Scenario Outline Row 2 from scenario-outlines feature file.
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.003 s - in Scenario Outline Row 2
[INFO] Running Scenario Number Two a
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.011 s - in Scenario Number Two a
[INFO] Running Scenario Number Two c
[pool-1-thread-3] INFO parallel.StepDefinitions -  Thread ID - 16 - Scenario 2 from scenarios-1 feature file.
[pool-1-thread-5] INFO parallel.StepDefinitions -  Thread ID - 18 - Scenario 2 from scenarios-3 feature file.
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.017 s - in Scenario Number Two c
[INFO] Running Scenario Number Two b
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.027 s - in Scenario Number Two b
[INFO] Running Scenario Number Two -
[pool-1-thread-4] INFO parallel.StepDefinitions -  Thread ID - 17 - Scenario 2 from scenarios-2 feature file.
[pool-1-thread-2] INFO parallel.StepDefinitions -  Thread ID - 15 - Scenario 2 from scenarios-0 feature file.
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.043 s - in Scenario Number Two -


10
 Scenarios (
10 passed
)


10
 Steps (
10 passed
)


0m
10,486s

实际行为:
只使用一个线程,所有测试都串行运行。输出:

[INFO] --- maven-failsafe-plugin:3.0.0-M4:integration-test (default) @ cucumber-test ---
...    
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.4.RELEASE)
...
Thread ID - 1 - Scenario Outline Row 1 from scenario-outlines feature file.
2020-06-10 12:04:02.519  INFO 13028 --- [           main] o.s.t.c.support.AbstractContextLoader    : Could not detect default resource locations for test class [parallel.CucumberSpringContextConfiguration]: no resource found for suffixes {-context.xml, Context.groovy}.
...
2020-06-10 12:04:02.544  INFO 13028 --- [           main] parallel.StepDefinitions                 : 
... 
Thread ID - 1 - Scenario Outline Row 2 from scenario-outlines feature file.
2020-06-10 12:04:07.550  INFO 13028 --- [           main] o.s.t.c.support.AbstractContextLoader    : Could not detect default resource locations for test class [parallel.CucumberSpringContextConfiguration]: no resource found for suffixes {-context.xml, Context.groovy}.
Thread ID - 1 - Scenario 1 from scenarios-0 feature file.
...
Thread ID - 1 - Scenario 2 from scenarios-0 feature file.
...
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 10, Failures: 0, Errors: 0, Skipped: 0

希望这个问题可以理解,有人可以帮助我!

提前感谢,
Maximotus

共有1个答案

芮朗
2023-03-14

为了并行运行cucumber测试,您必须添加pom。xml为junijupiterjunitvintage排除。这就是它对我的作用:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
 类似资料:
  • 问题内容: 我正在尝试使用Maven故障安全插件通过以下配置运行集成测试: 一切正常,直到Jetty在集成前测试阶段启动。然后什么也没有发生,好像它在等待什么。最后一行说: 我如何才能使测试立即开始?我使用运行Maven 。 问题答案: 将Jetty Maven插件版本从6.1.7更改为6.1.26可以解决所有问题。

  • 问题内容: 我还不清楚如何最好地使用Maven Failsafe插件进行集成测试。我的用例是针对本地MySQL数据库测试SQL查询。 我了解应该在该阶段启动数据库,而在期间关闭数据库。但是我该如何指定呢?我应该在pom.xml中放入命令行吗?还是我应该使用特定注释进行注释的方法? 问题答案: 在常规的内置Maven生命周期(jar,war …)中,和测试阶段未绑定到任何maven插件(即,这些阶段

  • 我还不完全清楚如何最好地使用Maven Failsecurity插件进行集成测试。我的用例是针对本地MySQL数据库测试SQL查询。 我知道数据库应该在集成前测试阶段启动,在集成后测试阶段关闭。但我该如何具体说明呢?是否应该在pom中输入命令行。xml?或者一个我应该用特定注释进行注释的方法?

  • 现在我们有一个项目,包含两个工作。1) 是带有单元测试的标准构建。2) 是集成测试。它们是这样工作的: 构建整个项目,运行单元测试,启动集成测试工作 问题是步骤2)现在需要一个多小时才能运行,我想将集成测试并行化,以便它们花费更少的时间。但我不确定我该怎么做。我的第一个想法是,我可以有两个这样的步骤: 构建整个项目,运行单元测试,启动集成测试工作 构建整个项目,将其部署到integration s

  • 它是可以配置surefire插件只运行一些测试并行和其他顺序? 也可以使用surefireforkCount来运行声明为jUnit Suite的并行测试吗?

  • 我有个关于Maven的问题。 我有文件名以*Test结尾的单元测试和文件名以*IT结尾的集成测试。 我的理解是,surefire将运行单元测试,故障保险将运行集成测试。 当我运行:mvn清洁安装 同时运行单元测试和集成测试。 当我跑步时: mvn验证 这两个测试也会运行。 我是否可以配置maven,以便在使用:mvn clean install时,只运行单元测试。当我使用mvn验证时,是否只运行集