如何 运行 两组测试与 Maven 之间的延迟

How to run two set of tests with maven with delay in between

我们的目标是对系统进行验收测试,可以在指定的时间内禁用,并确保系统被禁用并恢复启用。

我们的计划是:

我们希望将启用和禁用情况的测试保持分离,因此必须在 maven 本身或作为某种插件类型的解决方案中引入睡眠。

问题:如何通过maven指定测试目标运行的顺序,并在两者之间添加参数化延迟,这将被提供给Selenium?

您可以应用以下配置:

  • 配置 maven-surefire-pluginmaven-failsafe-plugin(更适合集成测试,在这个用例中听起来更合适)通过其 include/exclude mechanism 执行第一组测试,如第一次执行这个插件
  • 配置 maven-surefire-plugin(或 maven-failsafe-plugin)以执行示例测试用例,其唯一目的是 睡眠 确定(或可配置) ) 时间,再次通过 include/exclude mechanism,作为此插件的第二次执行(Maven 将按照 pom.xml 文件声明遵守执行顺序)
  • 再次通过 include/exclude 配置 maven-surefire-plugin(或 maven-failsafe-plugin)以执行第二组测试(或单个检查测试,在本用例中),如第三次执行(然后将作为最后一次执行)。

在同一阶段使用同一个插件多次执行将确保您在 Maven 执行期间

下面是上述方法的示例片段:

<profile>
    <id>check-test</id>
    <build>
        <properties>
           <sleep.time>2000</sleep.time>
        </properties>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <executions>
                    <execution>
                        <id>first-execution</id>
                        <phase>test</phase>
                        <configuration>
                            <includes>
                                <include>*FirstTestsSample.java</include>
                            </includes>
                        </configuration>
                    </execution>
                    <execution>
                        <id>second-execution</id>
                        <phase>test</phase>
                        <configuration>
                            <includes>
                                <include>SleepTest.java</include>
                            </includes>
                            <systemPropertyVariables>
                               <sleepParam>${sleep.time}</sleepParam>
                            </systemPropertyVariables>
                        </configuration>
                    </execution>
                    <execution>
                        <id>third-execution</id>
                        <phase>test</phase>
                        <configuration>
                            <includes>
                                <include>CheckTest.java</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

请注意,我将所有内容都包装在 Maven profile 中以保持清洁,因为您可能不希望将此行为作为默认构建的一部分,而是仅按需执行(或作为 CI 工作)。

如果您需要配置休眠时间,则可以通过systemPropertyVariables选项配置每个execution的相关configuration部分。

然后您可以按如下方式调用您的构建:

mvn clean verify -Pcheck-test -Dsleep.time=3000

其中 -P 通过其 ID 启用配置文件,我们还通过命令行覆盖 sleep.time 属性 的默认值,然后作为 sleepParam 系统变量,可以通过 System.gerProperty("sleepParam") 调用从 Java 代码中获取。


另请注意,maven-failsafe-plugin 可能更适合您的场景,因为它可以更好地处理 post-integration/acceptance 测试的执行,如其 official page 中所述,甚至尽管您的用例可能仍由 `maven-surefire-plugin' 提供服务。