【问题标题】:Starting external process during integration testing in maven在 Maven 集成测试期间启动外部进程
【发布时间】:2010-09-15 14:14:35
【问题描述】:

我想要对 Maven 项目进行完全自动化的集成测试。集成测试要求在运行之前启动一个外部(平台相关)程序。理想情况下,外部程序会在单元测试完成后被杀死,但这不是必需的。

是否有一个 Maven 插件来完成这个?其他想法?

【问题讨论】:

    标签: java maven-2 build-process automated-tests integration-testing


    【解决方案1】:

    您可以使用antrun 插件。在里面你会使用 ant 的 exec apply 任务。

    类似的东西。

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.2</version>
        <executions>
          <execution>
            <phase> <!-- a lifecycle phase --> </phase>
            <configuration>
    
              <tasks>
                <apply os="unix" executable="cmd">
                  <arg value="/c"/>
                  <arg value="ant.bat"/>
                  <arg value="-p"/>
                </apply>
                <apply os="windows" executable="cmd.exe">
                  <arg value="/c"/>
                  <arg value="ant.bat"/>
                  <arg value="-p"/>
                </apply>
              </tasks>
    
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    

    Ant 支持 os 特定命令当然是通过condition task.

    【讨论】:

    • antrun 插件的一个有趣的、非直观的使用。我希望有更清洁的东西,但这可能对我有用。
    • Maven 倾向于针对特定任务使用插件。你想运行什么命令,它们可能有插件。
    • 命令是特定于项目的可执行文件,因此需要一个通用的“执行”插件。
    【解决方案2】:

    如果您正在进行 servlet 开发并希望部署生成的 WAR 以进行集成测试,那么 cargo maven 插件是一个不错的选择。

    当我自己做这个时,我经常建立一个多模块项目(虽然这不是严格必要的)并将所有的集成测试封装到一个模块中。然后,我启用带有配置文件(或不启用)的模块,这样它就不会立即阻止“是的,我知道我破坏了它”构建。

    这是来自该功能测试模块的 pom - 随心所欲地制作:

    <?xml version="1.0"?><project>
      <parent>
        <artifactId>maven-example</artifactId>
        <groupId>com.jheck</groupId>
        <version>1.5.0.4-SNAPSHOT</version>
      </parent>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.jheck.example</groupId>
      <artifactId>functional-test</artifactId>
      <name>Example Functional Test</name>
      <packaging>pom</packaging>
    
      <dependencies>
        <dependency>
          <groupId>com.jheck.example</groupId>
          <artifactId>example-war</artifactId>
          <type>war</type>
          <scope>provided</scope>
          <version>LATEST</version>
        </dependency>
        <dependency>
          <groupId>httpunit</groupId>
          <artifactId>httpunit</artifactId>
          <version>1.6.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
    
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <executions>
              <execution>
                <goals>
                  <goal>testCompile</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
    
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <executions>
              <execution>
                <phase>integration-test</phase>
                <goals>
                  <goal>test</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
    
          <plugin>
            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
            <version>0.3</version>
            <configuration>
              <wait>false</wait> <!-- don't pause on launching tomcat... -->
              <container>
                <containerId>tomcat5x</containerId>
                <log>${project.build.directory}/cargo.log</log>
                <zipUrlInstaller>
                  <!--
                  <url>http://www.apache.org/dist/tomcat/tomcat-5/v5.0.30/bin/jakarta-tomcat-5.0.30.zip</url>
                   -->
                   <!-- better be using Java 1.5... -->
                  <url>http://www.apache.org/dist/tomcat/tomcat-5/v5.5.26/bin/apache-tomcat-5.5.26.zip</url>
    
                  <installDir>${installDir}</installDir>
                </zipUrlInstaller>
              </container>
              <configuration>
                <!-- where the running instance will be deployed for testing -->
                <home>${project.build.directory}/tomcat5x/container</home>
              </configuration>
            </configuration>
    
            <executions>
              <execution>
                <id>start-container</id>
                <phase>pre-integration-test</phase>
                <goals>
                  <goal>start</goal>
                  <goal>deploy</goal>
                </goals>
                <configuration>
                  <deployer>
                    <deployables>
                      <deployable>
                        <groupId>com.jheck.example</groupId>
                        <artifactId>example-war</artifactId>
                        <type>war</type>
                        <!-- <properties>
                          <plan>${basedir}/src/deployment/geronima.plan.xml</plan>
                        </properties> -->
                        <pingURL>http://localhost:8080/example-war</pingURL>
                      </deployable>
                    </deployables>
                  </deployer>
                </configuration>
              </execution>
              <execution>
                <id>stop-container</id>
                <phase>post-integration-test</phase>
                <goals>
                  <goal>stop</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    
    </project>
    

    【讨论】:

      【解决方案3】:

      我目前正在开发一个更具体的插件,它可以很容易地“降级”为一个简单的外部任务执行器,但是......还有很多其他的事情需要考虑。

      1. 你怎么知道这个过程实际上已经开始了?
      2. 您如何处理返回码?
      3. 如何确保执行器插件首先运行(将其绑定到测试编译阶段)?

      如果我真的开始开发插件,我敢肯定会有更多,但真的需要通用执行器吗?

      更新:

      我猜有……在 CodeHaus 有一组出色的 Maven 插件。这是您想要的:http://mojohaus.org/exec-maven-plugin/

      【讨论】:

        【解决方案4】:

        您可能希望将实际集成测试绑定到 maven 生命周期的集成测试阶段。如果您使用故障安全插件(如恰当命名的故障安全插件)来进行实际测试,您可以像这样运行您的阶段:

        预集成测试:启动外部应用程序(使用 exec 插件或此处的其他建议之一)

        集成测试:使用故障安全插件运行实际的集成测试

        集成后测试:关闭外部应用程序并进行任何其他必要的清理

        验证:让故障安全插件验证测试结果,此时构建失败

        使用 exec 插件相当简单,诀窍是让您的应用程序在后台启动。在开始下一阶段的测试之前,您应该小心确保应用程序已完全启动。不幸的是,让您的应用程序启动并确保它在后台运行时足够启动并不总是一项微不足道的任务,具体如何做到这一点取决于您的应用程序。它通常涉及应用程序中的自定义代码。

        【讨论】:

          【解决方案5】:

          您要启动应用程序服务器吗?看看Cargo 和它的Maven plugin

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-06-15
            • 1970-01-01
            • 2012-06-23
            • 1970-01-01
            • 1970-01-01
            • 2011-01-24
            相关资源
            最近更新 更多