重用 Cucumber SpringBoot 测试进行本地和远程集成测试

问题描述 投票:0回答:1

我有以下设置

  • Springboot v3.x
  • 黄瓜 v7.x
  • Junit v5.x
  • Maven v3.x

我能够使用以下配置运行黄瓜测试,以使用 Maven 故障安全插件测试本地运行的 SpringBoot 应用程序。

注意:配置使用@SpringBootTest注解在运行测试之前启动SubjectUnderTest(SUT)。

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example.bdd.stepdefs")
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME,
    value = "pretty," +
     "html:target/tests-reports/report.html," +
     "json:target/tests-reports/report.json," +
     "junit:target/tests-reports/cucumber-junit.xml,")
@CucumberContextConfiguration
@ActiveProfiles("local")
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class FunctionalIT {
} 

我的要求:

  1. 重用相同的 Cucumber 测试来验证在 Maven 集成测试期间使用 @SpringBootTest 启动的 springboot 应用程序,以及在远程环境(例如 qa、prod)上测试已部署的应用程序
  2. 利用 Spring application-env.properties 文件和配置文件进行环境特定配置。
  3. 仅启动所需的最低 Spring ApplicationContext,具体取决于要针对本地应用程序(@SpringBootTest)与远程应用程序(无 @SpringBootTest 注释)运行的测试。
  4. 根据需要指定活动配置文件,例如
    -Dspring.profiles.active=local
    -Dspring.profile.active=qa
    。 Spring 只能使用该配置文件。

到目前为止,我尝试了几种选择,但没有取得多大成功,我面临以下问题:

  1. 尝试在没有
    @SpringBootTest
    注释的情况下为远程环境创建另一个测试类,但
    @CucumberContextConfiguration
    只允许在单个类上使用。
  2. 如果我将所有 Cucumber 配置分离在自己的类中,则 junit 不会运行 Cucumber 测试。显然
    @CucumberContextConfiguration
    只需要应用于
    @SpringBootTest
    @ContextConfiguration

知道如何让设置满足上述要求吗?

spring-boot cucumber junit5 cucumber-java cucumber-spring
1个回答
0
投票

我可以通过以下更改“部分”解决问题。

  1. 常见黄瓜配置的自定义注释
package com.example.bdd.config;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@CucumberContextConfiguration
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME,
    value = "pretty," +
     "html:target/tests-reports/report.html," +
     "json:target/tests-reports/report.json," +
     "junit:target/tests-reports/cucumber-junit.xml")
public @interface CucumberTestSuite {
    
}
  1. 用于测试本地/ci 构建的 SUT(使用 @SpringBootTest 设置)的测试运行器。
package com.example.bdd.test;

@CucumberTestSuite
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, 
    value = "com.example.bdd.stepdefs," + 
            "com.example.bdd.test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class FunctionalIT {
}
  1. 用于在远程 QA 环境中测试 SUT 的测试运行程序
package com.example.bdd.qa;

@CucumberTestSuite
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, 
    value = "com.example.bdd.stepdefs," + 
            "com.example.bdd.qa")
@ContextConfiguration
@TestPropertySource("classpath:application-qa.properties")
@ActiveProfiles("qa")
public class FunctionalRemoteIT {
}

尚未解决的问题:

  • 如何在运行
    mvn clean verify
    时默认禁用运行FunctionalRemoteIT测试,并且仅在设置了
    spring.profiles.active=qa
    时才启用。

我尝试使用 Spring

@EnabledIf
,但显然 mvn 故障安全插件没有使用它......它总是执行与模式
*IT.java
匹配的所有测试。

© www.soinside.com 2019 - 2024. All rights reserved.