【问题标题】:Force post-integration phase to always complete after integration phase强制集成后阶段始终在集成阶段后完成
【发布时间】:2020-03-30 16:53:36
【问题描述】:

有没有办法强制post-integration 阶段始终在integration 阶段之后运行?我的意思是在integration 阶段出现测试失败时。

我正在运行一个 Angular / Springboot 应用程序。我使用量角器来运行测试整个 Angular + Springboot 链的 e2e 测试。我设法将它集成到我的 Maven 构建中,以便我可以:

  • 设置后端 Springboot 服务器
  • 使用初始数据设置数据库
  • integration 阶段运行量角器

使用以下插件:

spring-boot-maven-plugin 启动和停止测试服务器以进行集成测试:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        ...
    </configuration>
    <executions>
        <execution>
            <id>pre-integration-test</id>
            <goals>
                <goal>start</goal>
            </goals>
        </execution>
        <execution>
            <id>post-integration-test</id>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>

frontend-maven-pluginintegration 阶段运行我的量角器测试:

<plugin>
    <groupId>com.github.eirslett</groupId>
    <artifactId>frontend-maven-plugin</artifactId>
    <configuration>
        ...
    </configuration>
    <executions>
        <execution>
            <id>install node and npm</id>
            <goals>
                <goal>install-node-and-npm</goal>
            </goals>
            <phase>generate-resources</phase>
        </execution>
        <execution>
            <id>npm install</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <phase>generate-resources</phase>
            <configuration>
                <arguments>install</arguments>
            </configuration>
        </execution>
        <execution>
            <id>npm run build</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <phase>generate-resources</phase>
            <configuration>
                <arguments>run build</arguments>
            </configuration>
        </execution>
        <execution>
            <id>npm run integration tests</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <phase>integration-test</phase>
            <configuration>
                <arguments>run e2e</arguments>
                <testFailureIgnore>true</testFailureIgnore> // this should probably be deleted
            </configuration>
        </execution>
    </executions>
</plugin>

我将testFailureIgnore = true 添加到frontend-maven-plugin 中,因为如果任何量角器测试失败,它将在执行post-integration 阶段之前停止我的maven 构建。这会导致测试服务器继续使用该端口运行。任何后续运行都将失败,因为该端口已在使用中,直到该服务器被杀死(手动)。 testFailureIgnore 属性允许构建忽略失败的测试,有效地让我继续 post-integration 阶段。

明显的缺点是即使测试失败,我的构建也会打印 SUCCESS。我正在寻找类似于failsafe 插件的行为,其中失败的测试将使我的构建失败,但仍会首先执行post-integration 阶段以正确清理。

我似乎找不到合适的解决方案,但我肯定不会是第一个遇到这个问题的人。有哪些解决方案/替代方案可用于此?我想使用exec-maven-plugin 而不是frontend-maven-plugin 会导致同样的问题。

【问题讨论】:

    标签: spring-boot maven


    【解决方案1】:

    我没有设法在任何地方找到合适的解决方案,因此我决定尝试创建自己的解决方案。我扩展了frontend-maven-plugin,使用了一个在integration-test 阶段记录集成测试失败的参数,但只在verify 阶段失败了构建。这允许post-integration-test 阶段完成。

    我的解决方案可从my repository(版本1.9.1-failsafe)获得。此实现需要添加配置参数 integrationTestFailureAfterPostIntegration。不幸的是,我没有弄清楚如何在没有用户干预的情况下让 Mojo 执行在稍后阶段触发另一个 Mojo 执行。因此,用户需要在verify 阶段触发执行,即使它在功能上没有做任何有用的事情(即npm -version)。

    我的工作示例:

    <execution>
        <id>npm run integration tests</id>
        <goals>
            <goal>npm</goal>
        </goals>
        <phase>integration-test</phase>
        <configuration>
            <arguments>run e2e</arguments>
            <integrationTestFailureAfterPostIntegration>true</integrationTestFailureAfterPostIntegration>
        </configuration>
    </execution>
    <execution>
        <id>fail any integration tests</id>
        <goals>
            <goal>npm</goal>
        </goals>
        <phase>verify</phase>
        <configuration>
            <arguments>-version</arguments>
        </configuration>
    </execution>
    

    如果任何 IT 测试失败,它们将在 integration-test 阶段记录并在 verify 失败构建。如果所有 IT 测试均通过,则构建将成功。

    我在frontend-maven-plugin 有一个open pull request,它可能会添加到1.9.2 版本中。我仍然会尝试通过消除手动添加 verify 执行阶段的需要来改进更改。欢迎对拉取请求提出建议或改进!

    更新:我已经发布了自己的版本,以防拉取请求未通过:

    <dependency>
        <groupId>io.github.alexandertang</groupId>
        <artifactId>frontend-maven-plugin</artifactId>
        <version>1.9.1-failsafe</version>
    </dependency>
    

    在这个版本中,我添加了一个verify mojo,它将第二次执行简化为:

    <execution>
        <id>fail any integration tests</id>
        <goals>
            <goal>verify</goal>
        </goals>
        <phase>verify</phase>   <!--default phase is verify, so this is optional-->
    </execution>
    

    【讨论】:

      【解决方案2】:

      我解决了这个问题,把这个说明放在 package.json 上

      "scripts": {
        ...
        "e2e": "ng e2e && echo Success > e2e/result.txt || echo Error > e2e/result.txt"
      }
      

      这将在错误情况下抑制退出代码,并在您的内容中记录一个名为 result.txt 的文件,其中包含成功或错误。

      然后,我在maven上添加maven-verifier-plugin来验证文件result.txt的内容。

      【讨论】:

        猜你喜欢
        • 2017-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多