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

是否有Maven插件可以实现此目的?其他想法?

有帮助吗?

解决方案

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

像这样。

<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特定命令当然通过条件任务

其他提示

我目前正在研究一种更容易被“降级”的更具体的插件。要成为一个简单的外部任务执行者,但......还有很多其他事情需要考虑。

  1. 你怎么知道这个过程实际上已经开始了?
  2. 你如何处理返回代码?
  3. 如何确保执行程序插件先运行(将其绑定到测试编译阶段)?
  4. 我确信如果我真的开始开发这个插件会有更多,但是真的需要一个通用执行器吗?

    更新:

    我想有......在CodeHaus上有一套出色的Maven插件。这是你想要的: http://mojo.codehaus.org/exec-maven-plugin /

如果您正在进行servlet开发并希望部署生成的WAR进行集成测试,那么货物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>

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

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

集成测试:使用failsafe插件运行实际的集成测试

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

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

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

您想启动应用程序服务器吗?看看货物及其 Maven插件

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top