将Cucumber SpringBoot测试用于本地和远程集成测试

brvekthn  于 2023-11-17  发布在  Spring
关注(0)|答案(1)|浏览(183)

我有下面的设置

  • Springboot v3.x
  • cucumber v7.x
  • Junit v5.x
  • Maven v3.x

我能够运行 cucumber 测试与下面的配置来测试SpringBoot应用程序运行本地使用maven故障保护插件。
注意:配置使用@SpringBootTest annotation在运行Test之前启动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)上测试部署的应用程序
1.利用Spring application-env.properties文件和配置文件进行特定于环境的配置。
1.仅启动最小所需的Spring ApplicationContext,具体取决于针对本地应用程序(@SpringBootTest)和远程应用程序(No @SpringBootTest annotation)运行的测试。
1.根据需要指定活动配置文件,例如-Dspring.profiles.active=local-Dspring.profile.active=qa。Spring只应该使用该配置文件。
到目前为止,我尝试了几种选择,但没有取得多大成功,我面临以下问题:
1.已尝试在不带@SpringBootTest注解的情况下为远程环境添加另一个测试类,但@CucumberContextConfiguration仅允许用于单个类。
1.如果我把所有cucumber config都放在它自己的类中,junit就不会运行cucumber测试了。只需要在@SpringBootTest@ContextConfiguration上应用cucumber @CucumberContextConfiguration
有什么想法如何让设置工作的上述要求?

wnrlj8wa

wnrlj8wa1#

我通过下面的修改解决了这个问题。
1.普通 cucumber 配置的自定义注解

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.env.test;

@CucumberTestSuite
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, 
    value = "com.example.bdd.stepdefs," + 
            "com.example.bdd.env.test")
@ContextConfiguration(initializers = ApplicationInitializer.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class ComponentIT {
}

# Note: ApplicationInitializer takes care of spinning up required dependencies (mocks/stubs) for the Component Testing.


1.用于在远程QA环境中测试SUT的测试运行程序

package com.example.bdd.env.acceptance;

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

  1. pom.xml中的不同maven配置文件
<profiles>
    <profile>
      <id>default</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <configuration>
              <excludes>
                <exclude>**/*AcceptanceIT.java</exclude>
              </excludes>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>acceptance-tests</id>
      <properties>
        <skip.surefire.tests>true</skip.surefire.tests>
      </properties>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <configuration>
              <includes>
                <include>**/*AcceptanceIT.java</include>
              </includes>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>


现在开始跑步

  1. mvn clean verify默认情况下运行单元测试和QuestionIT(适用于本地和CI环境)
  2. mvn clean verify -P acceptance-tests -Denv=qaqa环境上运行AcceptanceIT测试,如wise -Denv=stage-Denv=prod分别在stageprod环境上运行测试。

相关问题