4

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

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

4

5 回答 5

4

您可以使用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 特定的命令。

于 2008-10-06T18:48:44.927 回答
2

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

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

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

更新:

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

于 2008-10-06T19:24:48.427 回答
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>
于 2008-10-06T21:48:39.947 回答
1

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

pre-integration-test:启动外部应用程序(使用 exec 插件或此处的其他建议之一)

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

post-integration-test:关闭外部应用程序并进行任何其他必要的清理

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

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

于 2011-08-23T17:10:19.703 回答
0

是否要启动应用程序服务器?看看Cargo和它的Maven 插件

于 2008-10-06T21:40:00.840 回答